Ответ 1
Вы можете использовать аннотацию ViewChild, чтобы захватить экземпляр вашего элемента canvas. После этого все ваниль js.
import {Component, View, Input, ViewChild, ElementRef} from 'angular2/core';
@Component({
selector: 'chess-diagram',
})
@View({
template: `<canvas #chessCanvas class='chess-diag'
[attr.width]='_size'
[attr.height]='_size'></canvas>`,
})
export class ChessDiagram {
private _size: number;
// get the element with the #chessCanvas on it
@ViewChild("chessCanvas") chessCanvas: ElementRef;
constructor(){
this._size = 150;
}
ngAfterViewInit() { // wait for the view to init before using the element
let context: CanvasRenderingContext2D = this.chessCanvas.nativeElement.getContext("2d");
// happy drawing from here on
context.fillStyle = 'blue';
context.fillRect(10, 10, 150, 150);
}
get size(){
return this._size;
}
@Input () set size(newValue: number){
this._size = Math.floor(newValue);
}
}
@ViewChild вернет ElementRef, вы можете получить родной canvas из этого свойства, используя свойство nativeElement.