Angular 5的Canvas drawImage没有显示

时间:2018-07-06 16:35:54

标签: angular typescript canvas

尝试使用drawImage将背景图像添加到画布,但未显示。我知道图像的路径是正确的,因为我可以做<img src="{{ imageName }}" />并且行得通。其他所有内容都可以在JavaScript中正常运行,但无法很好地转换为Angular。

HTML:

<canvas #canvas></canvas>

TypeScript:

import { Component, OnInit } from '@angular/core';
...

@Component({
  selector: 'app-box-picker',
  templateUrl: './box-picker.component.html',
  styleUrls: ['./box-picker.component.css']
})
export class BoxPickerComponent implements OnInit {
  @ViewChild('canvas') public canvas: ElementRef;

  canvasEl: any;
  ctx: CanvasRenderingContext2D;

  @Input() public width = 535.0;
  @Input() public height = 669.25;

  mousePosition: any;
  isMouseDown: boolean;
  dragoffx: number;
  dragoffy: number;

  circles: any;

  imageObj = new Image();
  imageName = "../../assets/pdf.png";

  constructor() {
  }

  ngOnInit() {

  }

  public ngAfterViewInit() {
    this.canvasEl = this.canvas.nativeElement;
    this.ctx = this.canvasEl.getContext('2d');

    this.canvasEl.width = this.width;
    this.canvasEl.height = this.height;

    this.imageObj.src = this.imageName;
    console.log(this.imageName);
    console.log(this.imageObj);
    // init circles
    var c1 = new Circle();
    c1.init(50, 50, 15, "rgba(217,83,79, .5)", "black", this.ctx);
    // console.log(c1);
    c1.out();

    this.circles = [c1];

    this.draw();

    this.captureDownEvents(this.canvasEl);
    this.captureMoveEvents(this.canvasEl);
    this.captureUpEvents(this.canvasEl);
  }

  draw() {
    //clear canvas
    this.ctx.clearRect(0, 0, this.canvasEl.width, this.canvasEl.height);
    this.ctx.drawImage(this.imageObj, 0, 0, this.canvasEl.width, this.canvasEl.height);
    this.drawCircles();
  }

  drawCircles() {
    for (var i = this.circles.length - 1; i >= 0; i--) {
      this.circles[i].draw();
    }
  }
...
}

当我做console.log(this.imageObj);时,有时会得到<img src="../../assets/pdf.png">,而有时我只会得到<img>。那有什么关系吗?

4 个答案:

答案 0 :(得分:1)

尝试使用以下代码:

组件HTML

at

Component.ts

beginShareFlow

最后,您的所有代码都可以。只需使用Element Ref来实现加载图像。

答案 1 :(得分:1)

@Kaiido是正确的。我需要等待图像加载。我一直使用setTimeout(e => this.draw(), 500);直到找到更好的解决方案。

答案 2 :(得分:1)

在您的javascript(或打字稿代码)中,您可以像这样解决图像超时问题:

var ctx = element.getContext("2d");
var img = new Image();
img.onload = function() {
    ctx.drawImage(img, 0, 0, img.width, img.height);
    // do other canvas handling here!
}
img.src = "assets/images/image.png";

此功能可在HTML5上100%正常运行,但尚未尝试将其与ts文件一起使用。希望这会有所帮助!

答案 3 :(得分:0)

您也可以将 onload 包裹在 Promise 中并为此使用 async/await 模式:

export async function loadImage(src: string): Promise<HTMLImageElement> {
    const image = new Image();
    image.src = src;
    return new Promise(resolve => {
        image.onload = (ev) => {
            resolve(image);
        }
    });
}

用法:

async drawImage() {
    const image: HTMLImageElement = await loadImage('assets/pdf.png');
    this.ctx = this.canvas.nativeElement.getContext('2d');
    this.ctx.drawImage(image, 0, 0, image.width, image.height);
}