blazor支持html5吗,asp.net core - GC_minor issue on Blazor html5 canvas and c# timer - Stack Overflow...

I could reproduce the same issue:

The above comment by @enet is right. You should at least check whether the firstRender is true and avoid attaching the draw() function to a new timer each time.

Apart from that, you should create a Timer as a separated field. And don't forget to DISPOSE the Timer when the component is disposed.

Finally, there's another issue within your code: You didn't invoke the _context.EndBatchAsync(). As a result, it will NOT draw anything.

To fix the above issues, change your code as below:

...

@implements IDisposable @* A Timer is a resource that should be disposed *@

...

@code{

private Canvas2DContext _context;

protected BECanvasComponent _canvasReference;

private Timer _aTimer = new Timer(500);

public void Dispose()

{

this._aTimer.Dispose();

}

protected override async Task OnAfterRenderAsync(bool firstRender)

{

this._context = await this._canvasReference.CreateCanvas2DAsync();

var centerX = _canvasReference.Width / 2;

var centerY = _canvasReference.Height / 2;

var radius = 80;

var full = radius * 2;

var amount = 0.0;

var amountToIncrease = 0.1;

if(firstRender)

{

// you might decide to put the above `var amount = 0.0; ...` here too, it depends on your needs

this._aTimer.Elapsed += draw;

this._aTimer.AutoReset = true;

this._aTimer.Enabled = true;

}

async void draw(Object source, ElapsedEventArgs e)

{

await this._context.BeginBatchAsync();

await this._context.ArcAsync(centerX, centerY, radius, 0, amount * Math.PI, false);

await this._context.SetFillStyleAsync("#13a8a4");

await this._context.FillAsync();

await this._context.SetLineWidthAsync(10);

await this._context.SetStrokeStyleAsync("#000000");

await this._context.StrokeAsync();

await this._context.EndBatchAsync(); // add this line !

amount += amountToIncrease;

if (amount > full) amount = 0; // restart

await InvokeAsync(() => { StateHasChanged(); });

}

}

}

Demo

fFtzz.gif

[Edit]

i am wondering why there are those lines inside the circle?

Those lines are the part of closed path last time. Actually, there's a beginPath api that starts a new path by emptying the list of sub-paths.( See docs on MDN). In other words, if you don't need them, invoke _context.BeginPathAsync(); :

await this._context.BeginBatchAsync();

await this._context.BeginPathAsync(); // add this line

await this._context.ArcAsync(centerX, centerY, radius, 0, amount * Math.PI, false);

...

[Demo2] :

CyEg6.gif

By the way, you don't have to notify the state has changed here (because you didn't change the component state):

//await InvokeAsync(() => { StateHasChanged(); });

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值