forked from mikeseven/node-webgl
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathworker.js
More file actions
45 lines (33 loc) · 950 Bytes
/
worker.js
File metadata and controls
45 lines (33 loc) · 950 Bytes
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
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');
if (isMainThread) {
const createContext = require('../..');
gl(1, 1);
module.exports = function (dimension) {
return new Promise((resolve, reject) => {
const worker = new Worker(__filename, {
workerData: dimension,
});
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0) {
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
});
};
} else {
const createContext = require('../..');
const dimension = workerData;
const width = dimension;
const height = dimension;
const gl = createContext(width, height);
gl.clearColor(0, 1, 0, 0.5)
gl.clear(gl.COLOR_BUFFER_BIT)
var pixels = new Uint8Array(width * height * 4)
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
gl.destroy()
parentPort.postMessage(pixels);
}