2017-02-21 57 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); 
} 

你可以做度到弧度的转换,我永远无法理解为什么每个人都希望这样做。