-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
211 lines (186 loc) · 7.34 KB
/
index.html
File metadata and controls
211 lines (186 loc) · 7.34 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Particle Field</title>
</head>
<body>
<canvas></canvas>
<div>
<button onclick="toggle_trace()">toggle trace (t)</button>
<button onclick="clear_canvas()">clear (c)</button>
<button onclick="download()">save (s)</button>
</div>
<style>
body {
overflow: hidden;
margin: 0;
padding: 0;
}
canvas {
position: absolute;
width: 100vw;
height: 100vh;
}
canvas, body, html, * {
touch-action: none;
-webkit-user-select: none; /* Safari */
-ms-user-select: none; /* IE 10 and IE 11 */
user-select: none; /* Standard syntax */
}
div {
position: absolute;
top: 10px;
left: 10px;
}
</style>
<script>
console.clear();
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx.textAlign = "right";
ctx.textBaseline = 'top';
const observer = new ResizeObserver(resizeTheCanvasToDisplaySize)
observer.observe(canvas);
function resizeTheCanvasToDisplaySize(entries) {
const entry = entries[0];
let width;
let height;
if (entry.devicePixelContentBoxSize) {
width = entry.devicePixelContentBoxSize[0].inlineSize;
height = entry.devicePixelContentBoxSize[0].blockSize;
} else if (entry.contentBoxSize) {
// fallback for Safari that will not always be correct
width = Math.round(entry.contentBoxSize[0].inlineSize * devicePixelRatio);
height = Math.round(entry.contentBoxSize[0].blockSize * devicePixelRatio);
}
canvas.width = width;
canvas.height = height;
ctx.fillStyle = "#AFA";
shorter_side = Math.min(canvas.width, canvas.height);
view_ratio = shorter_side/(CANVAS_VIEW*2);
shorter_offset = Math.min(canvas.offsetWidth, canvas.offsetHeight);
view_ratio_offset = shorter_offset * CANVAS_VIEW * 2;
if (trace) {
ctx.globalAlpha = .01;
} else {
ctx.globalAlpha = 1;
}
}
let x_pos = [];
let y_pos = [];
let x_vel = [];
let y_vel = [];
let mouse_x = 0;
let mouse_y = 0;
let mouse_down = false;
canvas.addEventListener("contextmenu", e => {
e.preventDefault();
});
canvas.addEventListener("mousemove", e => {
mouse_x = (e.pageX - canvas.offsetWidth/2) / shorter_offset * CANVAS_VIEW * 2;
mouse_y = -(e.pageY - canvas.offsetHeight/2) / shorter_offset * CANVAS_VIEW * 2;
});
canvas.addEventListener("touchmove", e => {
mouse_x = (e.touches[0].clientX - canvas.offsetWidth/2) / shorter_offset * CANVAS_VIEW * 2;
mouse_y = -(e.touches[0].clientY - canvas.offsetHeight/2) / shorter_offset * CANVAS_VIEW * 2;
});
canvas.addEventListener("mousedown", e => {
mouse_x = (e.pageX - canvas.offsetWidth/2) / shorter_offset * CANVAS_VIEW * 2;
mouse_y = -(e.pageY - canvas.offsetHeight/2) / shorter_offset * CANVAS_VIEW * 2;
mouse_down = true;
});
canvas.addEventListener("mouseup", e => {
mouse_down = false;
e.preventDefault();
});
canvas.addEventListener("touchstart", e => {
mouse_x = (e.touches[0].clientX - canvas.offsetWidth/2) / shorter_offset * CANVAS_VIEW * 2;
mouse_y = -(e.touches[0].clientY - canvas.offsetHeight/2) / shorter_offset * CANVAS_VIEW * 2;
mouse_down = true;
});
canvas.addEventListener("touchend", e => {
mouse_down = false;
e.preventDefault();
});
const size = 100;
const create_scale = 1/size;
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
x_pos.push((x - size/2) * create_scale);
y_pos.push((y - size/2) * create_scale);
x_vel.push(0);
y_vel.push(0);
}
}
const CANVAS_VIEW = 10;
let shorter_side = Math.min(canvas.width, canvas.height);
let view_ratio = shorter_side/(CANVAS_VIEW*2);
let shorter_offset = Math.min(canvas.offsetWidth, canvas.offsetHeight);
function draw_point(x, y) {
ctx.fillRect(x*view_ratio + canvas.width/2, -y*view_ratio + canvas.height/2, 1, 1);
}
const delta_time = 0.021;
let last_time = new Date().getTime();
let delta_value = 0;
let trace = false;
var download = function() {
var link = document.createElement('a');
link.download = 'particle_image.png';
link.href = canvas.toDataURL();
link.click();
}
var clear_canvas = function() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.fillStyle = "#222";
ctx.globalAlpha = 1;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore();
}
var toggle_trace = function() {
if (trace) {
trace = false;
ctx.globalAlpha = 1;
} else {
clear_canvas();
trace = true;
ctx.globalAlpha = .01;
}
}
window.addEventListener("keydown", e => {
if (e.key == "s") download();
if (e.key == "c") clear_canvas();
if (e.key == "t") toggle_trace();
});
const RESITANCE = 0.995;
(function loop() {
window.requestAnimationFrame(loop);
if(!trace) clear_canvas();
delta_value += ((new Date().getTime() - last_time) - delta_value) * 0.08;
if(!trace) ctx.fillText(Math.floor(delta_value), canvas.width, 0);
last_time = new Date().getTime();
for (let i = 0; i < x_pos.length; i++) {
x_pos[i] += x_vel[i] * delta_time;
y_pos[i] += y_vel[i] * delta_time;
if(mouse_down) {
let delta_x = mouse_x - x_pos[i];
let delta_y = mouse_y - y_pos[i];
const mouse_distance = Math.hypot(delta_x, delta_y);
//const linear_attraction = 500 / (mouse_distance**2 + 0);
const linear_attraction = 500 / (mouse_distance + 10);
const attraction_over_dist = linear_attraction / mouse_distance;
delta_x *= attraction_over_dist;
delta_y *= attraction_over_dist;
x_vel[i] = (x_vel[i] + delta_x * delta_time) * RESITANCE;
y_vel[i] = (y_vel[i] + delta_y * delta_time) * RESITANCE;
}
draw_point(x_pos[i], y_pos[i]);
}
})();
</script>
</body>
</html>