Skip to content

Commit

Permalink
fix: koch_snowflake
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-jerry-ye committed Apr 3, 2024
1 parent e6306a4 commit b256b91
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 22 deletions.
5 changes: 2 additions & 3 deletions examples/koch_snowflake/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
run:
moon build
wat2wasm target/build/main/main.wat --output=target/draw.wasm
npx serve ./
moon build --target wasm-gc
python3 -m http.server 8000
4 changes: 2 additions & 2 deletions examples/koch_snowflake/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@

var canvas = document.getElementById("canvas");

WebAssembly.instantiateStreaming(fetch("target/draw.wasm"), spectest).then(
WebAssembly.instantiateStreaming(fetch("target/wasm-gc/release/build/lib/lib.wasm"), spectest).then(
(obj) => {
obj.instance.exports._start();
const draw = obj.instance.exports["koch_snowflake/main::draw"];
const draw = obj.instance.exports["draw"];
var ctx = canvas.getContext("2d");
ctx.strikeStyle="#000";
draw(ctx, 1);
Expand Down
4 changes: 2 additions & 2 deletions examples/koch_snowflake/lib/koch.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn cos(x : Double) -> Double = "canvas" "cos"
fn get_pi() -> Double = "canvas" "get_pi"

fn koch(ctx : Canvas_ctx, x1 : Double, y1 : Double, x2 : Double,
y2 : Double, n : Double, m : Double) {
y2 : Double, n : Double, m : Double) -> Unit {
ctx.clear_rect(0.0, 0.0, 400.0, 400.0)
let x3 = (x2 - x1) / 3.0 + x1
let y3 = (y2 - y1) / 3.0 + y1
Expand All @@ -42,7 +42,7 @@ fn koch(ctx : Canvas_ctx, x1 : Double, y1 : Double, x2 : Double,
koch(ctx, x4, y4, x2, y2, n, m)
}

pub fn draw(ctx : Canvas_ctx, deep : Double) {
pub fn draw(ctx : Canvas_ctx, deep : Double) -> Unit {
ctx.begin_path()
let y = 80.0 + cos(get_pi() / 6.0) * 200.0
koch(ctx, 150.0, y, 250.0, 80.0, 0.0, deep)
Expand Down
8 changes: 7 additions & 1 deletion examples/koch_snowflake/lib/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"name": "lib"
"link": {
"wasm-gc": {
"exports": [
"draw"
]
}
}
}
8 changes: 0 additions & 8 deletions examples/koch_snowflake/main/main.mbt

This file was deleted.

6 changes: 0 additions & 6 deletions examples/koch_snowflake/main/moon.pkg.json

This file was deleted.

0 comments on commit b256b91

Please sign in to comment.