-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathbench2.js
93 lines (63 loc) · 1.86 KB
/
bench2.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
import {
ColorRGBA,
WebglPlot,
WebglLinePlot,
WebglLine,
} from "../dist/webglplot.mjs";
const canvas = document.getElementById("my_canvas");
const devicePixelRatio = window.devicePixelRatio || 1;
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;
const numX = canvas.width;
console.log("numX", numX);
const wglp = new WebglPlot(canvas);
let lines = [];
let plotLine;
let newYData = [];
const createLines = (num) => {
lines = Array.from({ length: num }, () => new WebglLine());
lines.forEach((line) => {
line.setColor(new ColorRGBA(255, 255, 0, 1)); //color not working
line.lineSpaceX(numX);
});
plotLine = new WebglLinePlot(wglp, lines);
newYData = Array(lines[0].getSize()).fill(0);
}
createLines(1);
let frame = 0;
let prevTime = new Date();
function newFrame() {
wglp.clear();
for (let i = 0; i < lines.length; i++) {
const y0 = (i / lines.length) + (window.performance.now() * 0.0001)
for (let j = 0; j < numX; j++) {
const y = y0 + j * 0.1 / numX;
const yy = y - Math.floor(y);
newYData[j] = yy * 2 - 1;
}
lines[i].setYs(newYData);
plotLine.updateLine(i);
}
plotLine.draw();
const timeNow = new Date();
if (timeNow - prevTime > 1000) {
console.log(frame);
fpsElem.innerHTML = `Current fps: ${frame}`
frame = 0;
prevTime = timeNow;
} else {
frame++;
}
requestAnimationFrame(newFrame);
}
requestAnimationFrame(newFrame);
const fpsElem = document.getElementById("fps");
const btClick = document.getElementById("btClick");
const inNum = document.getElementById("inNum");
btClick.addEventListener("click", () => {
const numLine = inNum.value;
const timeStart = new Date();
createLines(numLine);
const timeEnd = new Date();
console.log(`Created ${numLine} in ${timeEnd - timeStart} milliseconds`);
});