Deno version

This commit is contained in:
2025-05-13 21:56:41 +02:00
parent f9cf455cf8
commit 3d69229114
5 changed files with 136 additions and 0 deletions

1
base-deno/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
mandelbrot.png

3
base-deno/.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"deno.enable": true
}

9
base-deno/deno.json Normal file
View File

@@ -0,0 +1,9 @@
{
"tasks": {
"dev": "deno run --allow-write main.ts",
"devwatch": "deno run --allow-write --watch main.ts"
},
"imports": {
"@std/assert": "jsr:@std/assert@1"
}
}

44
base-deno/deno.lock generated Normal file
View File

@@ -0,0 +1,44 @@
{
"version": "4",
"specifiers": {
"jsr:@std/assert@1": "1.0.13",
"jsr:@std/internal@^1.0.6": "1.0.7"
},
"jsr": {
"@std/assert@1.0.13": {
"integrity": "ae0d31e41919b12c656c742b22522c32fb26ed0cba32975cb0de2a273cb68b29",
"dependencies": [
"jsr:@std/internal"
]
},
"@std/internal@1.0.7": {
"integrity": "39eeb5265190a7bc5d5591c9ff019490bd1f2c3907c044a11b0d545796158a0f"
}
},
"remote": {
"https://deno.land/std@0.106.0/encoding/base64.ts": "eecae390f1f1d1cae6f6c6d732ede5276bf4b9cd29b1d281678c054dc5cc009e",
"https://deno.land/x/canvas@v1.4.1/deps.ts": "e956026d98094946166e06d7b799290b732db015813870d84e04e33ab88e98f3",
"https://deno.land/x/canvas@v1.4.1/mod.ts": "a4e16972647ceafef58612a377a218372454c99d2c9da615a132694597114f80",
"https://deno.land/x/canvas@v1.4.1/src/base64.ts": "0928031fdba0c43b617154fbe2eb7578366460c04da1422933ae5e936d3d0349",
"https://deno.land/x/canvas@v1.4.1/src/canvas.ts": "58119999b04f68ebeed2627485c5c24c5b0c029707edde0b6568814f9049a3a8",
"https://deno.land/x/canvas@v1.4.1/src/canvaskit.ts": "c3d807472cbb3e1d9fc01bb43ff974ef796c4b010178d1595be5fa793cce5e7d",
"https://deno.land/x/canvas@v1.4.1/src/color_util.ts": "28f1072f0a5acbe7add7fac2f452311a47b44c080806fc4057de2d2e405c6c1c",
"https://deno.land/x/canvas@v1.4.1/src/lib.js": "bb21711589bfbc8997b455cdf53e3150e23289f3b44809188041b1d2fc7924fa",
"https://deno.land/x/canvas@v1.4.1/src/types.ts": "67d5800f8f4b0a407e0251676a03ae91b5f50a3ed53e6b72dc5984113cb93128",
"https://deno.land/x/canvas@v1.4.1/src/wasm.js": "449d72cc14fc4142a5853f944df49a744d852981d09c5515528ede8aebb0afda",
"https://deno.land/x/canvas@v1.4.2/deps.ts": "e956026d98094946166e06d7b799290b732db015813870d84e04e33ab88e98f3",
"https://deno.land/x/canvas@v1.4.2/mod.ts": "a4e16972647ceafef58612a377a218372454c99d2c9da615a132694597114f80",
"https://deno.land/x/canvas@v1.4.2/src/base64.ts": "0928031fdba0c43b617154fbe2eb7578366460c04da1422933ae5e936d3d0349",
"https://deno.land/x/canvas@v1.4.2/src/canvas.ts": "58119999b04f68ebeed2627485c5c24c5b0c029707edde0b6568814f9049a3a8",
"https://deno.land/x/canvas@v1.4.2/src/canvaskit.ts": "c3d807472cbb3e1d9fc01bb43ff974ef796c4b010178d1595be5fa793cce5e7d",
"https://deno.land/x/canvas@v1.4.2/src/color_util.ts": "28f1072f0a5acbe7add7fac2f452311a47b44c080806fc4057de2d2e405c6c1c",
"https://deno.land/x/canvas@v1.4.2/src/lib.js": "55c4064064870313439dd7ef5297374cb0cbed2dc815bd064c3ace6e067753b0",
"https://deno.land/x/canvas@v1.4.2/src/types.ts": "67d5800f8f4b0a407e0251676a03ae91b5f50a3ed53e6b72dc5984113cb93128",
"https://deno.land/x/canvas@v1.4.2/src/wasm.js": "449d72cc14fc4142a5853f944df49a744d852981d09c5515528ede8aebb0afda"
},
"workspace": {
"dependencies": [
"jsr:@std/assert@1"
]
}
}

79
base-deno/main.ts Normal file
View File

@@ -0,0 +1,79 @@
// deno run --allow-write mandelbrot.ts
import { createCanvas } from "https://deno.land/x/canvas@v1.4.2/mod.ts";
const WIDTH = 1024*2;
const HEIGHT = 1024*2;
const MAX_ITER = 1000;
const ZOOM = 0.8;
const X_CENTER = -0.75;
const Y_CENTER = 0.0;
// Converts HSV to RGB [0..1] input/output
function hsvToRgb(h: number, s: number, v: number): [number, number, number] {
let c = v * s;
let x = c * (1 - Math.abs(((h / 60) % 2) - 1));
let m = v - c;
let r = 0, g = 0, b = 0;
if (0 <= h && h < 60) [r, g, b] = [c, x, 0];
else if (60 <= h && h < 120) [r, g, b] = [x, c, 0];
else if (120 <= h && h < 180) [r, g, b] = [0, c, x];
else if (180 <= h && h < 240) [r, g, b] = [0, x, c];
else if (240 <= h && h < 300) [r, g, b] = [x, 0, c];
else if (300 <= h && h < 360) [r, g, b] = [c, 0, x];
return [
Math.round((r + m) * 255),
Math.round((g + m) * 255),
Math.round((b + m) * 255),
];
}
// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts
if (import.meta.main) {
const canvas = createCanvas(WIDTH, HEIGHT);
const ctx = canvas.getContext("2d");
/* deno-canvas has broader compatibility with putImageData than node-canvas, so we can use it as below. */
const imageData = ctx.createImageData(WIDTH, HEIGHT);
const data = imageData.data;
const start = new Date();
for (let y = 0; y < HEIGHT; y++) {
for (let x = 0; x < WIDTH; x++) {
const cx = (x - WIDTH / 2) * (4 / WIDTH) * ZOOM + X_CENTER;
const cy = (y - HEIGHT / 2) * (4 / HEIGHT) * ZOOM + Y_CENTER;
let zx = 0, zy = 0;
let iter = 0;
while (zx * zx + zy * zy < 4 && iter < MAX_ITER) {
const tmp = zx * zx - zy * zy + cx;
zy = 2 * zx * zy + cy;
zx = tmp;
iter++;
}
let pixelIndex = 4 * (y * WIDTH + x);
if (iter === MAX_ITER) {
data[pixelIndex + 0] = 0;
data[pixelIndex + 1] = 0;
data[pixelIndex + 2] = 0;
data[pixelIndex + 3] = 255;
} else {
let zn = Math.sqrt(zx * zx + zy * zy);
let nu = Math.log(Math.log(zn)) / Math.log(2);
let smoothIter = iter + 1 - nu;
let hue = 360 * smoothIter * smoothIter / MAX_ITER;
let rgb = hsvToRgb(hue, 1, 1);
data[pixelIndex + 0] = rgb[0];
data[pixelIndex + 1] = rgb[1];
data[pixelIndex + 2] = rgb[2];
data[pixelIndex + 3] = 255;
}
}
}
const stop = new Date();
ctx.putImageData(imageData, 0, 0);
// Save PNG
await Deno.writeFile("mandelbrot.png", canvas.toBuffer("image/png"));
const doneSecs = (stop.getTime()-start.getTime())/1000;
console.log(`Done! in ${doneSecs} sec`);
}