2012-11-02 5 views
2

이미지가 있습니다. 각도로 회전 한 다음 한 지점에서 캔버스로 그려야합니다. 나는 현재이 있습니다HTML5 Canvas에서 이미지 회전

var image = roach.image; 
image.style.msTransform = 'rotate(' + roach.heading + 'deg)'; 
this.gameContext.drawImage(image, roach.position.x, roach.position.y); 

내가 roach.heading 내가 각도로 회전 할 내 각이고 그것이 작동되도록하려면이 옵션을 편집 할 수있는 방법.

답변

1

이 시도 :

var image = roach.image, 
    ctx = this.gameContext, 
    widthHalf = Math.floor(image.width/2), 
    heightHalf = Math.floor(image.height/2); 

ctx.save(); 

ctx.translate(roach.position.x, roach.position.y); 
ctx.translate(widthHalf, heightHalf); 
ctx.rotate((Math.PI/180) * roach.heading); 
ctx.drawImage(image, -widthHalf, -heightHalf); 
ctx.restore(); 

jsfiddle : http://jsfiddle.net/PwzEc/

+0

을 일했다! 감사! 그냥 여기에서 이해합니다. 게임 컨텍스트를 var에 할당 한 이유가 있습니까? – nat45928

+1

@ nat45928 항상'this.gameContext'를 타이핑하는 것을 피하기 위해'ctx'를 6 번 사용하여 많은 타이핑과 텍스트를 줄일 수 있습니다 – Esailija

관련 문제