text from rust
This commit is contained in:
parent
5073e03b6a
commit
ddb064cdf4
3 changed files with 59 additions and 20 deletions
|
@ -1,34 +1,55 @@
|
|||
var ctx;
|
||||
var image;
|
||||
var memory;
|
||||
const width = 256;
|
||||
const height = 224;
|
||||
|
||||
function blit_frame() {
|
||||
ctx.putImageData(image, 0, 0);
|
||||
}
|
||||
|
||||
function blit_text(text, len, x, y, size) {
|
||||
let decoded = (new TextDecoder())
|
||||
.decode(new Uint8Array(memory.buffer, text, len));
|
||||
ctx.font = size +'vh serif'
|
||||
ctx.fillText(decoded,x,y);
|
||||
}
|
||||
|
||||
async function init() {
|
||||
const { instance } = await WebAssembly.instantiateStreaming(
|
||||
fetch("./index.wasm")
|
||||
);
|
||||
|
||||
const width = 600;
|
||||
const height = 600;
|
||||
|
||||
const canvas = document.getElementById("window");
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
|
||||
ctx = canvas.getContext("2d");
|
||||
|
||||
const { instance } = await WebAssembly.instantiateStreaming(
|
||||
fetch("./index.wasm"),
|
||||
{
|
||||
"env" : {
|
||||
"js_sin": Math.sin,
|
||||
"blit_frame": blit_frame,
|
||||
"blit_text": blit_text,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
memory = instance.exports.memory
|
||||
const buffer_address = instance.exports.BUFFER.value;
|
||||
const image = new ImageData(
|
||||
image = new ImageData(
|
||||
new Uint8ClampedArray(
|
||||
instance.exports.memory.buffer,
|
||||
memory.buffer,
|
||||
buffer_address,
|
||||
4 * width * height,
|
||||
),
|
||||
width,
|
||||
);
|
||||
|
||||
const ctx = canvas.getContext("2d");
|
||||
instance.exports.frame_entry();
|
||||
ctx.textBaseline = 'top'
|
||||
ctx.textAlign = 'left';
|
||||
|
||||
const render = () => {
|
||||
instance.exports.frame_entry();
|
||||
ctx.putImageData(image, 0, 0);
|
||||
ctx.font = '84px sans-serif'
|
||||
ctx.textBaseline = 'top'
|
||||
ctx.textAlign = 'left';
|
||||
ctx.fillText("demo",12,12);
|
||||
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
|
|
7
justfile
7
justfile
|
@ -7,10 +7,11 @@ build: minify-js minify-rust minify-html
|
|||
|
||||
build-rust:
|
||||
cargo build --manifest-path pirates/Cargo.toml --target wasm32-unknown-unknown --release
|
||||
cp pirates/target/wasm32-unknown-unknown/release/pirates.wasm front/index.wasm
|
||||
|
||||
minify-rust: build-rust build-dir
|
||||
wasm-strip pirates/target/wasm32-unknown-unknown/release/pirates.wasm
|
||||
wasm-opt -o build/index.wasm -Oz pirates/target/wasm32-unknown-unknown/release/pirates.wasm
|
||||
wasm-strip front/index.wasm
|
||||
wasm-opt -o build/index.wasm -Oz front/index.wasm
|
||||
|
||||
minify-js: build-dir
|
||||
#minify-js -m module --output build/index.js front/index.js
|
||||
|
@ -29,7 +30,7 @@ check-size: build
|
|||
check-zip: zip
|
||||
unzip -v build/release.zip | awk '{printf ("%5s\t%s\n", $3, $8)}'
|
||||
|
||||
@cat build/release.zip | wc -c | xargs -I {} python3 -c "print(str(round({}/(13*1024),2))+'%')"
|
||||
@cat build/release.zip | wc -c | xargs -I {} python3 -c "print(str(round({}/(13*1024)*100,2))+'%')"
|
||||
|
||||
zip: build
|
||||
zip -r build/release.zip build -x release.zip
|
||||
|
|
|
@ -7,8 +7,22 @@ fn handle_panic(_: &core::panic::PanicInfo) -> ! {
|
|||
loop {}
|
||||
}
|
||||
|
||||
const WIDTH: usize = 600;
|
||||
const HEIGHT: usize = 600;
|
||||
extern {
|
||||
fn blit_frame();
|
||||
fn blit_text(text: *const u8, len: u32, x: i32, y: i32, size: u8);
|
||||
}
|
||||
|
||||
fn draw_text(text: &str, x: i32, y: i32, size: u8) {
|
||||
unsafe {
|
||||
blit_text(
|
||||
(*text).as_ptr(),
|
||||
text.len() as u32,
|
||||
x, y, size)
|
||||
}
|
||||
}
|
||||
|
||||
const WIDTH: usize = 256;
|
||||
const HEIGHT: usize = 224;
|
||||
|
||||
#[no_mangle]
|
||||
static mut BUFFER: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT];
|
||||
|
@ -28,6 +42,9 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
|
|||
buffer[y*WIDTH + x] = frame.wrapping_add((x^y) as u32) | 0xFF000000;
|
||||
}
|
||||
}
|
||||
unsafe { blit_frame(); }
|
||||
|
||||
draw_text("hi from rust", 0,100,1);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in a new issue