能否获取 Svelte 缓动动画的状态?

huangapple go评论47阅读模式
英文:

Is it possible to get the state of a svelte tweened motion?

问题

在Svelte中,当某物从一个经过缓动的状态移动到另一个状态时,我希望能够进一步突出显示运动,但最终仍回到之前的状态。

使用Svelte教程中的示例的一部分:

<script>
	const progress = tweened(0, {
		duration: 400,
		easing: cubicOut
	});
</script>

<progress value={$progress}></progress>

<button on:click={() => progress.set(0.25)}>
	25%
</button>

<button on:click={() => progress.set(0.5)}>
	50%
</button>

在400毫秒的持续时间内,我希望以不同的颜色显示进度条。因此,如果通常是蓝色的,我希望它在整个400毫秒内变为红色,然后恢复到原始颜色。

为此,我只需知道progress当前是否正在“缓动”(抱歉,不确定这里的词是什么)。例如:

<progress value={$progress} class:tweening={progress.tweening === true}></progress>

<style>
.tweening {
  /* 缓动时要应用的样式 */
}
</style>

这是否可以在不添加过多复杂性的情况下实现(例如,不手动检查HTML值是否每几毫秒更改一次)?

英文:

In Svelte, while something is moving from one tweened state to another, I want to further highlight the motion , but want to end up in the same state as before.

Using part of the example from the svelte tutorial:

&lt;script&gt;
	const progress = tweened(0, {
		duration: 400,
		easing: cubicOut
	});
&lt;/script&gt;

&lt;progress value={$progress}&gt;&lt;/progress&gt;

&lt;button on:click=&quot;{() =&gt; progress.set(0.25)}&quot;&gt;
	25%
&lt;/button&gt;

&lt;button on:click=&quot;{() =&gt; progress.set(0.5)}&quot;&gt;
	50%
&lt;/button&gt;

During the 400ms duration, I want to display the progress bar in a different color. So if it is usually blue, I want it to be red for the entire 400ms and then go back to its original color.

For this I would simply need to know if progress is currently "tweening" (sorry, not sure what the word here is) or not. So for example

&lt;progress value={$progress} class:tweening={progress.tweening === true}&gt;&lt;/progress&gt;

&lt;style&gt;
.tweening {
  /* my styles I want to apply while tweening */
}
&lt;/style&gt;

Is this possible without adding lots of complexity (e.g. not manually checking that the HTML value is changing every few ms)?

答案1

得分: 4

Svelte的tweened函数返回基本的Svelte存储。

您可以在动画进行时使用progress.subscribe(console.log)获取状态。在控制台中,您每次只会看到一个数字。是的,这是您可以读取的唯一数据。

但是(来自svelte.dev/tutorial/tweened

setupdate 方法都返回一个在缓动完成时解析的 promise。

因此,您只需等待 promise 解析,然后将进度条的颜色恢复为"正常"。

示例代码:

let tweening = false;
async function setProgress(v: number) {
    tweening = true;
    await progress.set(v)
    tweening = false;
}

或者 REPL

英文:

Svelte tweened function returns basic svelte store.

You can get the state while it's animating using progress.subscribe(console.log). In the console you will see one number only per log. Yes, that's the only data you can read.

But (from svelte.dev/tutorial/tweened)

> The set and update methods both return a promise that resolves when the tween completes.

So you can just wait until promise resolves and then change the color of your progressbar back to the "normal" one.

Code example:

let tweening = false;
async function setProgress(v: number) {
    tweening = true;
    await progress.set(v)
    tweening = false;
}

Or REPL

huangapple
  • 本文由 发表于 2023年3月1日 16:22:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定