-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageParticles.js
424 lines (348 loc) · 12.8 KB
/
imageParticles.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
To do:
Fix big where touch interaction can break or become unsynced with screen position (usually after scrolling the page)
Investigate ways to add slight randomness to the animation physics
*/
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d', {willReadFrequently: true});
const fileInput = document.getElementById('fileInput');
const resetButton = document.getElementById('resetButton');
const particleCountDisplay = document.getElementById('particleCount');
let particles;
let particleInitialPositions;
let particleColors;
let width;
let height;
let particleCount;
let mouseX = -1, mouseY = -1;
let mouseRadius = 100;
let repelForce = 4;
let healingFactor = 30;
let animationId;
let currentImage = null;
const canvasMetrics = {};
let frameCounter = 0;
const MIN_DIMENSION = 500;
const MAX_DIMENSION = 1000;
//add gui
var obj = {
particleDensity: 50,
effectRadius: 100,
forceStrength: 4,
healingFactor: 30,
};
var gui = new dat.gui.GUI( { autoPlace: false } );
gui.close();
var guiOpenToggle = false;
gui.add(obj, "particleDensity").min(10).max(100).step(1).name('Particle Density').onFinishChange(resetCanvas);
gui.add(obj, "effectRadius").min(20).max(400).step(1).name('Effect Radius');
gui.add(obj, "forceStrength").min(1).max(20).step(1).name('Force Strength');
gui.add(obj, "healingFactor").min(1).max(100).step(1).name('Self-Healing Speed');
obj['uploadImage'] = function () {
fileInput.click();
};
gui.add(obj, 'uploadImage').name('Upload Image');
obj['resetCanvas'] = function () {
resetCanvas();
};
gui.add(obj, 'resetCanvas').name("Reset Canvas (r)");
obj['saveImage'] = function () {
saveImage();
};
gui.add(obj, 'saveImage').name("Save Image (s)");
obj['saveVideo'] = function () {
toggleVideoRecord();
};
gui.add(obj, 'saveVideo').name("Video Export (v)");
customContainer = document.getElementById( 'gui' );
customContainer.appendChild(gui.domElement);
function processImage(image, resetState = true) {
if (resetState) {
currentImage = image;
}
if (animationId) {
cancelAnimationFrame(animationId);
}
// Resize image if necessary
const resizedCanvas = resizeImage(image);
width = canvas.width = resizedCanvas.width;
height = canvas.height = resizedCanvas.height;
console.log("Canvas w/h dimensions: "+width+", "+height);
ctx.drawImage(resizedCanvas, 0, 0);
const imageData = ctx.getImageData(0, 0, width, height);
const pixels = new Uint32Array(imageData.data.buffer);
particleCount = Math.floor((width * height * obj.particleDensity/100)) * 1.5;
document.querySelector("#particleCountCell").textContent = "# Particles: "+Number(particleCount).toLocaleString();
particles = new Float32Array(particleCount * 4);
particleInitialPositions = new Float32Array(particleCount * 2);
particleColors = new Uint32Array(particleCount);
//Place initial particles randomly on the canvas, with color based on source image
for (let i = 0; i < particleCount; i++) {
const idx = i * 4;
const posIdx = i * 2;
const x = Math.random() * width;
const y = Math.random() * height;
particles[idx] = x;
particles[idx + 1] = y;
particles[idx + 2] = 0;
particles[idx + 3] = 0;
particleInitialPositions[posIdx] = x;
particleInitialPositions[posIdx + 1] = y;
const pixelX = Math.min(Math.floor(x), width - 1);
const pixelY = Math.min(Math.floor(y), height - 1);
const pixelIndex = pixelY * width + pixelX;
particleColors[i] = pixels[pixelIndex];
}
calculateCanvasMetrics();
animate();
}
//Repulsion forcefield effect with self-healing
function animate() {
mouseRadius = obj.effectRadius + (Math.sin(frameCounter/10)/2 * obj.effectRadius);
repelForce = obj.forceStrength;
healingFactor = obj.healingFactor / 25000;
const radiusSq = mouseRadius * mouseRadius;
const imageData = ctx.createImageData(width, height);
const data = new Uint32Array(imageData.data.buffer);
data.fill(0);
for (let i = 0; i < particleCount; i++) {
const idx = i * 4;
const posIdx = i * 2;
const px = particles[idx];
const py = particles[idx + 1];
const initialX = particleInitialPositions[posIdx];
const initialY = particleInitialPositions[posIdx + 1];
if (mouseX >= 0 && mouseY >= 0) {
const dx = px - mouseX;
const dy = py - mouseY;
const distanceSq = dx * dx + dy * dy;
if (distanceSq < radiusSq && distanceSq > 0) {
const distance = distanceSq ** 0.5;
const effectStrength = (1 - distance / mouseRadius) * repelForce;
particles[idx + 2] += (dx / distance) * effectStrength;
particles[idx + 3] += (dy / distance) * effectStrength;
}
}
particles[idx + 2] += (initialX - px) * healingFactor;
particles[idx + 3] += (initialY - py) * healingFactor;
particles[idx] += particles[idx + 2];
particles[idx + 1] += particles[idx + 3];
particles[idx + 2] *= 0.95;
particles[idx + 3] *= 0.95;
if (particles[idx] < 0) {
particles[idx] = 0;
particles[idx + 2] *= -0.5;
} else if (particles[idx] >= width) {
particles[idx] = width - 1;
particles[idx + 2] *= -0.5;
}
if (particles[idx + 1] < 0) {
particles[idx + 1] = 0;
particles[idx + 3] *= -0.5;
} else if (particles[idx + 1] >= height) {
particles[idx + 1] = height - 1;
particles[idx + 3] *= -0.5;
}
const x = Math.round(particles[idx]);
const y = Math.round(particles[idx + 1]);
const pixelIndex = y * width + x;
data[pixelIndex] = particleColors[i];
}
ctx.putImageData(imageData, 0, 0);
frameCounter++;
animationId = requestAnimationFrame(animate);
}
//HELPER FUNCTIONS
// Info screen functions
function closeInfoScreen() {
const infoScreen = document.getElementById('infoScreen');
infoScreen.style.opacity = '0.0';
infoScreen.style.transition = 'opacity 0.3s ease-out';
setTimeout(() => {
infoScreen.style.display = 'none';
}, 300);
}
function resetCanvas(){
if (currentImage) {
processImage(currentImage);
}
}
function resizeImage(image) {
const tempCanvas = document.createElement('canvas');
// Calculate aspect ratio
const aspectRatio = image.width / image.height;
// Initialize new dimensions
let newWidth = image.width;
let newHeight = image.height;
// First, handle minimum dimensions
if (newWidth < MIN_DIMENSION || newHeight < MIN_DIMENSION) {
if (aspectRatio > 1) {
// Image is wider than tall
if (newHeight < MIN_DIMENSION) {
newHeight = MIN_DIMENSION;
newWidth = Math.round(MIN_DIMENSION * aspectRatio);
}
} else {
// Image is taller than wide
if (newWidth < MIN_DIMENSION) {
newWidth = MIN_DIMENSION;
newHeight = Math.round(MIN_DIMENSION / aspectRatio);
}
}
}
// Then, handle maximum dimensions
if (newWidth > MAX_DIMENSION || newHeight > MAX_DIMENSION) {
if (aspectRatio > 1) {
// Image is wider than tall
if (newWidth > MAX_DIMENSION) {
newWidth = MAX_DIMENSION;
newHeight = Math.round(MAX_DIMENSION / aspectRatio);
}
} else {
// Image is taller than wide
if (newHeight > MAX_DIMENSION) {
newHeight = MAX_DIMENSION;
newWidth = Math.round(MAX_DIMENSION * aspectRatio);
}
}
}
// Set canvas dimensions to our calculated values
tempCanvas.width = newWidth;
tempCanvas.height = newHeight;
// Draw resized image
const tempCtx = tempCanvas.getContext('2d');
tempCtx.drawImage(image, 0, 0, newWidth, newHeight);
console.log(`Original dimensions: ${image.width}x${image.height}`);
console.log(`Resized dimensions: ${newWidth}x${newHeight}`);
document.querySelector("#canvasSizeCell").textContent = "Width / Height: "+newWidth+" x "+newHeight+" px";
return tempCanvas;
}
function calculateCanvasMetrics() {
const rect = canvas.getBoundingClientRect();
const aspectRatioCanvas = width / height;
const aspectRatioRect = rect.width / rect.height;
const isCanvasWider = aspectRatioCanvas > aspectRatioRect;
const displayedWidth = isCanvasWider ? rect.width : rect.height * aspectRatioCanvas;
const displayedHeight = isCanvasWider ? rect.width / aspectRatioCanvas : rect.height;
canvasMetrics.offsetX = (rect.width - displayedWidth) / 2 + rect.left;
canvasMetrics.offsetY = (rect.height - displayedHeight) / 2 + rect.top;
canvasMetrics.scaleX = width / displayedWidth;
canvasMetrics.scaleY = height / displayedHeight;
}
//Functions to handle mouse and mobile touch interactions
function updateMousePosition(e) {
const { offsetX, offsetY, scaleX, scaleY } = canvasMetrics;
mouseX = (e.clientX - offsetX) * scaleX;
mouseY = (e.clientY - offsetY) * scaleY;
}
function updateInteractionPosition(clientX, clientY) {
const { offsetX, offsetY, scaleX, scaleY } = canvasMetrics;
mouseX = (clientX - offsetX) * scaleX;
mouseY = (clientY - offsetY) * scaleY;
}
function handleStart(event) {
event.preventDefault();
const touch = event.type === 'touchstart' ? event.touches[0] : event;
updateInteractionPosition(touch.clientX, touch.clientY);
}
function handleMove(event) {
event.preventDefault();
const touch = event.type === 'touchmove' ? event.touches[0] : event;
updateInteractionPosition(touch.clientX, touch.clientY);
}
function handleEnd(event) {
event.preventDefault();
mouseX = -1;
mouseY = -1;
}
// Event Listeners
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const image = new Image();
image.onload = () => processImage(image);
image.src = e.target.result;
};
reader.readAsDataURL(file);
}
});
window.addEventListener('resize', calculateCanvasMetrics);
// canvas.addEventListener('mousemove', updateMousePosition);
// canvas.addEventListener('mouseleave', () => {
// mouseX = -1;
// mouseY = -1;
// });
// // Remove previous mouse event listeners and add unified interaction handlers
// canvas.removeEventListener('mousemove', updateMousePosition);
// canvas.removeEventListener('mouseleave', () => {
// mouseX = -1;
// mouseY = -1;
// });
// Add unified mouse and touch event listeners
canvas.addEventListener('mousedown', handleStart, { passive: false });
canvas.addEventListener('mousemove', handleMove, { passive: false });
canvas.addEventListener('mouseup', handleEnd, { passive: false });
canvas.addEventListener('mouseleave', handleEnd, { passive: false });
// Add touch event listeners
canvas.addEventListener('touchstart', handleStart, { passive: false });
canvas.addEventListener('touchmove', handleMove, { passive: false });
canvas.addEventListener('touchend', handleEnd, { passive: false });
canvas.addEventListener('touchcancel', handleEnd, { passive: false });
function loadDefaultImage() {
const defaultImage = new Image();
defaultImage.onload = () => processImage(defaultImage);
defaultImage.onerror = () => {
console.error('Error loading default image. Please upload your own image.');
createFallbackImage();
};
let random = Math.random()
if(random<0.33){
defaultImage.src = 'images/waterRipple.png';
} else if(random<0.67){
defaultImage.src = 'images/moons.png';
} else {
defaultImage.src = 'images/lavaLamp.png';
}
}
// Optional fallback function in case the default image fails to load
function createFallbackImage() {
const tempCanvas = document.createElement('canvas');
tempCanvas.width = 500; // Default size if no image loads
tempCanvas.height = 500;
const tempCtx = tempCanvas.getContext('2d');
// Create a gradient
const gradient = tempCtx.createLinearGradient(0, 0, 500, 500);
gradient.addColorStop(0, '#f5c851');
gradient.addColorStop(0.5, '#63a48e');
gradient.addColorStop(1, '#0e7395');
tempCtx.fillStyle = gradient;
tempCtx.fillRect(0, 0, 500, 500);
// Use this as our source image
processImage(tempCanvas);
}
function toggleGUI(){
if(guiOpenToggle == false){
gui.open();
guiOpenToggle = true;
} else {
gui.close();
guiOpenToggle = false;
}
}
//shortcut hotkey presses
document.addEventListener('keydown', function(event) {
if (event.key === 'r') {
resetCanvas();
} else if (event.key === 's') {
saveImage();
} else if (event.key === 'v') {
toggleVideoRecord();
} else if (event.key === 'o') {
toggleGUI();
}
});
//SCRIPT STARTUP
// Load default image on startup
loadDefaultImage();