-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-load.ts
45 lines (37 loc) · 1.04 KB
/
image-load.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// TODO:
// - draw image + other shapes on the same canvas
import { ARGB8888, imageFromURL, intBufferFromImage } from "@thi.ng/pixel";
import { ssam, type Sketch, type SketchSettings } from "ssam";
// import image from public directory
import IMG from "/c.png";
export const sketch: Sketch<"2d"> = async ({
wrap,
context: ctx,
width,
height,
pixelRatio,
}) => {
// load image as <img> element
const img = await imageFromURL(IMG);
const buf = intBufferFromImage(img, ARGB8888);
console.log(buf);
// cubic, linear, nearest
const resized = buf.resize(
width * pixelRatio,
height * pixelRatio,
"nearest",
);
wrap.render = ({ width, height }) => {
ctx.fillStyle = `gray`;
ctx.fillRect(0, 0, width, height);
// ctx.drawImage(img, 0, 0, width, height);
resized.blitCanvas(ctx);
};
};
export const settings: SketchSettings = {
dimensions: [800, 800],
pixelRatio: window.devicePixelRatio,
animate: false,
filename: import.meta.url?.split("/").pop()?.split(".")[0] || undefined,
};
ssam(sketch, settings);