-
Notifications
You must be signed in to change notification settings - Fork 1
/
canvas.js
192 lines (163 loc) · 4.56 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// From https://github.com/akira-cn/node-canvas-webgl/blob/master/lib/canvas.js
const {Canvas, Image} = require('canvas');
const EventEmitter = require('events');
const createGLContext = require('gl');
const _ctx = Symbol('ctx');
function putImageData(gl, canvas) {
const {width, height} = canvas;
const ctx = canvas[_ctx];
const data = ctx.getImageData(0, 0, width, height);
const pixels = new Uint8Array(width * height * 4);
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
for(let i = 0; i < height; i++) {
for(let j = 0; j < width; j++) {
const col = j;
const row = height - i - 1;
for(let k = 0; k < 4; k++) {
const idx = 4 * (row * width + col) + k;
const idx2 = 4 * (i * width + col) + k;
data.data[idx] = pixels[idx2];
}
}
}
ctx.putImageData(data, 0, 0);
return ctx;
}
class NodeCanvasElement extends Canvas {
constructor(...args) {
super(...args);
this.__event__ = new EventEmitter();
this.__attributes__ = {};
this.style = {};
}
get width() {
return super.width;
}
set width(value) {
if(this.__gl__) {
const ext = this.__gl__.getExtension('STACKGL_resize_drawingbuffer');
ext.resize(value, this.height);
}
super.width = value;
}
get height() {
return super.height;
}
set height(value) {
if(this.__gl__) {
const ext = this.__gl__.getExtension('STACKGL_resize_drawingbuffer');
ext.resize(this.width, value);
}
super.height = value;
}
get clientWidth() {
return super.width;
}
get clientHeight() {
return super.height;
}
get __ctx__() {
const gl = this.__gl__;
if(gl) {
putImageData(gl, this);
}
return this[_ctx];
}
getContext(type, options) {
if(this.__contextType__ && this.__contextType__ !== type) return null;
if(this.__gl__) return this.__gl__;
this.__contextType__ = type;
if(type === 'webgl' || type === 'webgl2') {
const {width, height} = this;
this[_ctx] = super.getContext('2d', options);
const ctx = createGLContext(width, height, options);
const _getUniformLocation = ctx.getUniformLocation;
// Temporary fix https://github.com/stackgl/headless-gl/issues/170
ctx.getUniformLocation = function (program, name) {
if(program._uniforms && !/\[\d+\]$/.test(name)) {
const reg = new RegExp(`${name}\\[\\d+\\]$`);
for(let i = 0; i < program._uniforms.length; i++) {
const _name = program._uniforms[i].name;
if(reg.test(_name)) {
name = _name;
}
}
}
return _getUniformLocation.call(this, program, name);
};
ctx.canvas = this;
const _tetImage2D = ctx.texImage2D;
ctx.texImage2D = function (...args) {
let pixels = args[args.length - 1];
if(pixels && pixels._image) pixels = pixels._image;
if(pixels instanceof Image) {
const canvas = new Canvas(pixels.width, pixels.height);
canvas.getContext('2d').drawImage(pixels, 0, 0);
args[args.length - 1] = canvas;
}
return _tetImage2D.apply(this, args);
};
this.__gl__ = ctx;
return this.__gl__;
}
return super.getContext(type, options);
}
toBuffer(...args) {
const gl = this.__gl__;
if(gl) {
putImageData(gl, this);
}
return super.toBuffer(...args);
}
toDataURL(...args) {
const gl = this.__gl__;
if(gl) {
putImageData(gl, this);
}
return super.toDataURL(...args);
}
createPNGStream(...args) {
const gl = this.__gl__;
if(gl) {
putImageData(gl, this);
}
return super.createPNGStream(...args);
}
createJPEGStream(...args) {
const gl = this.__gl__;
if(gl) {
putImageData(gl, this);
}
return super.createJPEGStream(...args);
}
createPDFStream(...args) {
const gl = this.__gl__;
if(gl) {
putImageData(gl, this);
}
return super.createPDFStream(...args);
}
addEventListener(type, listener) {
return this.__event__.addListener(type, listener);
}
removeEventListener(type, listener) {
if(listener) {
return this.__event__.removeListener(type, listener);
}
return this.removeAllListeners(type);
}
dispatchEvent(event) {
event.target = this;
return this.emit(event.type, event);
}
setAttribute(key, value) {
this.__attributes__[key] = value;
}
getAttribute(key) {
return this.__attributes__[key];
}
removeAttribute(key) {
delete this.__attributes__[key];
}
}
module.exports = NodeCanvasElement;