Skip to content

Commit e73c84b

Browse files
committed
⚡ calculate in web worker to fast up 2d drawings
1 parent 8c046a1 commit e73c84b

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

index.html

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ <h2 class="page-title">Visual<br />Noises</h2>
101101
<!-- Utils -->
102102
<script src="utils/math.js"></script>
103103
<script src="utils/draw.js"></script>
104+
<script src="utils/worker.js"></script>
104105

105106
<!-- Procedural Generation Methods -->
106107
<script src="methods/value-noise-1d.js"></script>

utils/draw.js

+18
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,22 @@ function rect(x, y, w, h, options) {
8282
context.rect(x, y, w, h);
8383
context.closePath();
8484
fill ? context.fill() : context.stroke();
85+
};
86+
87+
/**
88+
* Draw text.
89+
*/
90+
const text = (x, y, text, options = {}) => {
91+
const {
92+
color = '#fff',
93+
size = 14,
94+
align = 'center',
95+
baseline = 'bottom',
96+
} = options;
97+
98+
context.font = `${size}px sans-serif`;
99+
context.textBaseline = baseline;
100+
context.textAlign = align;
101+
context.fillStyle = color;
102+
context.fillText(text, x, y);
85103
};

utils/worker.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function run(fn, globals, done) {
2+
const worker = new Worker(URL.createObjectURL(new Blob([`
3+
// Utils
4+
const max = Math.max;
5+
const min = Math.min;
6+
const ceil = Math.ceil;
7+
const floor = Math.floor;
8+
const cos = Math.cos;
9+
const pow = Math.pow;
10+
const LCGMultiplier = ${LCGMultiplier};
11+
const LCGIncrement = ${LCGIncrement};
12+
const LCGModulus = ${LCGModulus};
13+
let LCGSeed = ${LCGSeed};
14+
${setRandomSeed};
15+
${srandInt};
16+
${srand};
17+
${srandRange};
18+
${fadeFilter};
19+
${cosFilter};
20+
${smoothStepFilter};
21+
${vectorize};
22+
${multiply};
23+
${sum};
24+
${lerp};
25+
${bilerp};
26+
${map};
27+
${constrain};
28+
29+
// Globals
30+
let speed = ${speed};
31+
let offset = ${offset};
32+
let amplitude = ${amplitude};
33+
let frequency = ${frequency};
34+
35+
// Value Noise 1D
36+
const VALUE_NOISE_1D_DEFAULT_MAX_VERTICES = ${VALUE_NOISE_1D_DEFAULT_MAX_VERTICES};
37+
const ValueNoise1DRandomsBySeed = ${JSON.stringify(ValueNoise1DRandomsBySeed)};
38+
${ValueNoise1D};
39+
40+
// Value Noise 2D
41+
const VALUE_NOISE_2D_DEFAULT_MAX_VERTICES = ${VALUE_NOISE_2D_DEFAULT_MAX_VERTICES};
42+
const ValueNoise2DRandomsBySeed = ${JSON.stringify(ValueNoise2DRandomsBySeed)};
43+
${ValueNoise2D};
44+
45+
// State
46+
const visualisationFn = ${getCurrentState().visualisationFn};
47+
const methodFn = ${getCurrentState().methodFn};
48+
const filterFn = ${getCurrentState().filterFn};
49+
50+
// Supplied Globals
51+
${Object.keys(globals).map(name => `const ${name} = ${globals[name]};`).join('\n')}
52+
53+
// Run fn.
54+
postMessage((${fn})());
55+
`])));
56+
57+
worker.onmessage = ({data}) => done(data);
58+
59+
return worker;
60+
}

visualisations/2d-static-image.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,31 @@ function draw2DStaticImage({
88
height -= 100;
99
width -= 200;
1010

11-
for (let y = 0; y < height; y++) {
12-
for (let x = 0; x < width; x++) {
13-
const color = map(0, 1, 0, 255, getValueFn(x/amplitude, y/amplitude)[0] / amplitude);
11+
text(offsetX/2 + screenWidth/2, screenHeight/2, 'Calculating...', {color: '#666'});
1412

15-
const xPos = offsetX + x + 100;
16-
const yPos = offsetY + height + 100 - y;
13+
const calculate = () => {
14+
const result = [];
1715

18-
line(xPos, yPos, xPos, yPos - 1, {color: `rgb(${color}, ${color}, ${color})`});
16+
for (let y = 0; y < height; y++) {
17+
for (let x = 0; x < width; x++) {
18+
const color = map(0, 1, 0, 255, getValueFn(x/amplitude, y/amplitude)[0] / amplitude);
19+
20+
const xPos = offsetX + x + 100;
21+
const yPos = offsetY + height + 100 - y;
22+
23+
result.push({xPos, yPos, color});
24+
}
1925
}
26+
27+
return result;
2028
}
29+
30+
run(calculate, {getValueFn, height, width, offsetX, offsetY}, result => {
31+
context.clearRect(0, 0, screenWidth, screenHeight);
32+
33+
result.forEach(({xPos, yPos, color}) => {
34+
context.fillStyle = `rgb(${color}, ${color}, ${color})`;
35+
context.fillRect(xPos, yPos, 1, 1);
36+
});
37+
});
2138
}

0 commit comments

Comments
 (0)