added basic font rendering
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 16 Feb 2021 19:57:12 +0000 (13:57 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 16 Feb 2021 19:57:12 +0000 (13:57 -0600)
dist/index.js
src/prez.onyx

index df5e9cb0a77000c6ae12c1dbc83e9770cd1fc510..345ee1f567bf19698b2f418937e7a01d52c08aae 100644 (file)
@@ -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);
     }
 }
 
index 848733503d47846806d02a251f0f872205017c6b..56af4821181ea959cd257301725275ef21f749b1 100644 (file)
@@ -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) {