Skip to content

Commit 5bfb9dc

Browse files
committed
Add play mode
1 parent d834128 commit 5bfb9dc

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

media/src/form-components/PlayButtons.svelte

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
play = () => {},
66
pause,
77
rew,
8+
clicker = () => {},
9+
playMode = 'loop',
810
// animate,
911
} = $props();
1012
</script>
@@ -32,6 +34,15 @@
3234
<button class="btn rewbtn" onclick={rew} aria-label="rewind">
3335
<i class="fa fa-fast-backward"></i>
3436
</button>
37+
<button class="btn modebtn" onclick={clicker} aria-label="play mode">
38+
{#if playMode == 'loop'}
39+
<i class="fa fa-retweet"></i>
40+
{:else if playMode == 'once'}
41+
<i class="fa fa-arrow-right"></i>
42+
{:else}
43+
<i class="fa fa-arrows-alt-h"></i>
44+
{/if}
45+
</button>
3546
</div>
3647

3748
<style>
@@ -43,4 +54,7 @@
4354
.btn {
4455
color: white;
4556
}
57+
.modebtn {
58+
width: 1rem;
59+
}
4660
</style>

media/src/objects/Point.svelte

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
selectObject,
4646
animate,
4747
onClose = () => {},
48+
playMode = 'bounce',
4849
} = $props();
4950
5051
let tau = $state(0);
@@ -106,6 +107,20 @@
106107
let isDynamic = $derived(dependsOn(params, 't'));
107108
let isDiscrete = $derived(dependsOn(params, 'n'));
108109
110+
/**
111+
* @type "once"|"loop"|"bounce"
112+
*/
113+
// let playMode = $state('loop');
114+
function playModeCycle() {
115+
if (playMode == 'once') {
116+
playMode = 'loop';
117+
} else if (playMode == 'loop') {
118+
playMode = 'bounce';
119+
} else {
120+
playMode = 'once';
121+
}
122+
}
123+
109124
$effect(() => {
110125
params;
111126
updatePoint();
@@ -213,9 +228,25 @@
213228
214229
// texString1 = `t = ${Math.round(100 * T) / 100}`;
215230
231+
let modeSign = 1;
232+
216233
const update = (dt = 0) => {
217-
tau += dt / (t1 - t0);
218-
if (tau > 1) tau %= 1;
234+
tau += (modeSign * dt) / (t1 - t0);
235+
if (tau > 1) {
236+
if (playMode == 'loop') {
237+
tau %= 1;
238+
} else if (playMode == 'once') {
239+
tau = 1;
240+
animation = false;
241+
} else {
242+
modeSign = -1;
243+
tau = 2 - tau;
244+
}
245+
}
246+
if (tau <= 0) {
247+
modeSign = 1;
248+
tau *= -1;
249+
}
219250
updatePoint(tVal);
220251
};
221252
// Start animating if animation changes (e.g. animating scene published)
@@ -308,6 +339,8 @@
308339
tau = 0;
309340
update();
310341
}}
342+
{playMode}
343+
clicker={playModeCycle}
311344
/>
312345
<!-- </div> -->
313346
{/if}

0 commit comments

Comments
 (0)