-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixelSwirl.html
More file actions
1496 lines (1261 loc) · 62.9 KB
/
PixelSwirl.html
File metadata and controls
1496 lines (1261 loc) · 62.9 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pixel Swirl Version 1.015</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: stretch;
background-color: #222;
color: #eee;
margin: 0;
padding: 0;
height: 100vh;
width: 100vw;
overflow: hidden;
box-sizing: border-box;
}
.main-content-container {
display: flex;
flex-direction: row;
flex-grow: 1;
gap: 20px;
align-items: flex-start; /* Aligns canvas container to the top */
height: 100%;
padding: 10px;
box-sizing: border-box;
/* margin-right will be set by JS */
}
.canvas-container {
flex-grow: 1;
flex-shrink: 1;
display: flex;
align-items: center; /* Center canvas vertically if it doesn't fill */
justify-content: center; /* Center canvas horizontally if it doesn't fill */
overflow: hidden;
background-color: #000;
/* Width and height will be set by JS */
}
canvas {
border: 2px solid #555;
background-color: #000;
display: block;
object-fit: contain;
/* cursor: none; */ /* Removed this line to keep the cursor visible */
}
/* New style for the mouse-following gravity field visualizer */
.mouse-follower-ball {
position: absolute; /* Position relative to the canvas container */
background-color: white; /* White square */
border-radius: 50%; /* Make it round */
pointer-events: none; /* Allow mouse events to pass through */
transform: translate(-50%, -50%); /* Center the ball on the cursor */
z-index: 999; /* Ensure it's above the canvas */
display: none; /* Always hidden now */
opacity: 0.0; /* Make it completely transparent */
}
.right-panel {
display: flex;
flex-direction: column;
gap: 8px;
padding: 10px;
width: 280px; /* Fixed width */
min-width: 280px; /* Prevent shrinking below this */
flex-shrink: 0; /* Prevent the right panel from shrinking too much */
justify-content: flex-start;
height: 100%;
box-sizing: border-box;
background-color: #333;
border-radius: 8px;
position: fixed; /* Docking and Locking to the right */
right: 0;
top: 0;
height: 100vh;
z-index: 1000;
padding-right: 20px;
padding-left: 10px;
box-shadow: -5px 0 10px rgba(0,0,0,0.3);
}
.controls-row {
display: flex;
flex-direction: column;
gap: 4px;
width: 100%;
}
.control-group {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 5px;
white-space: nowrap;
width: 100%;
}
.slider-with-buttons {
display: flex;
flex-direction: row;
align-items: center;
gap: 5px;
width: 100%;
}
.slider-with-value {
display: flex;
flex-direction: row;
align-items: center;
gap: 8px;
width: 100%;
}
.control-group.piece-size-group {
flex-direction: row;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
}
.control-group.piece-size-group label,
.control-group.piece-size-group span {
flex-shrink: 0;
}
.control-group.piece-size-group .slider-with-buttons {
flex-basis: 100%;
}
input[type="range"] {
flex-grow: 1;
min-width: 80px;
}
input[type="number"] {
width: 100%;
padding: 5px;
border-radius: 3px;
border: 1px solid #555;
background-color: #333;
color: #eee;
font-size: 14px;
box-sizing: border-box;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
transition: background-color 0.3s ease;
width: 100%;
box-sizing: border-box;
}
button:hover {
background-color: #0056b3;
}
button#defaultsButton {
background-color: #dc3545;
}
button#defaultsButton:hover {
background-color: #c82333;
}
button#randomizeButton {
background-color: #ff00ff;
}
button#randomizeButton:hover {
background-color: #cc00cc;
}
input[type="file"] {
display: none;
}
.custom-file-upload {
border: 1px solid #ccc;
display: inline-block;
padding: 10px 20px;
cursor: pointer;
background-color: #28a745;
color: white;
border-radius: 5px;
transition: background-color 0.3s ease;
width: 100%;
box-sizing: border-box;
text-align: center;
}
.custom-file-upload:hover {
background-color: #218838;
}
.checkbox-group {
display: flex;
flex-direction: row;
align-items: center;
gap: 8px;
width: 100%;
}
.slider-button {
padding: 5px 10px;
font-size: 14px;
min-width: 30px;
width: auto;
flex-shrink: 0;
}
.app-info {
margin-top: auto;
text-align: left;
width: 100%;
padding-top: 15px;
border-top: 1px solid #555;
margin-top: 15px;
}
.app-info h2 {
margin-bottom: 5px;
margin-top: 0;
}
.app-info a {
color: #8ab4f8;
text-decoration: none;
}
.app-info a:hover {
text-decoration: underline;
}
.not-mobile {
font-style: italic;
font-size: 0.9em;
color: #aaa;
margin-top: 5px;
margin-bottom: 5px;
}
</style>
</head>
<body>
<div class="main-content-container">
<div class="canvas-container">
<canvas id="pixelCanvas"></canvas>
<!-- New HTML element for the mouse-following gravity field visualizer -->
<div id="mouseFollowerBall" class="mouse-follower-ball"></div>
</div>
</div>
<div class="right-panel">
<div class="controls-row">
<div class="control-group">
<label for="imageUpload" class="custom-file-upload">Load Image</label>
<input type="file" id="imageUpload" accept="image/*">
</div>
</div>
<div class="controls-row">
<button id="restartButton">Restart</button>
</div>
<div class="controls-row">
<div class="control-group">
<label for="maxPiecesInput">Max # of Pieces:</label>
<input type="number" id="maxPiecesInput" value="100000" min="4" step="1">
</div>
</div>
<div class="controls-row">
<div class="control-group">
<label for="speedSlider">Piece Momentum:</label>
<div class="slider-with-value">
<input type="range" id="speedSlider" min="0.1" max="5" value="1" step="0.1">
<span id="speedValue">1.0</span>
</div>
</div>
</div>
<div class="controls-row">
<div class="control-group piece-size-group">
<label>Puzzle Piece Size:</label>
<span id="pixelSizeValue">64x64</span>
<div class="slider-with-buttons">
<button id="pixelSizeMinus" class="slider-button">-</button>
<input type="range" id="pixelSizeSlider" min="0" max="8" value="6" step="1">
<button id="pixelSizePlus" class="slider-button">+</button>
</div>
</div>
</div>
<div class="controls-row">
<div class="control-group">
<label for="gravitySlider">Gravity:</label>
<div class="slider-with-value">
<input type="range" id="gravitySlider" min="0.01" max="0.5" value="0.05" step="0.001">
<span id="gravityValue">0.050</span>
</div>
</div>
</div>
<div class="controls-row">
<div class="control-group">
<label for="snapDistanceSlider">Snap Distance (pixels):</label>
<div class="slider-with-value">
<input type="range" id="snapDistanceSlider" min="0" max="16" value="0" step="1">
<span id="snapDistanceValue">0</span>
</div>
</div>
</div>
<div class="controls-row">
<div class="control-group">
<label for="repelSlider">Repel:</label>
<div class="slider-with-value">
<input type="range" id="repelSlider" min="0" max="1" value="0" step="0.01">
<span id="repelValue">0.00</span>
</div>
</div>
</div>
<div class="controls-row">
<div class="control-group">
<label for="frequencySlider">Frequency (seconds):</label>
<div class="slider-with-value">
<input type="range" id="frequencySlider" min="0" max="10" value="0" step="1">
<span id="frequencyValue">0</span>
</div>
</div>
</div>
<div class="controls-row">
<div class="checkbox-group">
<input type="checkbox" id="gravityBallCheckbox">
<label for="gravityBallCheckbox">Gravity Field (Mouse)</label>
</div>
</div>
<!-- New slider for Gravity Field Strength -->
<div class="controls-row" id="gravityFieldStrengthGroup">
<div class="control-group">
<label for="gravityFieldStrengthSlider">Gravity Field Strength:</label>
<div class="slider-with-value">
<input type="range" id="gravityFieldStrengthSlider" min="1" max="200" value="1" step="0.1">
<span id="gravityFieldStrengthValue">1.0</span>
</div>
</div>
</div>
<div class="controls-row">
<div class="checkbox-group">
<input type="checkbox" id="unlockFreedomCheckbox">
<label for="unlockFreedomCheckbox">Unlock more freedom</label>
</div>
</div>
<div class="controls-row">
<div class="checkbox-group">
<input type="checkbox" id="unlockPieceSizeCheckbox">
<label for="unlockPieceSizeCheckbox">Unlock Piece Size (lag)</label>
</div>
</div>
<div class="controls-row">
<button id="defaultsButton">Defaults</button>
</div>
<div class="controls-row">
<button id="randomizeButton">Randomize</button>
</div>
<div class="app-info">
<h2>Pixel Swirl</h2>
<span id="versionNumber">Version 1.015</span>
<span class="not-mobile">(Not for Mobile Use)</span>
<hr>
<br>
Check out other cool stuff:<br>
<br>
<a href="https://pirillo.com/arcade" target="_blank">https://pirillo.com/arcade</a><br>
<br>
<a href="https://github.com/ChrisPirillo" target="_blank">https://github.com/ChrisPirillo</a>
<hr>
Vibe Coded with:<br>
<a href="https://www.google.com/search?q=%22google+gemini%22" target="_blank">Gemini</a>
</div>
</div>
<script>
const canvas = document.getElementById('pixelCanvas');
const imageUpload = document.getElementById('imageUpload');
const restartButton = document.getElementById('restartButton');
const speedSlider = document.getElementById('speedSlider');
const speedValueSpan = document.getElementById('speedValue');
const pixelSizeSlider = document.getElementById('pixelSizeSlider');
const pixelSizeMinusBtn = document.getElementById('pixelSizeMinus');
const pixelSizePlusBtn = document.getElementById('pixelSizePlus');
const pixelSizeValueSpan = document.getElementById('pixelSizeValue');
const gravitySlider = document.getElementById('gravitySlider');
const gravityValueSpan = document.getElementById('gravityValue');
const snapDistanceSlider = document.getElementById('snapDistanceSlider');
const snapDistanceValueSpan = document.getElementById('snapDistanceValue');
const repelSlider = document.getElementById('repelSlider');
const repelValueSpan = document.getElementById('repelValue');
const frequencySlider = document.getElementById('frequencySlider');
const frequencyValueSpan = document.getElementById('frequencyValue');
const unlockPieceSizeCheckbox = document.getElementById('unlockPieceSizeCheckbox');
const unlockFreedomCheckbox = document.getElementById('unlockFreedomCheckbox');
const versionNumberSpan = document.getElementById('versionNumber');
const maxPiecesInput = document.getElementById('maxPiecesInput');
const defaultsButton = document.getElementById('defaultsButton');
const randomizeButton = document.getElementById('randomizeButton');
const gravityBallCheckbox = document.getElementById('gravityBallCheckbox');
const rightPanel = document.querySelector('.right-panel');
const mainContentContainer = document.querySelector('.main-content-container');
const canvasContainer = document.querySelector('.canvas-container');
const mouseFollowerBall = document.getElementById('mouseFollowerBall'); // Reference to the HTML element for the gravity field visualizer
const gravityFieldStrengthSlider = document.getElementById('gravityFieldStrengthSlider');
const gravityFieldStrengthValueSpan = document.getElementById('gravityFieldStrengthValue');
const gravityFieldStrengthGroup = document.getElementById('gravityFieldStrengthGroup');
const PUZZLE_PIECE_SIZES = [1, 2, 3, 4, 5, 6, 7, 8, 16, 32, 64, 128, 256, 512];
let TARGET_MAX_PIECES = 100000;
// Default ranges for sliders (before unlock freedom)
const DEFAULT_SPEED_MIN = 0.1;
const DEFAULT_SPEED_MAX = 5;
const DEFAULT_GRAVITY_MIN = 0.01;
const DEFAULT_GRAVITY_MAX = 0.5;
const DEFAULT_SNAP_MIN = 0;
const DEFAULT_SNAP_MAX = 16;
const DEFAULT_REPEL_MIN = 0;
const DEFAULT_REPEL_MAX = 1;
const DEFAULT_FREQUENCY_MIN = 0;
const DEFAULT_FREQUENCY_MAX = 10;
const DEFAULT_PIECE_SIZE_INDEX = PUZZLE_PIECE_SIZES.indexOf(16);
const DEFAULT_GRAVITY_FIELD_STRENGTH_MIN = 1;
const DEFAULT_GRAVITY_FIELD_STRENGTH_MAX = 200;
// Unlocked ranges for sliders
const UNLOCKED_SPEED_MIN = 0.1;
const UNLOCKED_SPEED_MAX = 10;
const UNLOCKED_GRAVITY_MIN = 0.001;
const UNLOCKED_GRAVITY_MAX = 0.99;
const UNLOCKED_SNAP_MIN = 0;
const UNLOCKED_SNAP_MAX = 256;
const UNLOCKED_REPEL_MIN = 0;
const UNLOCKED_REPEL_MAX = 5;
const UNLOCKED_FREQUENCY_MIN = 0;
const UNLOCKED_FREQUENCY_MAX = 10;
const UNLOCKED_GRAVITY_FIELD_STRENGTH_MIN = 1;
const UNLOCKED_GRAVITY_FIELD_STRENGTH_MAX = 2000; // Allow even stronger for "unlocked freedom"
let gl = null;
let program = null;
let positionBuffer = null;
let texCoordBuffer = null;
let numVertices = 0;
let animationFrameId = null;
let originalImageObject = null;
let currentPuzzlePieceSize = PUZZLE_PIECE_SIZES[10];
let animationSpeed = 1;
let gravityForce = 0.05;
let snapDistance = 0;
let repelForce = 0;
let repelFrequency = 0;
// Gravity field variables
let gravityBallEnabled = false;
let gravityBallX = 0; // Will be mouseX
let gravityBallY = 0; // Will be mouseY
let gravityBallRadius = 0; // Calculated dynamically (size of the *effect* area)
const BASE_GRAVITY_FIELD_FORCE = 0.5; // Base force
let gravityFieldStrength = 1.0; // Multiplier for the base force
// WebGL resources for gravity field visualizer (no longer used for drawing the ball itself)
let gravityBallTexture = null;
let gravityBallPositionBuffer = null;
let gravityBallTexCoordBuffer = null;
let puzzlePieces = [];
let actualCanvasWidth = 0; // Actual width of the WebGL canvas
let actualCanvasHeight = 0; // Actual height of the WebGL canvas
let currentMinPieceSizeIndex = 0;
let mainAtlasTexture = null;
let allPositions = null;
let allTexCoords = null;
let lastSetPuzzlePieceSizeIndex = PUZZLE_PIECE_SIZES.indexOf(64);
let lastRepelTriggerCheckTime = 0;
let repelTriggeredThisFrame = false;
// --- Shaders ---
const vsSource = `
attribute vec2 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
uniform mat4 u_projectionMatrix;
void main() {
gl_Position = u_projectionMatrix * vec4(a_position, 0.0, 1.0);
v_texCoord = a_texCoord;
}
`;
const fsSource = `
precision mediump float;
varying vec2 v_texCoord;
uniform sampler2D u_sampler;
void main() {
gl_FragColor = texture2D(u_sampler, v_texCoord);
}
`;
// --- WebGL Initialization ---
function initWebGL() {
gl = canvas.getContext('webgl');
if (!gl) {
console.error('Unable to initialize WebGL. Your browser or machine may not support it.');
console.error('WebGL is not supported in your browser. This simulator requires WebGL.');
return false;
}
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
program = initShaderProgram(gl, vsSource, fsSource);
if (!program) return false;
gl.useProgram(program);
positionBuffer = gl.createBuffer();
texCoordBuffer = gl.createBuffer();
// WebGL resources for gravity field visualizer are no longer needed for drawing the ball
// but the variables are kept for consistency if future WebGL features need them.
gravityBallTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, gravityBallTexture);
const whitePixel = new Uint8Array([255, 255, 255, 255]); // RGBA white pixel
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, whitePixel);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
return true;
}
function initShaderProgram(gl, vsSource, fsSource) {
const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource);
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
console.error('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram));
console.error('Vertex Shader Info:', gl.getShaderInfoLog(vertexShader));
console.error('Fragment Shader Info:', gl.getShaderInfoLog(fragmentShader));
gl.deleteProgram(shaderProgram);
return null;
}
return shaderProgram;
}
function loadShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
function createProjectionMatrix(width, height) {
return new Float32Array([
2 / width, 0, 0, 0,
0, -2 / height, 0, 0,
0, 0, 1, 0,
-1, 1, 0, 1
]);
}
// --- Core Logic for Particles (Puzzle Pieces) ---
class PuzzlePiece {
constructor(startX, startY, targetX, targetY, pieceDimension, texU0, texV0, texU1, texV1) {
this.x = startX;
this.y = startY;
this.targetX = targetX;
this.targetY = targetY;
this.dimension = pieceDimension; // This is the scaled dimension for rendering
this.texCoords = [
texU0, texV0, // Top-Left
texU1, texV0, // Top-Right
texU0, texV1, // Bottom-Left
texU0, texV1, // Bottom-Left
texU1, texV0, // Top-Right
texU1, texV1 // Bottom-Right
];
this.vx = 0;
this.vy = 0;
this.settled = false;
this.maxSpeed = 10 * animationSpeed;
this.damping = 0.95;
this.swirlRadius = Math.random() * 200 + 50;
this.swirlAngle = Math.random() * Math.PI * 2;
this.swirlSpeed = Math.random() * 0.05 + 0.01;
}
update(deltaTime) {
// The gravity field force will apply even if settled, so we remove this check here.
// However, the main "gravity" and "swirl" forces still respect the settled state.
if (this.settled) {
// If settled, only allow external forces (like gravity field) to move it.
// The internal swirl/target-seeking logic is skipped.
this.vx *= this.damping;
this.vy *= this.damping;
this.x += this.vx;
this.y += this.vy;
// If it moves too far from its target due to external forces,
// it should become unsettled again to return to its target.
const dx = this.targetX - this.x;
const dy = this.targetY - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance > snapDistance + 1) { // A small threshold to prevent constant toggling
this.settled = false;
}
return;
}
const dx = this.targetX - this.x;
const dy = this.targetY - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
const normalizedDistance = Math.min(1, distance / 5.0 / (this.swirlRadius > 0 ? this.swirlRadius : 1));
const swirlDampingFactor = 0.99 - (gravityForce * 0.7 * (1 - normalizedDistance));
this.swirlRadius *= Math.max(0.7, swirlDampingFactor);
if (distance <= snapDistance + 0.01) {
this.x = this.targetX;
this.y = this.targetY;
this.vx = 0;
this.vy = 0;
this.settled = true;
this.swirlRadius = 0;
return;
}
this.swirlAngle += this.swirlSpeed * animationSpeed;
const swirlOffsetX = this.swirlRadius * Math.cos(this.swirlAngle);
const swirlOffsetY = this.swirlRadius * Math.sin(this.swirlAngle);
const swirlTargetX = this.targetX + swirlOffsetX;
const swirlTargetY = this.targetY + swirlOffsetY;
const swirlDx = swirlTargetX - this.x;
const swirlDy = swirlTargetY - this.y;
let effectiveForce = gravityForce * animationSpeed;
if (repelForce > 0 && repelFrequency > 0 && repelTriggeredThisFrame) {
effectiveForce = -repelForce;
}
this.vx += swirlDx * effectiveForce;
this.vy += swirlDy * effectiveForce;
this.vx *= this.damping;
this.vy *= this.damping;
const currentSpeed = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
if (currentSpeed > this.maxSpeed) {
const scale = this.maxSpeed / currentSpeed;
this.vx *= scale;
this.vy *= scale;
}
this.x += this.vx;
this.y += this.vy;
if (distance < 1 && this.swirlRadius < 5 && Math.abs(this.vx) < 0.1 && Math.abs(this.vy) < 0.1) {
this.x = this.targetX;
this.y = this.targetY;
this.settled = true;
this.vx = 0;
this.vy = 0;
this.swirlRadius = 0;
}
}
}
// Helper to find next power of 2
function nextPowerOf2(n) {
if (n === 0) return 1;
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
// --- Setup and Rendering ---
// Function to dynamically size the canvas container and the canvas
function resizeCanvasContainerAndCanvas() {
const rightPanelWidth = rightPanel.offsetWidth +
parseFloat(window.getComputedStyle(rightPanel).paddingLeft) +
parseFloat(window.getComputedStyle(rightPanel).paddingRight);
const mainContentPadding = parseFloat(window.getComputedStyle(mainContentContainer).paddingLeft) +
parseFloat(window.getComputedStyle(mainContentContainer).paddingRight);
// Calculate available width for the canvas container
const availableWidth = window.innerWidth - rightPanelWidth - mainContentPadding;
const availableHeight = window.innerHeight - (parseFloat(window.getComputedStyle(mainContentContainer).paddingTop) +
parseFloat(window.getComputedStyle(mainContentContainer).paddingBottom));
// Set the canvas container's dimensions
canvasContainer.style.width = `${availableWidth}px`;
canvasContainer.style.height = `${availableHeight}px`;
// Update main content container's margin to account for fixed right panel
mainContentContainer.style.marginRight = `${rightPanelWidth + mainContentPadding}px`;
// If an image is loaded, resize the canvas to fit the image aspect ratio within the new container size
if (originalImageObject) {
resizeCanvasToImage(originalImageObject);
} else {
// If no image is loaded, make the canvas fill the container
canvas.width = availableWidth;
canvas.height = availableHeight;
actualCanvasWidth = canvas.width;
actualCanvasHeight = canvas.height;
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
const projectionMatrix = createProjectionMatrix(gl.canvas.width, gl.canvas.height);
const uProjectionMatrixLoc = gl.getUniformLocation(program, 'u_projectionMatrix');
gl.uniformMatrix4fv(uProjectionMatrixLoc, false, projectionMatrix);
}
// Update gravity ball radius if enabled
if (gravityBallEnabled) {
gravityBallRadius = Math.min(actualCanvasWidth, actualCanvasHeight) * 0.25; // 25% of smaller dimension for radius
// Update the HTML element's size (though it's invisible now)
mouseFollowerBall.style.width = `${gravityBallRadius * 2}px`;
mouseFollowerBall.style.height = `${gravityBallRadius * 2}px`;
}
}
function resizeCanvasToImage(img) {
const containerWidth = canvasContainer.clientWidth;
const containerHeight = canvasContainer.clientHeight;
let targetWidth = 0;
let targetHeight = 0;
const imageAspectRatio = img.width / img.height;
const containerAspectRatio = containerWidth / containerHeight;
if (imageAspectRatio > containerAspectRatio) {
// Image is wider than container, fit by container width
targetWidth = containerWidth;
targetHeight = containerWidth / imageAspectRatio;
} else {
// Image is taller or same aspect ratio as container, fit by container height
targetHeight = containerHeight;
targetWidth = containerHeight * imageAspectRatio;
}
// Set canvas dimensions to fit the calculated target size, preserving aspect ratio
canvas.width = Math.floor(targetWidth);
canvas.height = Math.floor(targetHeight);
// Update actualCanvasWidth/Height based on new canvas dimensions
actualCanvasWidth = canvas.width;
actualCanvasHeight = canvas.height;
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
const projectionMatrix = createProjectionMatrix(gl.canvas.width, gl.canvas.height);
const uProjectionMatrixLoc = gl.getUniformLocation(program, 'u_projectionMatrix');
gl.uniformMatrix4fv(uProjectionMatrixLoc, false, projectionMatrix);
console.log(`Canvas resized to: ${canvas.width}x${canvas.height} for image ${img.width}x${img.height}. Container: ${containerWidth}x${containerHeight}`);
}
function calculateMinPieceSizeForImage() {
if (!originalImageObject) return;
const imgWidth = actualCanvasWidth; // Use actual canvas width
const imgHeight = actualCanvasHeight; // Use actual canvas height
let calculatedMinIndex = 0;
const currentTargetMaxPieces = parseInt(maxPiecesInput.value);
for (let i = 0; i < PUZZLE_PIECE_SIZES.length; i++) {
const p = PUZZLE_PIECE_SIZES[i];
// When calculating piece count, use the original image's aspect ratio to ensure consistent piece distribution,
// but scale the piece dimension to the current canvas size
const scaledPieceDimension = p * (imgWidth / originalImageObject.width);
const pieceCountX = Math.ceil(imgWidth / scaledPieceDimension);
const pieceCountY = Math.ceil(imgHeight / scaledPieceDimension);
const totalPieceCount = pieceCountX * pieceCountY;
if (totalPieceCount <= currentTargetMaxPieces) {
calculatedMinIndex = i;
break;
}
}
currentMinPieceSizeIndex = calculatedMinIndex;
updatePieceSizeSliderRange();
}
function updatePieceSizeSliderRange() {
if (unlockPieceSizeCheckbox.checked) {
pixelSizeSlider.min = 0;
} else {
pixelSizeSlider.min = currentMinPieceSizeIndex;
}
let sliderVal = parseInt(pixelSizeSlider.value);
const sliderMin = parseInt(pixelSizeSlider.min);
const sliderMax = parseInt(pixelSizeSlider.max);
let valueChanged = false;
if (sliderVal < sliderMin) {
pixelSizeSlider.value = sliderMin;
valueChanged = true;
} else if (sliderVal > sliderMax) {
pixelSizeSlider.value = sliderMax;
valueChanged = true;
}
if (valueChanged) {
pixelSizeSlider.dispatchEvent(new Event('input'));
} else {
pixelSizeValueSpan.textContent = `${PUZZLE_PIECE_SIZES[pixelSizeSlider.value]}x${PUZZLE_PIECE_SIZES[pixelSizeSlider.value]}`;
pixelSizeMinusBtn.disabled = parseInt(pixelSizeSlider.value) <= parseInt(pixelSizeSlider.min);
pixelSizePlusBtn.disabled = parseInt(pixelSizeSlider.value) >= parseInt(pixelSizeSlider.max);
}
}
function createPuzzlePiecesFromImage() {
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
let inputValue = parseInt(maxPiecesInput.value);
if (isNaN(inputValue) || inputValue < 4) {
inputValue = 4;
maxPiecesInput.value = 4;
} else {
maxPiecesInput.value = Math.floor(inputValue);
}
TARGET_MAX_PIECES = inputValue;
if (mainAtlasTexture) {
gl.deleteTexture(mainAtlasTexture);
mainAtlasTexture = null;
}
puzzlePieces = [];
numVertices = 0;
// These are the dimensions of the original image's pixels that each piece corresponds to
const originalPieceDim = currentPuzzlePieceSize;
// Calculate the scale factor from original image dimensions to current canvas dimensions
const scaleFactorX = actualCanvasWidth / originalImageObject.width;
const scaleFactorY = actualCanvasHeight / originalImageObject.height;
// The dimension of the piece when rendered on the canvas
// Use the average scale for consistent square pieces visually
const pieceDimForRendering = originalPieceDim * Math.min(scaleFactorX, scaleFactorY);
const numPiecesX = Math.ceil(originalImageObject.width / originalPieceDim);
const numPiecesY = Math.ceil(originalImageObject.height / originalPieceDim);
const atlasWidth = nextPowerOf2(numPiecesX * originalPieceDim);
const atlasHeight = nextPowerOf2(numPiecesY * originalPieceDim);
const atlasCanvas = document.createElement('canvas');
const atlasCtx = atlasCanvas.getContext('2d', { willReadFrequently: true });
atlasCanvas.width = atlasWidth;
atlasCanvas.height = atlasHeight;
atlasCtx.clearRect(0, 0, atlasWidth, atlasHeight);
let pieceCount = 0;
for (let y = 0; y < originalImageObject.height; y += originalPieceDim) {
for (let x = 0; x < originalImageObject.width; x += originalPieceDim) {
const currentPieceSrcX = x;
const currentPieceSrcY = y;
const atlasCol = Math.floor(currentPieceSrcX / originalPieceDim);
const atlasRow = Math.floor(currentPieceSrcY / originalPieceDim);
const atlasDestX = atlasCol * originalPieceDim;
const atlasDestY = atlasRow * originalPieceDim;
atlasCtx.drawImage(
originalImageObject,
currentPieceSrcX, currentPieceSrcY, originalPieceDim, originalPieceDim,
atlasDestX, atlasDestY, originalPieceDim, originalPieceDim
);
const texU0 = atlasDestX / atlasWidth;
const texV0 = atlasDestY / atlasHeight;
const texU1 = (atlasDestX + originalPieceDim) / atlasWidth;
const texV1 = (atlasDestY + originalPieceDim) / atlasHeight;
let startX, startY;
const edge = Math.floor(Math.random() * 4);
// These start positions should be relative to the scaled image (actualCanvasWidth/Height)
if (edge === 0) { // Top
startX = Math.random() * actualCanvasWidth;
startY = -Math.random() * 200;
} else if (edge === 1) { // Right
startX = actualCanvasWidth + Math.random() * 200;
startY = Math.random() * actualCanvasHeight;
} else if (edge === 2) { // Bottom
startX = Math.random() * actualCanvasWidth;
startY = actualCanvasHeight + Math.random() * 200;
} else { // Left
startX = -Math.random() * 200;
startY = Math.random() * actualCanvasHeight;
}
// Target X and Y must be scaled according to current canvas dimensions
puzzlePieces.push(new PuzzlePiece(startX, startY, x * scaleFactorX, y * scaleFactorY, pieceDimForRendering, texU0, texV0, texU1, texV1));
numVertices += 6;
pieceCount++;
}
}
mainAtlasTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, mainAtlasTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, atlasCanvas);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
allPositions = new Float32Array(numVertices * 2);
allTexCoords = new Float32Array(numVertices * 2);
let texCoordIndex = 0;
puzzlePieces.forEach(piece => {
for (let i = 0; i < piece.texCoords.length; i++) {
allTexCoords[texCoordIndex++] = piece.texCoords[i];
}
});
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, allTexCoords, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, allPositions.byteLength, gl.DYNAMIC_DRAW);
console.log(`Created ${pieceCount} puzzle pieces (Total GL vertices: ${numVertices}) with rendering dimension ${pieceDimForRendering.toFixed(2)}x${pieceDimForRendering.toFixed(2)} (original texture piece size: ${originalPieceDim}x${originalPieceDim}). Gravity: ${gravityForce}, Snap Distance: ${snapDistance}. Repel: ${repelForce}, Frequency: ${repelFrequency}. Gravity Field: ${gravityBallEnabled ? (BASE_GRAVITY_FIELD_FORCE * gravityFieldStrength) : 'Off'}. Atlas size: ${atlasWidth}x${atlasHeight}`);
startAnimation();
}
let previousTimeStamp = 0;
let repelInterval = 0;
let uSamplerLoc = null; // Declare uSamplerLoc globally or pass it