Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
313 lines (275 loc) · 10.8 KB
/
Copy pathscript.js
File metadata and controls
313 lines (275 loc) · 10.8 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
const CACHE_VERSION = "3";
const CACHE_KEY = "moire-design-rotator-cache-version";
function forceCacheRefresh() {
try {
const storedVersion = window.localStorage.getItem(CACHE_KEY);
const currentUrl = new URL(window.location.href);
const versionParam = currentUrl.searchParams.get("v");
if (storedVersion !== CACHE_VERSION || versionParam !== CACHE_VERSION) {
window.localStorage.setItem(CACHE_KEY, CACHE_VERSION);
currentUrl.searchParams.set("v", CACHE_VERSION);
window.location.replace(currentUrl.toString());
return true;
}
} catch (error) {
// If storage or URL rewriting is unavailable, continue normally.
}
return false;
}
forceCacheRefresh();
const imageContainer = document.getElementById("imageContainer");
const image1 = document.getElementById("image1");
const image2 = document.getElementById("image2");
const inputImage1 = document.getElementById("inputImage1");
const inputImage2 = document.getElementById("inputImage2");
const inputImage2Text = document.getElementById("inputImage2Text");
const speed1Input = document.getElementById("speed1");
const direction1CheckBox = document.getElementById("direction1");
const speed2Input = document.getElementById("speed2");
const direction2CheckBox = document.getElementById("direction2");
const pauseRotationButton = document.getElementById("pauseRotation");
const resetRotationButton = document.getElementById("resetRotation");
const sameDesignCheckBox = document.getElementById("sameDesign");
const sameDesignText = document.getElementById("sameDesignText");
const flipDesignCheckBox = document.getElementById("flipped");
const flipDesignText = document.getElementById("flippedText");
const colorInput = document.getElementById("color");
const bgColor = document.getElementById("bgColor");
const animColorControls = document.getElementById("animColorControls");
const animColorList = document.getElementById("animColorList");
const addAnimColor = document.getElementById("addAnimColor");
const removeAnimColor = document.getElementById("removeAnimColor");
const animColorButton = document.getElementById("animColorButton");
const bgAnimSpeed = document.getElementById("bgAnimSpeed");
const bgAnimSpeedLabel = document.getElementById("bgAnimSpeedLabel");
const root = document.querySelector(":root");
const variables = getComputedStyle(root);
let isPaused = true;
let bgAnimPaused = true;
let initialSpeed1 = parseFloat(speed1Input.value);
// initialize visibility of speed control and its label based on current background mode
if (bgAnimSpeed && colorInput) {
const show = colorInput.value === "animColor";
bgAnimSpeed.style.display = show ? "inline-block" : "none";
if (bgAnimSpeedLabel)
bgAnimSpeedLabel.style.display = show ? "inline-block" : "none";
}
let initialSpeed2 = parseFloat(speed2Input.value);
inputImage1.addEventListener("change", (event) => {
const file = event.target.files[0];
if (file) {
const imageUrl = URL.createObjectURL(file);
image1.src = imageUrl;
}
});
inputImage2.addEventListener("change", (event) => {
const file = event.target.files[0];
if (file) {
const imageUrl = URL.createObjectURL(file);
image2.src = imageUrl;
}
});
colorInput.addEventListener("change", (event) => {
if (event.target.value === "animColor") {
animColorControls.style.display = "flex";
bgColor.style.display = "none";
// ensure at least 2 pickers exist
if (animColorList.querySelectorAll(".anim-color").length === 0) {
addColorPicker("#33CCCC");
addColorPicker("#33CC36");
addColorPicker("#B8CC33");
}
imageContainer.style.transitionDuration = "0.75s";
if (bgAnimSpeed) bgAnimSpeed.style.display = "inline-block";
if (bgAnimSpeedLabel) bgAnimSpeedLabel.style.display = "inline-block";
} else {
animColorControls.style.display = "none";
bgColor.style.display = "inline-block";
bgAnimPaused = true;
imageContainer.style.transitionDuration = "0s";
imageContainer.style.backgroundColor = bgColor.value;
if (bgAnimSpeed) bgAnimSpeed.style.display = "none";
if (bgAnimSpeedLabel) bgAnimSpeedLabel.style.display = "none";
animColorButton.textContent = "Resume Anim";
}
});
animColorButton.addEventListener("click", () => {
if (bgAnimPaused === true) {
// collect colors from pickers
const pickers = Array.from(
animColorList.querySelectorAll(".anim-color"),
);
const colors = pickers.map((p) => p.value).filter(Boolean);
if (colors.length === 0) return; // nothing to animate
bgAnimPaused = false;
animateBackgroundColor(colors);
animColorButton.textContent = "Stop Anim";
} else {
bgAnimPaused = true;
imageContainer.style.backgroundColor = bgColor.value;
animColorButton.textContent = "Resume Anim";
}
});
addAnimColor.addEventListener("click", () => addColorPicker("#ffffff"));
removeAnimColor.addEventListener("click", () => {
const pickers = animColorList.querySelectorAll(".anim-color");
if (pickers.length > 0)
animColorList.removeChild(pickers[pickers.length - 1]);
});
function addColorPicker(initial = "#ffffff") {
const input = document.createElement("input");
input.type = "color";
input.value = initial;
input.className = "anim-color";
input.style.width = "40px";
input.style.height = "36px";
input.style.border = "none";
input.style.padding = "0";
input.style.background = "transparent";
animColorList.appendChild(input);
return input;
}
bgColor.addEventListener("input", () => {
imageContainer.style.backgroundColor = bgColor.value;
});
speed1Input.addEventListener("input", () => {
if (!isPaused) {
updateImageRotation(image1, speed1Input, direction1CheckBox);
}
});
direction1CheckBox.addEventListener("click", () => {
updateImageRotation(image1, speed1Input, direction1CheckBox);
});
speed2Input.addEventListener("input", () => {
if (!isPaused) {
updateImageRotation(image2, speed2Input, direction2CheckBox);
}
});
direction2CheckBox.addEventListener("click", () => {
updateImageRotation(image2, speed2Input, direction2CheckBox);
});
pauseRotationButton.addEventListener("click", () => {
if (pauseRotationButton.textContent === "Start") {
// Starting rotation from initial paused state
speed1Input.value = 5;
speed2Input.value = 5;
isPaused = false;
pauseRotationButton.textContent = "Pause Rotation";
resetAnimation();
updateImageRotation(image1, speed1Input, direction1CheckBox);
updateImageRotation(image2, speed2Input, direction2CheckBox);
} else {
// Toggling between pause and resume
isPaused = !isPaused;
if (isPaused) {
pauseRotationButton.textContent = "Resume Rotation";
pauseRotation();
} else {
pauseRotationButton.textContent = "Pause Rotation";
resumeRotation();
}
}
resetAnimation();
});
resetRotationButton.addEventListener("click", () => {
resetAnimation();
});
// Remembers the user's own Image #2 while "Same Design" borrows Image #1's,
// so unchecking restores it instead of discarding it.
let savedImage2Src = null;
sameDesignCheckBox.addEventListener("click", (event) => {
if (event.target.checked) {
flipDesignCheckBox.style.display = "inline-grid";
flipDesignText.style.display = "inline-block";
savedImage2Src = image2.src;
image2.src = image1.src;
} else {
flipDesignCheckBox.style.display = "none";
flipDesignText.style.display = "none";
// The flip only applies to the borrowed design; clear it so the
// restored Image #2 isn't left mirrored.
flipDesignCheckBox.checked = false;
image2.style.scale = "1 1";
image2.src = savedImage2Src || "DPEA_Logo.png?v=3";
inputImage2Text.style.display = "none";
inputImage2.style.display = "inline-block";
}
});
flipDesignCheckBox.addEventListener("click", (event) => {
if (event.target.checked) {
image2.style.scale = "-1 1";
} else {
image2.style.scale = "1 1";
}
});
function load() {
pauseRotation();
pauseRotationButton.textContent = "Start";
}
function updateImageRotation(image, speedInput, directionSelect) {
const speed = parseFloat(speedInput.value);
const direction = directionSelect.checked === false ? "normal" : "reverse";
image.style.setProperty("--rotation-speed", `${speed}s`);
image.style.setProperty("--rotation-direction", direction);
// Control animation play state
image.style.animationPlayState = isPaused ? "paused" : "running";
}
function pauseRotation() {
initialSpeed1 = parseFloat(speed1Input.value);
initialSpeed2 = parseFloat(speed2Input.value);
speed1Input.value = "0";
speed2Input.value = "0";
updateImageRotation(image1, speed1Input, direction1CheckBox);
updateImageRotation(image2, speed2Input, direction2CheckBox);
}
function resumeRotation() {
if (parseFloat(speed1Input.value) == 0) speed1Input.value = initialSpeed1;
if (parseFloat(speed2Input.value) == 0) speed2Input.value = initialSpeed2;
updateImageRotation(image1, speed1Input, direction1CheckBox);
updateImageRotation(image2, speed2Input, direction2CheckBox);
}
function resetAnimation() {
let image1 = document.querySelector("#image1");
let image2 = document.querySelector("#image2");
image1.style.animationName = "none";
image2.style.animationName = "none";
requestAnimationFrame(() => {
setTimeout(() => {
image1.style.animationName = "";
}, 0);
});
requestAnimationFrame(() => {
setTimeout(() => {
image2.style.animationName = "";
}, 0);
});
}
function animateBackgroundColor(colors) {
let currentIndex = 0;
function changeColor() {
if (colors.length > 0 && bgAnimPaused !== true) {
imageContainer.style.backgroundColor = colors[currentIndex];
currentIndex = (currentIndex + 1) % colors.length;
// read speed (seconds) from control, default to 1s
let interval = 1000;
try {
const v = parseFloat(bgAnimSpeed && bgAnimSpeed.value);
if (!isNaN(v) && v > 0) interval = Math.max(50, v * 1000);
} catch (e) {
interval = 1000;
}
setTimeout(changeColor, interval);
}
}
changeColor();
}
setInterval(function () {
if (sameDesignCheckBox.checked) {
inputImage2.style.display = "none";
inputImage2Text.style.display = "inline-block";
image2.src = image1.src;
if (flipDesignCheckBox.checked) {
image2.style.scale = "-1 1";
}
}
}, 400);