-
Notifications
You must be signed in to change notification settings - Fork 12
/
q5-server.js
60 lines (50 loc) · 1.36 KB
/
q5-server.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
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @module q5-server
* @description Run q5 with node.js using node-canvas and jsdom
*/
let depsLoaded = false;
try {
global.SkiaCanvas ??= require('skia-canvas');
global.JSDOM ??= require('jsdom').JSDOM;
depsLoaded = true;
} catch (e) {
require('./q5.js');
module.exports = Q5;
}
if (depsLoaded) {
global.window = new JSDOM('', { url: 'http://localhost' }).window;
{
let props = Object.getOwnPropertyNames(window).concat(Object.getOwnPropertyNames(Object.getPrototypeOf(window)));
for (let prop of props) {
if (!global[prop]) {
Object.defineProperty(global, prop, Object.getOwnPropertyDescriptor(window, prop));
}
}
global.Event = window.Event;
}
global.Image = window.Image = SkiaCanvas.Image;
global.Window ??= SkiaCanvas.Window;
require('./q5.js');
Q5._createServerCanvas = function () {
let skiaCanvas = new SkiaCanvas.Canvas(...arguments);
let domCanvas = document.createElement('canvas');
skiaCanvas.save = skiaCanvas.saveAsSync;
return new Proxy(skiaCanvas, {
get: function (target, prop) {
let t = prop in target ? target : domCanvas;
let p = t[prop];
if (typeof p === 'function') return p.bind(t);
return p;
},
set: function (target, prop, value) {
if (prop in target) {
target[prop] = value;
} else {
domCanvas[prop] = value;
}
return true;
}
});
};
module.exports = Q5;
}