BlueGit

timfa kwin-marble

Marble Magic Lamp: clock-driven path segments (no drift/stretch)

timfa Tim Falken · pushed by timfa

Authored 2026-11-02 · 2 weeks ago

Hash
a865999d5efe16affdfbd9288feafdc0a18643cd
Parent
6463ba12
1 file changed +50 −18
kwin6_effect_marble_minimize/contents/code/main.js +50 −18
@@ -228,7 +228,17 @@ class BurnMyWindowsMarbleMinimizeEffect {
228 228 const q = j < M ? [pts[j][0] + (pts[j + 1][0] - pts[j][0]) * f, pts[j][1] + (pts[j + 1][1] - pts[j][1]) * f] : pts[M];
229 229 nodes.push({x: q[0], y: q[1], t: animationTime(Math.round(1000 * Math.sqrt(2 * s / this.gravity)))});
230 230 }
231 return {nodes: nodes, length: L, total: nodes[N].t};
231 // continuous position at path time t (ms, already scaled by the animation factor)
232 const fac = Math.max(0.001, animationTime(1000) / 1000), g = this.gravity;
233 const posAt = (tms) => {
234 const ts = Math.max(0, tms) / 1000 / fac;
235 const sa = Math.min(L, 0.5 * g * ts * ts);
236 let lo = 0, hi = M;
237 while (hi - lo > 1) { const mid = (lo + hi) >> 1; if (cum[mid] <= sa) lo = mid; else hi = mid; }
238 const f = (sa - cum[lo]) / Math.max(1e-6, cum[hi] - cum[lo]);
239 return {x: pts[lo][0] + (pts[hi][0] - pts[lo][0]) * f, y: pts[lo][1] + (pts[hi][1] - pts[lo][1]) * f};
240 };
241 return {nodes: nodes, length: L, total: nodes[N].t, posAt: posAt, tScale: nodes[N - 2].t};
232 242 }
233 243
234 244 segAnim(window, a, b, dur, extra) {
@@ -293,12 +303,13 @@ class BurnMyWindowsMarbleMinimizeEffect {
293 303 window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, T - nd[N - 2].t),
294 304 animations: [{ type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 1.0, value2: 1.0} }]
295 305 });
296 window.bmwPath = path; window.bmwSeg = N; window.bmwMode = "rise";
297 window.bmwSegAnimation = this.segAnim(window, nd[N], nd[N - 1], nd[N].t - nd[N - 1].t);
306 window.bmwPath = path; window.bmwMode = "rise";
307 window.bmwT0 = Date.now(); window.bmwCur = {x: nd[N].x, y: nd[N].y};
308 this.step(window);
298 309 }
299 310
300 311 stopAll(window) {
301 window.bmwMode = null;
312 window.bmwMode = null; window.bmwScaling = false;
302 313 for (const k of ["bmwMinAnimation", "bmwMarbleDone", "bmwSegAnimation", "bmwScaleAnimation",
303 314 "bmwTailAnimation", "bmwUnminAnimation"]) {
304 315 if (window[k]) { cancel(window[k]); delete window[k]; }
@@ -317,15 +328,25 @@ class BurnMyWindowsMarbleMinimizeEffect {
317 328 try { effects.addRepaintFull(); } catch (e) {}
318 329 return;
319 330 }
331 if (window.bmwMode == "min" && window.bmwSeg < 0) { // morph + pause done
332 window.bmwSeg = 0; window.bmwT0 = Date.now(); window.bmwCur = {x: 0, y: 0};
333 }
334 this.step(window);
335 }
336
337 // Clock-driven path: every short segment aims at the path position for (now + STEP), measured
338 // from the real start time, so callback latency never accumulates (no drift, no stretch).
339 step(window) {
340 const STEP = 33; // ~2 frames per segment
341 const path = window.bmwPath, T = path.total, N = path.nodes.length - 1;
342 const now = Date.now() - window.bmwT0;
343 const end = path.nodes[N];
320 344 if (window.bmwMode == "min") {
321 if (window.bmwSeg >= N) return; // already in the tail
322 window.bmwSeg++;
323 const i = window.bmwSeg;
324 if (i == N) {
325 // arrived: invisible hold at the icon (scale 10%) until the shader animation ends
345 if (now >= T - 4) {
346 // arrived: invisible hold at the icon (scale 10%) until the shader animation is cancelled
326 347 window.bmwTailAnimation = animate({
327 348 window: window, curve: QEasingCurve.Linear, duration: 250,
328 animations: [{ type: Effect.Translation, from: {value1: nd[N].x, value2: nd[N].y}, to: {value1: nd[N].x, value2: nd[N].y} },
349 animations: [{ type: Effect.Translation, from: {value1: end.x, value2: end.y}, to: {value1: end.x, value2: end.y} },
329 350 { type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 0.1, value2: 0.1} },
330 351 { type: Effect.Opacity, from: 0.0, to: 0.0 }]
331 352 });
@@ -333,19 +354,30 @@ class BurnMyWindowsMarbleMinimizeEffect {
333 354 window.bmwMode = "tail";
334 355 return;
335 356 }
336 let extra = null;
337 if (i == N - 2) { // last 10% of arc length: 100% -> 10%
357 const tgt = Math.min(T, now + STEP);
358 const p = tgt >= T ? {x: end.x, y: end.y} : path.posAt(tgt);
359 if (!window.bmwScaling && tgt >= path.tScale) { // last 10% of arc length: 100% -> 10%
360 window.bmwScaling = true;
338 361 window.bmwScaleAnimation = animate({
339 window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, nd[N].t - nd[N - 2].t),
362 window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, T - now),
340 363 animations: [{ type: Effect.Scale, from: {value1: 1.0, value2: 1.0}, to: {value1: 0.1, value2: 0.1} }]
341 364 });
342 365 }
343 window.bmwSegAnimation = this.segAnim(window, nd[i], nd[i + 1], nd[i + 1].t - nd[i].t, extra);
366 window.bmwSegAnimation = this.segAnim(window, window.bmwCur, p, tgt - now);
367 window.bmwCur = p;
344 368 } else if (window.bmwMode == "rise") {
345 window.bmwSeg--;
346 const i = window.bmwSeg;
347 if (i <= 0) { window.bmwMode = null; return; }
348 window.bmwSegAnimation = this.segAnim(window, nd[i], nd[i - 1], nd[i].t - nd[i - 1].t);
369 if (now >= T - 4) {
370 if (window.bmwCur.x != 0 || window.bmwCur.y != 0) {
371 window.bmwSegAnimation = this.segAnim(window, window.bmwCur, {x: 0, y: 0}, 1);
372 window.bmwCur = {x: 0, y: 0};
373 }
374 window.bmwMode = null;
375 return;
376 }
377 const tgt = Math.min(T, now + STEP);
378 const p = tgt >= T ? {x: 0, y: 0} : path.posAt(T - tgt);
379 window.bmwSegAnimation = this.segAnim(window, window.bmwCur, p, tgt - now);
380 window.bmwCur = p;
349 381 }
350 382 }
351 383
@@ -228,7 +228,17 @@ class BurnMyWindowsMarbleMinimizeEffect {
228 const q = j < M ? [pts[j][0] + (pts[j + 1][0] - pts[j][0]) * f, pts[j][1] + (pts[j + 1][1] - pts[j][1]) * f] : pts[M];
228 const q = j < M ? [pts[j][0] + (pts[j + 1][0] - pts[j][0]) * f, pts[j][1] + (pts[j + 1][1] - pts[j][1]) * f] : pts[M];
229 nodes.push({x: q[0], y: q[1], t: animationTime(Math.round(1000 * Math.sqrt(2 * s / this.gravity)))});
229 nodes.push({x: q[0], y: q[1], t: animationTime(Math.round(1000 * Math.sqrt(2 * s / this.gravity)))});
230 }
230 }
231 return {nodes: nodes, length: L, total: nodes[N].t};
231 // continuous position at path time t (ms, already scaled by the animation factor)
232 const fac = Math.max(0.001, animationTime(1000) / 1000), g = this.gravity;
233 const posAt = (tms) => {
234 const ts = Math.max(0, tms) / 1000 / fac;
235 const sa = Math.min(L, 0.5 * g * ts * ts);
236 let lo = 0, hi = M;
237 while (hi - lo > 1) { const mid = (lo + hi) >> 1; if (cum[mid] <= sa) lo = mid; else hi = mid; }
238 const f = (sa - cum[lo]) / Math.max(1e-6, cum[hi] - cum[lo]);
239 return {x: pts[lo][0] + (pts[hi][0] - pts[lo][0]) * f, y: pts[lo][1] + (pts[hi][1] - pts[lo][1]) * f};
240 };
241 return {nodes: nodes, length: L, total: nodes[N].t, posAt: posAt, tScale: nodes[N - 2].t};
232 }
242 }
233
243
234 segAnim(window, a, b, dur, extra) {
244 segAnim(window, a, b, dur, extra) {
@@ -293,12 +303,13 @@ class BurnMyWindowsMarbleMinimizeEffect {
293 window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, T - nd[N - 2].t),
303 window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, T - nd[N - 2].t),
294 animations: [{ type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 1.0, value2: 1.0} }]
304 animations: [{ type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 1.0, value2: 1.0} }]
295 });
305 });
296 window.bmwPath = path; window.bmwSeg = N; window.bmwMode = "rise";
306 window.bmwPath = path; window.bmwMode = "rise";
297 window.bmwSegAnimation = this.segAnim(window, nd[N], nd[N - 1], nd[N].t - nd[N - 1].t);
307 window.bmwT0 = Date.now(); window.bmwCur = {x: nd[N].x, y: nd[N].y};
308 this.step(window);
298 }
309 }
299
310
300 stopAll(window) {
311 stopAll(window) {
301 window.bmwMode = null;
312 window.bmwMode = null; window.bmwScaling = false;
302 for (const k of ["bmwMinAnimation", "bmwMarbleDone", "bmwSegAnimation", "bmwScaleAnimation",
313 for (const k of ["bmwMinAnimation", "bmwMarbleDone", "bmwSegAnimation", "bmwScaleAnimation",
303 "bmwTailAnimation", "bmwUnminAnimation"]) {
314 "bmwTailAnimation", "bmwUnminAnimation"]) {
304 if (window[k]) { cancel(window[k]); delete window[k]; }
315 if (window[k]) { cancel(window[k]); delete window[k]; }
@@ -317,15 +328,25 @@ class BurnMyWindowsMarbleMinimizeEffect {
317 try { effects.addRepaintFull(); } catch (e) {}
328 try { effects.addRepaintFull(); } catch (e) {}
318 return;
329 return;
319 }
330 }
331 if (window.bmwMode == "min" && window.bmwSeg < 0) { // morph + pause done
332 window.bmwSeg = 0; window.bmwT0 = Date.now(); window.bmwCur = {x: 0, y: 0};
333 }
334 this.step(window);
335 }
336
337 // Clock-driven path: every short segment aims at the path position for (now + STEP), measured
338 // from the real start time, so callback latency never accumulates (no drift, no stretch).
339 step(window) {
340 const STEP = 33; // ~2 frames per segment
341 const path = window.bmwPath, T = path.total, N = path.nodes.length - 1;
342 const now = Date.now() - window.bmwT0;
343 const end = path.nodes[N];
320 if (window.bmwMode == "min") {
344 if (window.bmwMode == "min") {
321 if (window.bmwSeg >= N) return; // already in the tail
345 if (now >= T - 4) {
322 window.bmwSeg++;
346 // arrived: invisible hold at the icon (scale 10%) until the shader animation is cancelled
323 const i = window.bmwSeg;
324 if (i == N) {
325 // arrived: invisible hold at the icon (scale 10%) until the shader animation ends
326 window.bmwTailAnimation = animate({
347 window.bmwTailAnimation = animate({
327 window: window, curve: QEasingCurve.Linear, duration: 250,
348 window: window, curve: QEasingCurve.Linear, duration: 250,
328 animations: [{ type: Effect.Translation, from: {value1: nd[N].x, value2: nd[N].y}, to: {value1: nd[N].x, value2: nd[N].y} },
349 animations: [{ type: Effect.Translation, from: {value1: end.x, value2: end.y}, to: {value1: end.x, value2: end.y} },
329 { type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 0.1, value2: 0.1} },
350 { type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 0.1, value2: 0.1} },
330 { type: Effect.Opacity, from: 0.0, to: 0.0 }]
351 { type: Effect.Opacity, from: 0.0, to: 0.0 }]
331 });
352 });
@@ -333,19 +354,30 @@ class BurnMyWindowsMarbleMinimizeEffect {
333 window.bmwMode = "tail";
354 window.bmwMode = "tail";
334 return;
355 return;
335 }
356 }
336 let extra = null;
357 const tgt = Math.min(T, now + STEP);
337 if (i == N - 2) { // last 10% of arc length: 100% -> 10%
358 const p = tgt >= T ? {x: end.x, y: end.y} : path.posAt(tgt);
359 if (!window.bmwScaling && tgt >= path.tScale) { // last 10% of arc length: 100% -> 10%
360 window.bmwScaling = true;
338 window.bmwScaleAnimation = animate({
361 window.bmwScaleAnimation = animate({
339 window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, nd[N].t - nd[N - 2].t),
362 window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, T - now),
340 animations: [{ type: Effect.Scale, from: {value1: 1.0, value2: 1.0}, to: {value1: 0.1, value2: 0.1} }]
363 animations: [{ type: Effect.Scale, from: {value1: 1.0, value2: 1.0}, to: {value1: 0.1, value2: 0.1} }]
341 });
364 });
342 }
365 }
343 window.bmwSegAnimation = this.segAnim(window, nd[i], nd[i + 1], nd[i + 1].t - nd[i].t, extra);
366 window.bmwSegAnimation = this.segAnim(window, window.bmwCur, p, tgt - now);
367 window.bmwCur = p;
344 } else if (window.bmwMode == "rise") {
368 } else if (window.bmwMode == "rise") {
345 window.bmwSeg--;
369 if (now >= T - 4) {
346 const i = window.bmwSeg;
370 if (window.bmwCur.x != 0 || window.bmwCur.y != 0) {
347 if (i <= 0) { window.bmwMode = null; return; }
371 window.bmwSegAnimation = this.segAnim(window, window.bmwCur, {x: 0, y: 0}, 1);
348 window.bmwSegAnimation = this.segAnim(window, nd[i], nd[i - 1], nd[i].t - nd[i - 1].t);
372 window.bmwCur = {x: 0, y: 0};
373 }
374 window.bmwMode = null;
375 return;
376 }
377 const tgt = Math.min(T, now + STEP);
378 const p = tgt >= T ? {x: 0, y: 0} : path.posAt(T - tgt);
379 window.bmwSegAnimation = this.segAnim(window, window.bmwCur, p, tgt - now);
380 window.bmwCur = p;
349 }
381 }
350 }
382 }
351
383