-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
node.ts
58 lines (50 loc) · 1.49 KB
/
node.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
46
47
48
49
50
51
52
53
54
55
56
57
58
/* eslint-disable no-restricted-globals */
import type { Canvas as NodeCanvas } from 'canvas';
import { JSDOM } from 'jsdom';
// @ts-ignore
import utils from 'jsdom/lib/jsdom/living/generated/utils.js';
import { config } from '../config';
import { NodeGLProbe } from '../filters/GLProbes/NodeGLProbe';
import { setEnv } from './index';
import type { TCopyPasteData, TFabricEnv } from './types';
const { implForWrapper: jsdomImplForWrapper } = utils;
const copyPasteData: TCopyPasteData = {};
const { window: JSDOMWindow } = new JSDOM(
decodeURIComponent(
'%3C!DOCTYPE%20html%3E%3Chtml%3E%3Chead%3E%3C%2Fhead%3E%3Cbody%3E%3C%2Fbody%3E%3C%2Fhtml%3E'
),
{
resources: 'usable',
// needed for `requestAnimationFrame`
pretendToBeVisual: true,
}
);
config.configure({
devicePixelRatio: JSDOMWindow.devicePixelRatio || 1,
});
export const getNodeCanvas = (canvasEl: HTMLCanvasElement) => {
const impl = jsdomImplForWrapper(canvasEl);
return (impl._canvas || impl._image) as NodeCanvas;
};
export const dispose = (element: Element) => {
const impl = jsdomImplForWrapper(element);
if (impl) {
impl._image = null;
impl._canvas = null;
// unsure if necessary
impl._currentSrc = null;
impl._attributes = null;
impl._classList = null;
}
};
export const getEnv = (): TFabricEnv => {
return {
document: JSDOMWindow.document,
window: JSDOMWindow,
isTouchSupported: false,
WebGLProbe: new NodeGLProbe(),
dispose,
copyPasteData,
};
};
setEnv(getEnv());