2017-02-21 60 views
-1

我正在尋找一個函數,它在畫布上繪製一個帶有函數的正方形,即drawSquare();其中帶有傾斜正方形的參數「degrees」。我不要想要傾斜畫布。謝謝,如果您有任何問題,請發表評論。旋轉正方形函數JS Canvas

回答

2

無需傾斜帆布

function drawRotatedSquare (x, y, width, height, rotate) { // rotate in radians 
    ctx.setTransform(1, 0, 0, 1, x, y); // overwrite existing transform 
    ctx.rotate(rotate); 
    ctx.fillRect(-width/2, -height/2, width, height); 
} 

你可以做度到弧度的轉換,我永遠無法理解爲什麼每個人都希望這樣做。