-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
558 lines (523 loc) · 25.8 KB
/
Copy pathscript.js
File metadata and controls
558 lines (523 loc) · 25.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
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
const sections = Array.from(document.querySelectorAll("main section[id]"));
const navLinks = Array.from(document.querySelectorAll(".site-nav a[href^='#']"));
const langToggle = document.getElementById("langToggle");
const copyEmailButtons = Array.from(document.querySelectorAll(".copy-email[data-email]"));
const inlineTranslationElements = Array.from(document.querySelectorAll("[data-en]"));
const isAuthorPage = window.location.pathname.toLowerCase().endsWith("author.html");
const commonTranslations = [
{ selector: ".site-nav", attrs: { "aria-label": "Main navigation" } },
{ selector: ".site-nav a:nth-of-type(1)", text: "Overview" },
{ selector: ".site-nav a:nth-of-type(2)", text: "Core Math" },
{ selector: ".site-nav a:nth-of-type(3)", text: "Workflow" },
{ selector: ".site-nav a:nth-of-type(4)", text: "Downloads" },
{ selector: ".site-nav a:nth-of-type(5)", text: "Author" },
{ selector: ".brand", attrs: { "aria-label": "MotionLab Tracker Guide" } },
];
const indexTranslations = [
{ selector: ".intro-copy .section-label", text: "2D & 3D Motion Tracking Tool" },
{ selector: ".intro-copy h1", html: "MotionLab Tracker<br>Technical Guide" },
{
selector: ".intro-copy > p:not(.section-label)",
text:
"MotionLab 2D / 3D Workbench is a physics experiment video-tracking tool for high school classrooms and inquiry-based labs. The portable .exe requires no installation — ZNCC auto-tracking, robust 3D fusion, and one-click CSV / Python export included.",
},
{ selector: ".intro-actions .button.primary", text: "Download Now" },
{ selector: ".intro-actions .button:not(.primary)", text: "See Workflow" },
{
selector: ".app-showcase",
attrs: { "aria-label": "MotionLab 2D and 3D application screenshots" },
},
{
selector: ".app-shot-primary figcaption span",
text: "Single-view tracking with calibration, scale setup, and exported motion data.",
},
{
selector: ".app-shot:not(.app-shot-primary) figcaption span",
text: "Dual-view XZ / YZ tracking with depth calibration and 3D trajectory output.",
},
// Feature strip
{ selector: ".fcard:nth-child(1) h3", text: "ZNCC Auto Track" },
{ selector: ".fcard:nth-child(1) p", text: "ZNCC template matching — robust to lighting changes; single cached frame read for fast continuous tracking." },
{ selector: ".fcard:nth-child(2) h3", text: "Robust 3D Fusion" },
{ selector: ".fcard:nth-child(2) p", text: "Sub-frame sync search, Huber IRLS outlier rejection, SG velocity smoothing, cubic Hermite resampling." },
{ selector: ".fcard:nth-child(3) h3", text: "One-click Export" },
{ selector: ".fcard:nth-child(3) p", text: "Export XYZ data as CSV, 3D plot, four-view image, and a ready-to-run Python analysis script." },
{ selector: "#principles .section-head .section-label", text: "Core Math" },
{ selector: "#principles .section-head h2", text: "Core Calculation Principles" },
{
selector: "#principles .split-block:not(.reverse) h3",
text: "2D Tracker: calibration, coordinates, and derivatives",
},
{
selector: "#principles .split-block:not(.reverse) > div:first-child > p:nth-of-type(1)",
text:
"2D tracking treats the video as a calibrated plane. After choosing an origin, axis direction, and known physical length, pixel positions can be converted into real-world coordinates. With the frame rate, position samples can then be transformed into displacement, velocity, and acceleration.",
},
{
selector: "#principles .split-block:not(.reverse) .equation-card:nth-child(1) figcaption",
html: "A known physical length L and its pixel distance d<sub>px</sub> define the pixel-to-world scale S.",
},
{
selector: "#principles .split-block:not(.reverse) .equation-card:nth-child(2) figcaption",
text:
"Pixel coordinates are translated to the selected origin, rotated into the chosen axes, and scaled into physical coordinates.",
},
{
selector: "#principles .split-block:not(.reverse) .equation-card:nth-child(3) figcaption",
text:
"Velocity and acceleration are estimated from neighboring frames, so stable tracking points and frame rate matter.",
},
{
selector: "#principles .split-block:not(.reverse) > div:first-child > p:nth-of-type(2)",
text:
"The exported data is suited for classroom analysis, graphs, CSV processing, and follow-up fitting. When the tracked point jitters, centroid snap and manual correction reduce frame-to-frame noise.",
},
{ selector: "#principles .split-block:not(.reverse) .note-panel h4", text: "2D Use Notes" },
{
selector: "#principles .split-block:not(.reverse) .note-panel li:nth-child(1)",
text: "Use a clear calibration length in the same plane as the motion.",
},
{
selector: "#principles .split-block:not(.reverse) .note-panel li:nth-child(2)",
text: "Keep the camera still and avoid perspective changes when possible.",
},
{
selector: "#principles .split-block:not(.reverse) .note-panel li:nth-child(3)",
text: "Export time, x, y, vx, vy, and derived data for graphs or CSV analysis.",
},
{
selector: "#principles .split-block.reverse > div:first-child h3",
text: "3D Tracker: paired XZ / YZ views",
},
{
selector: "#principles .split-block.reverse > div:first-child > p:nth-of-type(1)",
text:
"MotionLab 3D Workbench uses two synchronized views: view A records the XZ plane and view B records the YZ plane. After setting scale, FPS, and matching frames, the two tracks can be fused into X, Y, and Z coordinates.",
},
{
selector: "#principles .split-block.reverse .equation-card:nth-child(1) figcaption",
text: "Parallax model: the depth of Y affects x_A, and the depth of X affects y_B.",
},
{
selector: "#principles .split-block.reverse .equation-card:nth-child(2) figcaption",
text: "View B is aligned by scale (s) and offset (b) before fusion: both Y and Z are scaled, Z also offset.",
},
{
selector: "#principles .split-block.reverse .equation-card:nth-child(3) figcaption",
text: "Z is estimated jointly from both views; Z residual RMS gauges fusion quality.",
},
{
selector: "#principles .split-block.reverse > div:first-child > p:nth-of-type(2)",
text:
"α and β are estimated by Huber IRLS across all valid frames; sub-frame sync τ is found by golden-section search. Each frame's X, Y, Z is solved individually using the current depth, then resampled with cubic Hermite splines for smooth curved trajectories.",
},
{ selector: "#principles .split-block.reverse .note-panel h4", text: "3D Calibration & Use Notes" },
{
selector: "#principles .split-block.reverse .note-panel li:nth-child(1)",
text: "Place the origin near a shared calibration reference visible in both views.",
},
{
selector: "#principles .split-block.reverse .note-panel li:nth-child(2)",
text: "If view B shoots from +X toward −X, map the horizontal axis to world Y: left = +Y, right = −Y.",
},
{
selector: "#principles .split-block.reverse .note-panel li:nth-child(3)",
text: "Full 3D mode suits trajectories with clear X, Y, and Z variation; plane mode suits mostly-flat motion.",
},
{
selector: "#principles .split-block.reverse .note-panel li:nth-child(4)",
text: "Inspect the 3D trajectory and export CSV/Python-ready data after correction.",
},
{ selector: "#workflow .section-head .section-label", text: "How To Use" },
{ selector: "#workflow .section-head h2", text: "Operating Workflow" },
{ selector: ".workflow-card:nth-child(1) h3", text: "2D Workbench Workflow" },
{
selector: ".workflow-card:nth-child(1) li:nth-child(1)",
text: "Import a video, confirm FPS, and set a clear calibration length.",
},
{
selector: ".workflow-card:nth-child(1) li:nth-child(2)",
text: "Use the calibration tool to define origin, axes, and the real-world scale.",
},
{
selector: ".workflow-card:nth-child(1) li:nth-child(3)",
text: "Create a point mass and click the tracked object frame by frame (← → to step, Shift+← → for ±10 frames).",
},
{
selector: ".workflow-card:nth-child(1) li:nth-child(4)",
text: "Press T to capture a template, R to run continuous auto-tracking, or Shift+click to skip snap on a frame.",
},
{
selector: ".workflow-card:nth-child(1) li:nth-child(5)",
text: "Use centroid snap or auto tracking when the target is visually stable.",
},
{
selector: ".workflow-card:nth-child(1) li:nth-child(6)",
text: "Export coordinates, velocity, acceleration, graphs, and CSV data.",
},
{ selector: ".workflow-card:nth-child(2) h3", text: "3D Workbench Workflow" },
{
selector: ".workflow-card:nth-child(2) li:nth-child(1)",
text: "Load view A for XZ tracking and view B for YZ tracking.",
},
{
selector: ".workflow-card:nth-child(2) li:nth-child(2)",
text: "Calibrate both views with scale, orientation, and synchronized frame timing.",
},
{
selector: ".workflow-card:nth-child(2) li:nth-child(3)",
text: "Track the same physical point in both views with matched frames.",
},
{
selector: ".workflow-card:nth-child(2) li:nth-child(4)",
text: "Run Z correction and check the residual or alignment quality.",
},
{
selector: ".workflow-card:nth-child(2) li:nth-child(5)",
text: "Inspect the 3D trajectory with the plot controls and switch views as needed.",
},
{
selector: ".workflow-card:nth-child(2) li:nth-child(6)",
text: "Export the 3D result for CSV, graphing, or Python analysis.",
},
// Shortcut strip
{ selector: ".shortcut-label", text: "Common Shortcuts" },
{ selector: ".sc-item:nth-child(1) span", text: "Step frame" },
{ selector: ".sc-item:nth-child(2) span", text: "±10 frames" },
{ selector: ".sc-item:nth-child(3) span", text: "Play / Pause" },
{ selector: ".sc-item:nth-child(4) span", text: "Capture template" },
{ selector: ".sc-item:nth-child(5) span", text: "Auto track" },
{ selector: ".sc-item:nth-child(6) span", text: "Switch view (3D)" },
{ selector: ".sc-item:nth-child(7) span", text: "Run fusion (3D)" },
{ selector: ".sc-item:nth-child(8) span", text: "Export bundle" },
{ selector: "#downloads .section-head .section-label", text: "Downloads" },
{ selector: "#downloads .section-head h2", text: "Downloads And Release Status" },
{
selector: "#downloads .section-head p:not(.section-label)",
text: "Both portable packages are available on GitHub Releases — no installation needed, just download and run.",
},
// Download cards
{ selector: ".dl-card:nth-child(1) strong", text: "MotionLab 2D Workbench" },
{ selector: ".dl-card:nth-child(1) .dl-features li:nth-child(1)", text: "ZNCC auto-track · Smart centroid snap v2" },
{ selector: ".dl-card:nth-child(1) .dl-features li:nth-child(2)", text: "Savitzky-Golay velocity / acceleration smoothing" },
{ selector: ".dl-card:nth-child(1) .dl-features li:nth-child(3)", text: "FFT spectrum analysis · CSV / Python export" },
{ selector: ".dl-card:nth-child(1) .dl-features li:nth-child(4)", text: "Windows 10+ · Portable single .exe" },
{ selector: ".dl-card:nth-child(1) .dl-btn span", text: "Download .exe" },
{ selector: ".dl-card:nth-child(2) strong", text: "MotionLab 3D Workbench" },
{ selector: ".dl-card:nth-child(2) .dl-features li:nth-child(1)", text: "Dual-view XZ / YZ fusion · sub-frame sync search" },
{ selector: ".dl-card:nth-child(2) .dl-features li:nth-child(2)", text: "Huber IRLS robust calibration · cubic Hermite resampling" },
{ selector: ".dl-card:nth-child(2) .dl-features li:nth-child(3)", text: "3D trajectory plot · four-view image · Python export" },
{ selector: ".dl-card:nth-child(2) .dl-features li:nth-child(4)", text: "Windows 10+ · Portable single .exe" },
{ selector: ".dl-card:nth-child(2) .dl-btn span", text: "Download .exe" },
{ selector: ".download-row:nth-child(1) strong", text: "MotionLab 3D Source Code And Build Documentation" },
{
selector: ".download-row:nth-child(1) span",
text: "Complete Electron / JavaScript source, build configuration, and technical documentation under the MIT License.",
},
{ selector: ".download-row:nth-child(1) .button", text: "View Source Code" },
{ selector: ".download-row:nth-child(2) strong", text: "Demo Videos And Sample Data" },
{
selector: ".download-row:nth-child(2) span",
text: "Sample videos, projects, CSV output, and classroom notes will be published after organization.",
},
{ selector: ".download-row:nth-child(2) .button", text: "Pending" },
{ selector: ".references .section-head .section-label", text: "Technical References" },
{ selector: ".references .section-head h2", text: "Technical Reference Sources" },
{
selector: ".references > p",
text:
"The technical notes are based on concepts from older Tracker resources and DLTdv8 references. MotionLab reorganizes those ideas into a compact teaching workflow for 2D and 3D video tracking, with references preserved for later expansion.",
},
{
selector: ".reference-list li:nth-child(1) span",
text: "Open Source Physics / Tracker official documentation and project overview.",
},
{
selector: ".reference-list li:nth-child(2) span",
text: "Tracker getting-started workflow, including video import, calibration, tracking, and export.",
},
{
selector: ".reference-list li:nth-child(3) span",
text: "Tracker open-source project and implementation resources under the Open Source Physics framework.",
},
{
selector: ".reference-list li:nth-child(4) span",
text: "DLTdv8 reference for DLT coefficients, 2D/3D DLT calibration, and multi-view reconstruction.",
},
{ selector: ".author-preview .section-label", text: "About The Author" },
{ selector: ".author-preview h2", text: "Teacher Chang-Yi Jiang" },
{
selector: ".author-preview p:not(.section-label)",
text:
"A physics educator focused on high-school physics teaching, inquiry-based practice, physics competition mentoring, physics simulations, and MotionLab video-tracking tools for classroom and research use.",
},
{ selector: ".author-preview .button", text: "View Full Author Profile" },
{ selector: ".footer-copy", text: "© 2026 MotionLab · aiphysicsteacher" },
];
const authorTranslations = [
{ selector: ".author-summary .section-label", text: "Author Information" },
{ selector: ".author-summary h1", text: "Teacher Chang-Yi Jiang" },
{
selector: ".author-lead",
text:
"A physics educator dedicated to high-school physics teaching, inquiry-based practice, science fair mentoring, and physics competitions. MotionLab turns video tracking, data visualization, and physics modeling into classroom-ready tools so students can study motion, mechanics, and 3D trajectory data from real footage.",
},
{ selector: ".tag-list", attrs: { "aria-label": "Expertise tags" } },
{ selector: ".tag-list span:nth-child(1)", text: "Physics Education" },
{ selector: ".tag-list span:nth-child(2)", text: "Motion Analysis" },
{ selector: ".tag-list span:nth-child(3)", text: "Inquiry And Practice" },
{ selector: ".tag-list span:nth-child(4)", text: "Physics Simulation" },
{ selector: ".author-contact-card", attrs: { "aria-label": "Author contact information" } },
{ selector: ".profile-name span", text: "MotionLab / AI Physics Teacher" },
{ selector: ".education-card .section-label", text: "Education" },
{ selector: ".education-card h2", text: "Education" },
{ selector: ".education-card li:nth-child(1)", text: "B.S., Department of Physics, National Changhua University of Education" },
{ selector: ".education-card li:nth-child(2)", text: "M.S., Graduate Institute of Physics, National Changhua University of Education" },
{ selector: ".education-card li:nth-child(3)", text: "Doctoral student, Graduate Institute of Science Education, National Taiwan Normal University" },
{ selector: ".experience-card .section-label", text: "Teaching Experience" },
{ selector: ".experience-card h2", text: "Experience" },
{ selector: ".experience-card li:nth-child(1)", text: "One year as a full-time substitute physics teacher at National Chung Hsing Senior High School" },
{ selector: ".experience-card li:nth-child(2)", text: "Five years as a full-time physics teacher at Mingdao High School" },
{ selector: ".achievement-card .section-label", text: "Mentoring" },
{ selector: ".achievement-card h2", text: "Mentoring And Competition Highlights" },
{ selector: ".achievement-item:nth-child(1) strong", text: "15 projects" },
{ selector: ".achievement-item:nth-child(1) span", text: "High-school science fair projects mentored" },
{ selector: ".achievement-item:nth-child(2) strong", text: "2 projects" },
{ selector: ".achievement-item:nth-child(2) span", text: "Third place in Taichung Astronomy and Physics science fair category" },
{ selector: ".achievement-item:nth-child(3) strong", text: "3 projects" },
{ selector: ".achievement-item:nth-child(3) span", text: "Honorable mentions in Taichung Astronomy and Physics science fair category" },
{ selector: ".achievement-item:nth-child(4) strong", text: "TYPT" },
{ selector: ".achievement-item:nth-child(4) span", text: "Taiwan Young Physicists' Tournament: 2 gold medals, 3 bronze medals, and 3 individual awards" },
{ selector: ".achievement-item:nth-child(5) strong", text: "4 students" },
{ selector: ".achievement-item:nth-child(5) span", text: "Advanced to the national physics academic ability competition" },
{ selector: ".achievement-item:nth-child(6) strong", text: "4 medals" },
{ selector: ".achievement-item:nth-child(6) span", text: "Inquiry and practice competition: 2 gold medals and 2 bronze medals" },
{ selector: ".project-card .section-label", text: "MotionLab Project" },
{ selector: ".project-card h2", text: "MotionLab Project" },
{
selector: ".project-card p:nth-of-type(2)",
text:
"MotionLab 2D / 3D Tracker is a video analysis tool designed for physics classes, inquiry-based practice, and science fair research. The project combines older Tracker resources, DLTdv8-related concepts, and classroom workflow needs to support calibration, tracking, graph review, data export, and 3D trajectory analysis.",
},
{
selector: ".project-card p:nth-of-type(3)",
text:
"The 2D and 3D portable packages are available on GitHub Releases. The complete MotionLab 3D Workbench source code and build documentation are publicly available under the MIT License.",
},
{ selector: ".project-card .button", text: "View MotionLab Releases" },
{ selector: ".research-card .section-label", text: "Research And Works" },
{ selector: ".research-card h2", text: "Research And Works" },
{
selector: ".publication-list li:nth-child(1) span",
text: "Chang-Yi Jiang. (2016). The Secrets and Techniques of Swinging. Chinese Physics, 17(2), 25-32.",
},
{
selector: ".publication-list li:nth-child(2) a",
text: "Master's thesis: Conceptual Difficulties And Instructional Effects Of Friction",
},
{
selector: ".publication-list li:nth-child(3) a",
text: "Physics Bimonthly: Revealing The Secrets Of Polarized Light And Color",
},
{
selector: ".publication-list li:nth-child(4) a",
text: "Physics Bimonthly column: Physics Inquiry And Practice: Online Simulation",
},
{ selector: ".publication-list li:nth-child(5) > span", text: "Physics Simulations: High School And Competition Physics" },
{ selector: ".publication-links a:nth-child(1)", text: "High School Physics Simulations" },
{ selector: ".publication-links a:nth-child(2)", text: "Competition Physics Simulations" },
{ selector: ".author-footer-actions .button.primary", text: "Back To Guide" },
{ selector: ".author-footer-actions .copy-email", text: "Copy Email" },
{ selector: ".site-footer span:nth-child(2)", text: "Author profile" },
];
const translations = commonTranslations.concat(isAuthorPage ? authorTranslations : indexTranslations);
const originals = [];
const originalTitle = document.title;
function setActiveNav() {
const current = sections
.map((section) => ({ id: section.id, top: section.getBoundingClientRect().top }))
.filter((item) => item.top <= 120)
.pop();
navLinks.forEach((link) => {
const target = link.getAttribute("href").replace("#", "");
link.classList.toggle("active", Boolean(current && target === current.id));
});
}
function captureOriginals() {
translations.forEach((entry, entryIndex) => {
originals[entryIndex] = Array.from(document.querySelectorAll(entry.selector)).map((element) => {
const attrs = {};
Object.keys(entry.attrs || {}).forEach((name) => {
attrs[name] = element.getAttribute(name);
});
return {
html: element.innerHTML,
text: element.textContent,
attrs,
};
});
});
}
function applyLanguage(language) {
const isEnglish = language === "en";
document.documentElement.lang = isEnglish ? "en" : "zh-Hant";
document.title = isEnglish
? isAuthorPage
? "Author Information | MotionLab Tracker Guide"
: "MotionLab Tracker Technical And Operation Guide"
: originalTitle;
translations.forEach((entry, entryIndex) => {
Array.from(document.querySelectorAll(entry.selector)).forEach((element, elementIndex) => {
const original = originals[entryIndex]?.[elementIndex];
if (isEnglish) {
if (entry.html !== undefined) {
element.innerHTML = entry.html;
} else if (entry.text !== undefined) {
element.textContent = entry.text;
}
Object.entries(entry.attrs || {}).forEach(([name, value]) => {
element.setAttribute(name, value);
});
return;
}
if (original) {
if (entry.html !== undefined) {
element.innerHTML = original.html;
} else if (entry.text !== undefined) {
element.textContent = original.text;
}
Object.keys(entry.attrs || {}).forEach((name) => {
if (original.attrs[name] === null) {
element.removeAttribute(name);
} else {
element.setAttribute(name, original.attrs[name]);
}
});
}
});
});
applyInlineTranslations(isEnglish);
if (langToggle) {
langToggle.textContent = isEnglish ? "中" : "EN";
langToggle.setAttribute("aria-label", isEnglish ? "切換到中文" : "Switch to English");
}
}
function applyInlineTranslations(isEnglish) {
inlineTranslationElements.forEach((element) => {
if (!element.dataset.zhOriginal) {
element.dataset.zhOriginal = element.textContent;
}
element.textContent = isEnglish ? element.dataset.en : element.dataset.zhOriginal;
});
}
function captureInlineOriginals() {
inlineTranslationElements.forEach((element) => {
if (!element.dataset.zhOriginal) {
element.dataset.zhOriginal = element.textContent;
}
});
}
function initLanguageToggle() {
if (!langToggle) {
return;
}
captureOriginals();
captureInlineOriginals();
const savedLanguage = readSavedLanguage();
applyLanguage(savedLanguage);
langToggle.addEventListener("click", () => {
const nextLanguage = document.documentElement.lang === "en" ? "zh" : "en";
saveLanguage(nextLanguage);
applyLanguage(nextLanguage);
setActiveNav();
});
}
function readSavedLanguage() {
try {
return localStorage.getItem("motionlab-guide-language") === "en" ? "en" : "zh";
} catch {
return "zh";
}
}
function saveLanguage(language) {
try {
localStorage.setItem("motionlab-guide-language", language);
} catch {
// The toggle still works for the current page even if file:// storage is blocked.
}
}
function initCopyEmailButtons() {
copyEmailButtons.forEach((button) => {
button.addEventListener("click", async () => {
const email = button.dataset.email;
try {
await copyText(email);
button.textContent = document.documentElement.lang === "en" ? "Email copied" : "Email 已複製";
} catch {
button.textContent = email;
}
window.setTimeout(() => {
button.textContent = document.documentElement.lang === "en" ? "Copy Email" : "複製 Email";
}, 1800);
});
});
}
async function copyText(value) {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(value);
return;
}
const textarea = document.createElement("textarea");
textarea.value = value;
textarea.setAttribute("readonly", "");
textarea.style.position = "fixed";
textarea.style.left = "-9999px";
textarea.style.opacity = "0";
document.body.appendChild(textarea);
textarea.select();
const copied = document.execCommand("copy");
textarea.remove();
if (!copied) {
throw new Error("Copy command failed");
}
}
window.addEventListener("scroll", setActiveNav, { passive: true });
window.addEventListener("load", setActiveNav);
initLanguageToggle();
initCopyEmailButtons();
// ===== SCROLL REVEAL =====
function initScrollReveal() {
if (typeof IntersectionObserver === 'undefined') {
document.querySelectorAll('[data-reveal]').forEach(el => el.classList.add('revealed'));
return;
}
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('revealed');
observer.unobserve(entry.target);
}
});
}, { threshold: 0.06, rootMargin: '0px 0px -40px 0px' });
document.querySelectorAll('[data-reveal]').forEach(el => observer.observe(el));
}
initScrollReveal();
// ===== THEME TOGGLE =====
// Reads system preference on first visit; subsequent visits use localStorage.
// The anti-flash inline script in <head> applies the saved theme before CSS loads.
function initThemeToggle() {
var btn = document.getElementById('themeToggle');
if (!btn) return;
btn.addEventListener('click', function () {
var html = document.documentElement;
var isDark = html.getAttribute('data-theme') === 'dark';
if (isDark) {
html.removeAttribute('data-theme');
localStorage.setItem('ml-theme', 'light');
} else {
html.setAttribute('data-theme', 'dark');
localStorage.setItem('ml-theme', 'dark');
}
});
}
initThemeToggle();