From 64e5feb095e3c396a020809f82b8581c6878004c Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 16 Feb 2021 13:57:12 -0600 Subject: [PATCH] added basic font rendering --- dist/index.js | 10 ++++++++++ src/prez.onyx | 4 ++++ 2 files changed, 14 insertions(+) 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) { -- 2.25.1