0
處理繪製到html 5畫布的角度2項目。其主要功能是,用戶的鼠標位置被繪製到屏幕上,慢慢地消失(被由半透明的矩形覆蓋了)爲什麼fillStyle未定義?
現在,當我運行該腳本,我得到這個錯誤:
Cannot set property 'fillStyle' of undefined
我究竟做錯了什麼?
interface mouseInterface {
previousX: number;
previousY: number;
}
import {Injectable} from '@angular/core';
@Injectable()
export class CanvasService {
context: CanvasRenderingContext2D;
canvas: any = null;
init(canvas) {
this.canvas = canvas;
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.canvas.style = 'border:solid 1px red;'
this.context = canvas.getContext("2d");
this.fadeOut();
}
fadeOut() {
let ctx = this.context;
ctx.fillStyle = "rgba(255,255,255,.2)";
ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
setTimeout(this.fadeOut, 100);
}
draw(data) {
//does drawing behavior based on mouse position
}
}
要澄清,'fillStyle'不是未定義的,'ctx'是。 – CollinD
你確定init方法在fadeOut之前被調用嗎? – Polochon