From: Brendan Hansen Date: Tue, 16 Feb 2021 19:57:12 +0000 (-0600) Subject: added basic font rendering X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=64e5feb095e3c396a020809f82b8581c6878004c;p=onyx-prez.git added basic font rendering --- diff --git a/dist/index.js b/dist/index.js index df5e9cb..345ee1f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -33,6 +33,16 @@ let canvas_import_obj = { fillRect(canvas, x, y, w, h, r, g, b, a) { canvasCtx.fillStyle = `rgba(${r * 255}, ${g * 255}, ${b * 255}, ${a})`; canvasCtx.fillRect(x, y, w, h); + }, + + fillText(canvas, text_ptr, text_len, x, y, max_width) { + const data = new Uint8Array(wasm_instance.exports.memory.buffer, text_ptr, text_len); + const str = new TextDecoder().decode(data); + + canvasCtx.fillStyle = "#ffffff"; + + if (max_width > 0) canvasCtx.fillText(str, x, y, max_width); + else canvasCtx.fillText(str, x, y); } } diff --git a/src/prez.onyx b/src/prez.onyx index 8487335..56af482 100644 --- a/src/prez.onyx +++ b/src/prez.onyx @@ -11,6 +11,8 @@ Canvas :: struct { fill_rect :: (handle: Handle, x: f32, y: f32, w: f32, h: f32, r: f32, g: f32, b: f32, a := 1.0f) -> void #foreign "canvas" "fillRect" --- + + fill_text :: (handle: Handle, text: str, x: f32, y: f32, max_width: f32 = -1.0f) -> void #foreign "canvas" "fillText" --- } setup_canvas :: () { @@ -26,6 +28,8 @@ setup_canvas :: () { width, height := cast(f32) get_width(canvas), cast(f32) get_height(canvas); fill_rect(canvas, width / 2, height / 2, width / 2, height / 2, 1, 0, 0); + + fill_text(canvas, "Hello, World!!", width / 2, height / 2); } main :: (args: [] cstr) {