├── LICENSE ├── Makefile ├── README.md └── main.c /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 programmeruser2 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SRC = main.c 2 | 3 | .PHONY: all 4 | all: main.wasm 5 | 6 | main.wasm: 7 | clang --target=wasm32 -nostdlib -Wl,--no-entry -Wl,--export-all -Wl,--allow-undefined -o $@ $(SRC) 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wasm-canvas 2 | [![Run on repl.it](https://repl.it/badge/github/programmeruser2/wasm-canvas)](https://repl.it/github/programmeruser2/wasm-canvas) 3 | 4 | A simple example of calling JavaScript functions from C to draw on the canvas. 5 | 6 | https://programmeruser2.github.io/wasm-canvas 7 | 8 | ## Imports Object 9 | ``` 10 | const canvas = //... HTMLElement representing a canvas 11 | const ctx = //... Canvas2DRenderingContext 12 | const imports = { 13 | env: { 14 | getCanvasWidth: function() { return canvas.width; }, 15 | getCanvasHeight: function() { return canvas.height; }, 16 | jsSetInterval: function(callback, interval) { 17 | setInterval(function() { 18 | instance.exports.runCallback(callback); 19 | },interval); 20 | }, 21 | jsFillRect: function(x,y,w,h) { 22 | ctx.fillRect(x,y,w,h); 23 | }, 24 | jsClearRect: function(x,y,w,h) { 25 | ctx.clearRect(x,y,w,h); 26 | } 27 | } 28 | }; 29 | ``` 30 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | //imports 2 | extern void jsSetInterval(void (*callback)(void), float interval); 3 | extern void jsFillRect(int x, int y, int w, int h); 4 | extern void jsClearRect(int x, int y, int w, int h); 5 | extern int getCanvasWidth(void); 6 | extern int getCanvasHeight(void); 7 | 8 | typedef struct { 9 | float x; 10 | float y; 11 | float width; 12 | float height; 13 | int direction; 14 | } Sprite; 15 | Sprite* sprite; 16 | 17 | void runCallback(void (*callback)()) { 18 | return callback(); 19 | } 20 | 21 | void mainloop(void) { 22 | jsClearRect(0,0,getCanvasWidth(), getCanvasHeight()); 23 | if(sprite->x + sprite->width == getCanvasWidth()) { 24 | sprite->direction = -1; 25 | } else if(sprite->x == 0) { 26 | sprite->direction = 1; 27 | } 28 | sprite->x += 50 * sprite->direction; 29 | jsFillRect(sprite->x, sprite->y, sprite->width, sprite->height); 30 | } 31 | 32 | int main(void) { 33 | sprite->x = 0; 34 | sprite->y = 0; 35 | sprite->width = 50; 36 | sprite->height = 50; 37 | sprite->direction = 1; 38 | jsSetInterval(mainloop, 16.67); 39 | return 0; 40 | } 41 | --------------------------------------------------------------------------------