如何从Blazor访问HTML Canvas? [英] How do I access the HTML Canvas from Blazor?

查看:754
本文介绍了如何从Blazor访问HTML Canvas?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Blazor到HTML Canvas进行2D绘图. 我尝试使用Blazor.Extensions.Canvas,但它似乎已经过时,并且针对的是以前的Blazor服务器端实现.

I am trying to do 2D drawing to the HTML Canvas from Blazor. I tried using the Blazor.Extensions.Canvas but it seems to be quite dated and targeted at a prior Blazor Server Side implementation.

我尝试实现一个简单的示例,但出现了异常

I tried implementing a simple example and got an exception

有...

{
protected Blazor.Extensions.BECanvasComponent _canvasRef;
private Blazor.Extensions.Canvas.Canvas2D.Canvas2DContext _context;

// Note I had to provide the full path since it seemed to ignore
//  my @using statement in most cases

    protected override async Task OnAfterRenderAsync()
    {
      this._context = await this._canvasRef.CreateCanvas2DAsync();
      await this._context.SetFillStyleAsync("green");

      await this._context.FillRectAsync(10, 100, 100, 100);

      await this._context.SetFontAsync("48px serif");
      await this._context.StrokeTextAsync("Hello Blazor!!!", 10, 100);
    }
}

我什至不得不在生成的文件中添加canvas类的完整路径. 但是,我终于在没有警告的情况下构建了它. 我希望它会绘制绿色填充的矩形,但是...

I even had to add the full path for the canvas classes in the generated files. However, I finally got it to build without warnings. I expected it to draw the green filled rectangle but ...

这在浏览器中创建了一个未处理的异常呈现组件": "Microsoft.JSInterop.JSException:t.getContext不是函数"

This created an 'unhandled exception rendering component ' in the browser: 'Microsoft.JSInterop.JSException: t.getContext is not a function'

引用该行: 'this._context =等待this._canvasRef.CreateCanvas2DAsync();'

referencing the line: ' this._context = await this._canvasRef.CreateCanvas2DAsync();'

推荐答案

我相信.net核心版本和canvas版本的兼容性问题. (我在使用.net Core Preview 6和canvas 0.1.9时遇到了同样的问题). 目前,他们发布了Canvas 0.2.0,因此它应与.core预览版7配合使用.

I believe the problem in compatibility of .net core version and canvas version. (I had same problems with .net core preview 6 and canvas 0.1.9). Currently they released Canvas 0.2.0, so it should work with .core preview 7.

这篇关于如何从Blazor访问HTML Canvas?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆