-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
128 lines (114 loc) · 3.63 KB
/
sketch.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
// jshint esversion: 10
const $ = query => document.querySelector(query);
const c = $('canvas');
const ctx = c.getContext('2d');
let list = [];
c.width = c.offsetWidth;
c.height = c.offsetHeight;
c.width *= 2;
c.height *= 2;
let imgdata = ctx.getImageData(0, 0, c.width, c.height);
const mouse = {
state: 'idle',
states: {
idle: {
move() {},
down(e) {
mouse.state = 'drawing';
mouse.states[mouse.state].move(e)
},
up() {}
},
drawing: {
move(e) {
let x = 0|(c.width*e.layerX/innerWidth);
let y = 0|(c.height*e.layerY/innerHeight);
const i = 4 * (y * c.width + x);
if (imgdata.data[i+3] == 0) {
imgdata.data[i+0] = 0|(Math.random()*256);
imgdata.data[i+1] = 0|(Math.random()*256);
imgdata.data[i+2] = 0|(Math.random()*256);
// imgdata.data[i+0] = 128;
// imgdata.data[i+1] = 128;
// imgdata.data[i+2] = 128;
imgdata.data[i+3] = 255;
ctx.putImageData(imgdata, 0, 0);
list.push( i );
}
},
down() {},
up() {
mouse.state = 'idle';
}
}
}
}
c.addEventListener('mousedown', e => mouse.states[mouse.state].down(e));
c.addEventListener('mousemove', e => mouse.states[mouse.state].move(e));
c.addEventListener('mouseup', e => mouse.states[mouse.state].up(e));
// function random_bm() {
// let u = 0, v = 0;
// while (u == 0) u = Math.random();
// while (v == 0) v = Math.random();
// const num = 0.1*Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v) + 0.5;
// if (num > 1 || num < 0) return random_bm();
// return num*2-1;
// }
function random_bm() {
return 2*Math.random() - 1
}
(async () => {
const deltaColour = 1;
let last = performance.now();
while (true) {
if (list.length == 0) {
ctx.putImageData(imgdata, 0, 0);
await new Promise(requestAnimationFrame);
continue;
}
let index = 0|(Math.random()*list.length);
// index = 0|(9999*list.length/10000);
// index = 0;
const i = list[index];
const [x, y] = [(i/4)%c.width, 0|((i/4)/c.width)];
let [r, g, b] = [imgdata.data[i], imgdata.data[i+1], imgdata.data[i+2]];
let i_ = 4 * ((y-1) * c.width + x);
if (y-1 >= 0 && imgdata.data[i_+3] == 0) {
imgdata.data[i_+0] = r + deltaColour*random_bm();
imgdata.data[i_+1] = g + deltaColour*random_bm();
imgdata.data[i_+2] = b + deltaColour*random_bm();
imgdata.data[i_+3] = 255;
list.push( i_ );
}
i_ = 4 * (y * c.width + x-1);
if (x-1 >= 0 && imgdata.data[i_+3] == 0) {
imgdata.data[i_+0] = r + deltaColour*random_bm();
imgdata.data[i_+1] = g + deltaColour*random_bm();
imgdata.data[i_+2] = b + deltaColour*random_bm();
imgdata.data[i_+3] = 255;
list.push( i_ );
}
i_ = 4 * ((y+1) * c.width + x);
if (y+1 < c.height && imgdata.data[i_+3] == 0) {
imgdata.data[i_+0] = r + deltaColour*random_bm();
imgdata.data[i_+1] = g + deltaColour*random_bm();
imgdata.data[i_+2] = b + deltaColour*random_bm();
imgdata.data[i_+3] = 255;
list.push( i_ );
}
i_ = 4 * (y * c.width + x+1);
if (x+1 < c.width && imgdata.data[i_+3] == 0) {
imgdata.data[i_+0] = r + deltaColour*random_bm();
imgdata.data[i_+1] = g + deltaColour*random_bm();
imgdata.data[i_+2] = b + deltaColour*random_bm();
imgdata.data[i_+3] = 255;
list.push( i_ );
}
list.splice(index, 1);
if (performance.now() - last > (1000 >> 6)) {
ctx.putImageData(imgdata, 0, 0);
await new Promise(requestAnimationFrame);
last = performance.now();
}
}
})()