-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcanvas.js
48 lines (44 loc) · 1.1 KB
/
canvas.js
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
46
47
48
import { createCanvas as npcc } from '@napi-rs/canvas';
export function createCanvas(width, height) {
const canvas = npcc(width, height);
const baseGetContext = canvas.getContext.bind(canvas);
let ctx;
canvas.style = {};
canvas.getContext = function getContext(type) {
// TODO handle webgl
if (!ctx) {
ctx = baseGetContext(type);
const baseDrawImage = ctx.drawImage.bind(ctx);
ctx.drawImage = (image, ...args) => {
if (image) {
if (image._imgImpl) {
baseDrawImage(image._imgImpl, ...args);
} else {
baseDrawImage(image, ...args);
}
}
};
}
return ctx;
}
canvas.getBoundingClientRect = () => {
return {
x: 0,
y: 0,
top: 0,
left: 0,
right: canvas.width,
bottom: canvas.height,
width: canvas.width,
height: canvas.height,
};
}
canvas.parent = globalThis.document.body;
globalThis.document.body._canvas = canvas;
return canvas;
}
export class OffscreenCanvas {
constructor(width, height) {
return createCanvas(width, height);
}
}