<tfoot id='EEjHN'></tfoot>
    <bdo id='EEjHN'></bdo><ul id='EEjHN'></ul>

        <legend id='EEjHN'><style id='EEjHN'><dir id='EEjHN'><q id='EEjHN'></q></dir></style></legend>
      1. <small id='EEjHN'></small><noframes id='EEjHN'>

        <i id='EEjHN'><tr id='EEjHN'><dt id='EEjHN'><q id='EEjHN'><span id='EEjHN'><b id='EEjHN'><form id='EEjHN'><ins id='EEjHN'></ins><ul id='EEjHN'></ul><sub id='EEjHN'></sub></form><legend id='EEjHN'></legend><bdo id='EEjHN'><pre id='EEjHN'><center id='EEjHN'></center></pre></bdo></b><th id='EEjHN'></th></span></q></dt></tr></i><div id='EEjHN'><tfoot id='EEjHN'></tfoot><dl id='EEjHN'><fieldset id='EEjHN'></fieldset></dl></div>
      2. 如何在 Angular 中使用画布

        时间:2023-06-20
        <i id='kx0ki'><tr id='kx0ki'><dt id='kx0ki'><q id='kx0ki'><span id='kx0ki'><b id='kx0ki'><form id='kx0ki'><ins id='kx0ki'></ins><ul id='kx0ki'></ul><sub id='kx0ki'></sub></form><legend id='kx0ki'></legend><bdo id='kx0ki'><pre id='kx0ki'><center id='kx0ki'></center></pre></bdo></b><th id='kx0ki'></th></span></q></dt></tr></i><div id='kx0ki'><tfoot id='kx0ki'></tfoot><dl id='kx0ki'><fieldset id='kx0ki'></fieldset></dl></div>

            <legend id='kx0ki'><style id='kx0ki'><dir id='kx0ki'><q id='kx0ki'></q></dir></style></legend>

                  <tbody id='kx0ki'></tbody>
              1. <tfoot id='kx0ki'></tfoot>

                <small id='kx0ki'></small><noframes id='kx0ki'>

                • <bdo id='kx0ki'></bdo><ul id='kx0ki'></ul>

                  本文介绍了如何在 Angular 中使用画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 javascript 中使用画布的常见方法是:

                  The common approaching to use canvas in javascript is like :

                  var canvas = document.getElementById('tutorial');
                  var ctx = canvas.getContext('2d');
                  

                  但在 Angular2 中我无法获取 HTMLCanvasElement 对象,var "canvas" 只能获取 Angular2 中的 HTMLElement.那么如何在Angular2中使用canvas呢?还有,如何在Angular2中使用TypeScript语言中的第三方javascript?

                  but in Angular2 I cannot get the HTMLCanvasElement object, the var "canvas" only get the HTMLElement in Angular2. So how to use canvas in Angular2? And furthermore, how to use the third-party javascript in Angular2 with the language TypeScript?

                  推荐答案

                  您可以使用 @ViewChild

                  在您的班级中执行以下操作.

                  In your class do the following.

                  import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
                  
                  @Component({
                     name: 'my-component',
                     // notice the variable name myCanvas
                     template: `<canvas #myCanvas></canvas>`
                  })
                  export class myComponent implements AfterViewInit {
                    // its important myCanvas matches the variable name in the template
                    @ViewChild('myCanvas')
                    myCanvas: ElementRef<HTMLCanvasElement>;
                  
                    public context: CanvasRenderingContext2D;
                  
                    ngAfterViewInit(): void {
                      this.context = this.myCanvas.nativeElement.getContext('2d');
                    }
                  }
                  

                  尽量不要使用 document,因为从长远来看它可能会咬你一口.在编译应用程序后,使用 @ViewChild 也比查询 DOM 更有优势.Angular 已经提前知道它需要对哪个元素进行修改,而不必在 DOM 中找到它.

                  Try to stay away from using document as much as you can, as it could bite you on the long run. Also using @ViewChild has an advantage over querying the DOM, once the application is compiled. Angular already knows ahead of time which element it needs to do the modifications on, rather than having to find it in the DOM.

                  有关完整示例,请查看此 演示

                  For a full example check out this demo

                  更新

                  对于 Angular 8,您需要像这样使用 ViewChild.

                  For angular 8 you need to use ViewChild like this.

                  @ViewChild('myCanvas', {static: false}) myCanvas: ElementRef;
                  

                  请参阅 如何我在 Angular 8 中为 @ViewChild 使用了新的静态选项?了解更多信息

                  这篇关于如何在 Angular 中使用画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将图像存储和检索到localStorage? 下一篇:在画布中绘制 1px 粗线会创建 2px 粗线

                  相关文章

                    <bdo id='EkiPQ'></bdo><ul id='EkiPQ'></ul>

                  <legend id='EkiPQ'><style id='EkiPQ'><dir id='EkiPQ'><q id='EkiPQ'></q></dir></style></legend>

                    <i id='EkiPQ'><tr id='EkiPQ'><dt id='EkiPQ'><q id='EkiPQ'><span id='EkiPQ'><b id='EkiPQ'><form id='EkiPQ'><ins id='EkiPQ'></ins><ul id='EkiPQ'></ul><sub id='EkiPQ'></sub></form><legend id='EkiPQ'></legend><bdo id='EkiPQ'><pre id='EkiPQ'><center id='EkiPQ'></center></pre></bdo></b><th id='EkiPQ'></th></span></q></dt></tr></i><div id='EkiPQ'><tfoot id='EkiPQ'></tfoot><dl id='EkiPQ'><fieldset id='EkiPQ'></fieldset></dl></div>
                    <tfoot id='EkiPQ'></tfoot>

                  1. <small id='EkiPQ'></small><noframes id='EkiPQ'>