2013-06-14 1 views
3

안녕하세요 Ember View에서 캔버스 '2D'컨텍스트를 얻는 방법은 무엇입니까? 다음 코드는 작동하지 않습니다. dom의 캔버스 요소를 가져 오지만 '2d'의 컨텍스트는 가져 오지 않습니다. 귀하의 예를 다음Ember.js and Canvas

App.Canvas = Ember.View.extend({ 
tagName: 'canvas', 
quader: function() { 
     return this.get('controller.model'); 
}.property('controller.model'), 
didInsertElement: function(){ 

    var canvas = this.$('#id'); // get the Element 
    var ctx = canvas.getContext('2d'); // --> get Error 

    // Filled triangle 
    ctx.beginPath(); 
    ctx.moveTo(25,25); 
    ctx.lineTo(105,25); 
    ctx.lineTo(25,105); 
    ctx.fill(); 

    // Stroked triangle 
    ctx.beginPath(); 
    ctx.moveTo(125,125); 
    ctx.lineTo(125,45); 
    ctx.lineTo(45,125); 
    ctx.closePath(); 
    ctx.stroke(); 
} 
}); 

답변

4

하는 엠버보기 내부에서 tagName에 의해 정의 된 요소에 액세스하는 방법과 같이 할 것입니다 :

가의 tagName 속성에 정의 된 DOM 요소 GET있어
var canvas = this.get('element'); 
var ctx = canvas.getContext('2d'); 

this.get('element') 보기, 귀하의 경우에 canvas 요소

여기서 작동 jsbin.

희망 하시겠습니까?

+0

감사합니다. 그래, 그것은 위대한 작품 :) – abuder