【发布时间】:2015-09-11 17:09:36
【问题描述】:
我创建了以下绘图函数:
function draw(ctx, scale, angle) {
// DRAW SHAPE:
var rotation = (Math.PI / 180) * angle;
var h = scale * (Math.sqrt(3) / 2);
var radius = h / 4;
ctx.fillStyle = '#BE1942';
ctx.strokeStyle = "#FF2159";
ctx.lineJoin = "round";
ctx.lineWidth = radius;
ctx.save();
ctx.translate(getCX(), getCY());
ctx.rotate(rotation);
ctx.beginPath();
ctx.moveTo(0, -h / 2);
ctx.lineTo(-scale / 2, h / 2);
ctx.lineTo(scale / 2, h / 2);
ctx.lineTo(0, -h / 2);
ctx.closePath();
ctx.stroke();
ctx.fill();
// RESET:
ctx.restore();
// FINALIZE:
ctx.save();
}
我注意到,当我旋转这个三角形时,它不会围绕真正的中心旋转,而是在偏移处旋转。有没有一种简单的方法让它围绕真正的中心旋转?我认为它应该已经这样做了,因为我是从中心坐标绘制它的。
【问题讨论】:
-
旋转前需要先平移旋转中心,然后再恢复,设置fiddle