【问题标题】:Why my canvas drawImage not showing up in angular2 component为什么我的画布 drawImage 没有出现在 angular2 组件中
【发布时间】:2017-07-16 09:34:56
【问题描述】:

我正在写一个画布,它在 angular2 的组件中绘制图像。

这里是 canvas.component.ts

export class BoardComponent implements  OnInit,  AfterViewInit, AfterViewChecked {

  @ViewChild("visualization") visualization: ElementRef;
  @ViewChild('img') img: ElementRef;

  private context: CanvasRenderingContext2D;
  private element: HTMLImageElement;

  src: string;
  width: number
  height: number

  constructor() {
    this.src = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
  }

  ngOnInit() {
    this.width = 400;
    this.height = 400;
  }

  ngAfterViewInit() {
    this.context = this.visualization.nativeElement.getContext("2d");
    this.element = this.img.nativeElement;
  }

  ngAfterViewChecked() {
    this.context.clearRect(0, 0, this.width, this.height);
    console.log('drawImage');
    // this prints an image element with src I gave
    console.log(this.element);
    this.context.drawImage(this.element, 0, 0, this.width, this.height);
  }
}

这是模板

<img #img src="{{src}}" style='display: none;' />
<canvas #visualization width="{{width}}" height="{{height}}"></canvas>`

看起来图像已经加载,但我的画布仍然是空的。

我发现了很多类似的问题,但没有一个使用 angular2。

例如,drawImage() not working 的回答是,您应该等到图像加载完毕。

但我不知道如何以角度方式检查这种情况。

有什么建议吗?

谢谢。

编辑

我发现可能是因为我渲染到画布上时图片没有加载。

我添加以下代码并调整页面大小,画布确实出现了。

@HostListener('window:resize')
resize() {
    this.render();
  }
render() {
    this.context.clearRect(0, 0, this.width, this.height);
    this.context.drawImage(this.element, 0, 0, this.width, this.height);
 }

如果是这个原因,问题就变了,向 angular2 中的 html 元素添加事件监听器(如 img.onLoad)的正确方法是什么?

【问题讨论】:

标签: javascript html angular canvas


【解决方案1】:

在这里,我调整了您的代码以使其正常工作。

HTML

  <img #img src="{{src}}" (load)="afterLoading()" style='display: none;' /> 
  <canvas #visualization width={{imgWidth}} height={{imgHeight}} "></canvas>

打字稿

export class BoardComponent implements  implements AfterViewInit {

  @ViewChild("visualization") visualization: ElementRef;
  @ViewChild('img') img: ElementRef;

  private context: CanvasRenderingContext2D;
  private element: HTMLImageElement;

  src: string;
  imgWidth: number;
  imgHeight: number;

  constructor() {
    this.imgWidth = 400;
    this.imgHeight = 400;
    this.src = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
  }

  ngAfterViewInit() {
    this.context = this.visualization.nativeElement.getContext("2d");
    this.element = this.img.nativeElement;
  }


  afterLoading(){
    this.context.clearRect(0, 0, this.imgWidth, this.imgHeight);
    console.log('drawImage');
    // this prints an image element with src I gave
    console.log(this.element);
    this.context.drawImage(this.element, 0, 0, this.imgWidth, this.imgHeight);
  }
}

这是工作的DEMO

【讨论】:

  • OP 正在画布上绘制 img 元素,不需要为此显示图像元素。
  • 好吧,您的回答绝对不能回答问题,因此“没有帮助”,这是投反对票的理由。
  • HTMLImageElement 不需要显示就可以在 HTMLCanvasELement 上绘制它。问题很清楚:如何等待图像以角度加载,以便 CanvasRenderingContext2D#drawImage 方法可以使用它。此方法不关心 CSS。您甚至可以绘制一个未附加到文档的 HTMLImageElement。你的回答没有回答问题,我觉得你不明白什么是HTMLCanvasElement,或者问题中的“canvas”。
  • 我在问题的 cmets 中提到的链接中的 复制意大利面?很高兴我能够在一个我一无所知的库中挖掘出一些东西。
  • 你是说copypasta?
猜你喜欢
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 2020-06-08
  • 2021-02-24
  • 2017-03-19
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多