gpt4 book ai didi

animation - 使用等效的 Javascript SetTimeOut() 函数在 Blazor .net 上执行动画

转载 作者:行者123 更新时间:2023-12-02 19:30:44 24 4
gpt4 key购买 nike

问题:过去我已经能够在 Javascript 中使用 SetimeOut 函数制作动画。这是球落下的代码:

var ball = <HTMLElement>document.getElementById("ball");
ball.setAttribute("style", "fill:blue");

var inc2: number = 0;
var timet = document.getElementById("timeT");
timet.setAttribute("y", "420");
//time=200 ms // 1m is 3780 pixel // 400 pixels are 2m / ball is falling from 2m // g=10m/s
fallt2();
var timet2 = document.getElementById("timeT2");
timet2.setAttribute("y", "440");
var tiem = new Date();
var mili = tiem.getMilliseconds();
var dated = Date.now();
function fallt2() {
//while (inc2 <= 400) {
let ii = 0; //200 ms in 8 then 25 ms // 400 px high
//let slow = 3;

for (; inc2 <= 400; inc2 += 2)//px //10
{

goodST2(inc2, ii);
ii += 25;
}
}

function goodST2(i: number, ii: number) {
setTimeout(() => {
ball.setAttribute("transform", `translate(0 ${i})`);
var newdate = Date.now();
var newdiff = newdate - dated;
mili += 25;
timet.innerHTML = mili.toString();

timet2.innerHTML = newdiff.toString();
}, ii);
}

所以请帮助我一个 .net 标准函数,例如 Theading.sleep 在 Blazor 中执行动画。 谢谢,

这是我尝试过的:

@*<svg width="12cm" height="4cm" viewBox="0 0 1200 400">*@
@*<button class="btn btn-primary" @onclick="transformar">Ver caer</button>*@
<button class="btn btn-primary" @onclick="tt">Ver caer</button>
<div>
<svg viewBox="0 0 100 100">
@*<circle cx="10" cy="@c1" r="5" fill="blue" stroke="black" stroke-width="0.2" transform="translate(10,@c1)">*@
<circle cx="10" cy="8" r="5" fill="blue" stroke="black" stroke-width="0.2" transform="translate(10,@c1)">
@*<animate attributeName="cy" attributeType="XML" begin="0s" dur="4s" from="8" to="28" fill="remove" />*@
</circle>

<rect x="10" y="25" width="4" height="4" fill="red" stroke="black" stroke-width="0.5">
@*<animate attributeName="rx" values="0;5;0" dur="10s" repeatCount="indefinite"></animate>*@
</rect>
</svg>
</div>
</div>
@{tt(); }
@code {
private void transformar()
{
c1++;
}
// Demonstrates how a parent component can supply parameters
[Parameter]
public string Saludos { get; set; }
string Saludos2 = "Hola Diego";
public bool bA { get; set; }
[Parameter] public EventCallback<bool> TransitionEnded { get; set; }
int c1 = 8;

void tt()
{
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 500;
t.AutoReset = true;
t.Enabled = true;
t.Start();
for (var i = 0; i < 16; i++)
{
t.Elapsed += ett;

}
t.Stop();
}

public void ett(Object source, System.Timers.ElapsedEventArgs e)
{
caer2();
}

void caer2()
{ c1++; }
//SetTimer(c1);
//public int c1 { get; set; } = 8;
//for(var i=10;i<22;i++>)
// {
// }
private static System.Timers.Timer aTimer;
private static void SetTimer(int c1)
{
aTimer = new System.Timers.Timer(500);
//aTimer.Elapsed += caer(c1); //OntimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
//private static void caer(Object source, System.Timers.ElapsedEventArgs e, int s)
private void caer()
{
int dormir = 0;
for (var i=0;i<16;i++)
{
c1++;
//SurveyPrompt2.c1 c1++;
//System.Timers.Timer(500);
//Task.Run(()=>CaerAnimate());
//System.Threading.Thread.Sleep(200);
//esperar(dormir+200);
//Task.WaitAll(Task.Delay(100));

}
}
void esperar(int d)
{
System.Threading.Thread.Sleep(d);
}

async Task CaerAnimate()
{
bA = !bA;
await Task.Delay(10600);
await TransitionEnded.InvokeAsync(bA);
//var tcs = new TaskCompletionSource<bool>();
//DateTime endT = DateTime.Now.AddMilliseconds(3000);
//while (DateTime.Now<endT)
//{
//await Task.Delay(10000000);
//c1++;
//}
//return tcs.Task;
//await System.Threading.Thread.Sleep(100);

}


}

我尝试了计时器,但不起作用或者我不知道如何实现。提前致谢。

最佳答案

我删除了所有额外的代码以保持干净

<button class="btn btn-primary" @onclick="tt">Ver caer</button>
<div>
<svg viewBox="0 0 100 100">
<circle cx="10" cy="8" transform="translate(10,@c1)" r="5" fill="blue" stroke="black" stroke-width="0.2" >
</circle>

<rect x="10" y="25" width="4" height="4" fill="red" stroke="black" stroke-width="0.5">
</rect>
</svg>
</div>
@code {
int c1 = 8;

private static System.Timers.Timer aTimer;
void tt()
{
if (aTimer is null)
{
aTimer = new System.Timers.Timer(500);
aTimer.Elapsed += (sender,args) => { c1 = c1 > 16 ? 8 : c1+1; InvokeAsync(StateHasChanged); };
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
else
{
aTimer.Stop();
aTimer.Dispose();
aTimer = null;
}
}

}

您可以使用计时器,但在 Elapsed 事件中,您需要调用 StateHasChanged 来标记要渲染的组件 - 您应该通过 执行此操作>InvokeAsync 以确保它发生在渲染器上下文中。

在这里查看它的实际情况:https://blazorfiddle.com/s/9jbd8f34

关于animation - 使用等效的 Javascript SetTimeOut() 函数在 Blazor .net 上执行动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61837589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com