如何获取canvas元素的上下文?这一行
DynamicContext = DynamicCanvas.getContext("2d"); // ERROR
抛出此错误:
TypeError: DynamicCanvas.getContext is not a function
DynamicContext = DynamicCanvas.getContext("2d");
源代码
HTML
script type="text/x-handlebars">
{{view "App.Canvas"}}
</script>
Ember.js
App.Canvas = Em.View.extend({
//attrs for canvas element
tagName: "canvas",
attributeBindings: ['height', 'width'],
height: 300,
width: 300,
faceRadius: 40,
didInsertElement: function(){
this.drawStaticCanvas();
this.drawDynamicCanvas();
},
drawDynamicCanvas: function(){
// How to get the context?
DynamicContext = DynamicCanvas.getContext("2d"); // ERROR
// face
DynamicContext.beginPath();
DynamicContext.arc(300/2, // x x,y is at the center
300/2, // y
100, // arc radius
0, // starting angle
(360 * Math.PI)/180, // ending angle
true); // counter-clockwise
DynamicContext.fillStyle = "#ffddee";
DynamicContext.fill();
DynamicContext.stroke();
}
});