Javascript 偏移相当于平移然后旋转

Javascript 偏移相当于平移然后旋转,javascript,canvas,html5-canvas,Javascript,Canvas,Html5 Canvas,目标是找出vx和vy必须是什么,以便下面的两个代码是等效的 //this is the behaviour I want ctx.translate(x,y); ctx.rotate(angle); ctx.fillRect(0,0,10,10); //find vx and vy so both this below is equivalent to above var vx = ?; var vy = ?; ctx.rotate(angle); ctx.fillRect(0 + vx, 0

目标是找出vx和vy必须是什么,以便下面的两个代码是等效的

//this is the behaviour I want
ctx.translate(x,y);
ctx.rotate(angle);
ctx.fillRect(0,0,10,10);

//find vx and vy so both this below is equivalent to above
var vx = ?;
var vy = ?;
ctx.rotate(angle);
ctx.fillRect(0 + vx, 0 + vy,10,10);

答案是将点反向旋转

Tk.rotatePt.x = function(x,y,rad_angle,anchorX,anchorY){
    return Math.cos(rad_angle) * (x-anchorX) - Math.sin(rad_angle) * (y-anchorY) + anchorX;
}
Tk.rotatePt.y = function(x,y,rad_angle,anchorX,anchorY){
    return Math.sin(rad_angle) * (x-anchorX) + Math.cos(rad_angle) * (y-anchorY) + anchorY;
}

var vx = Tk.rotatePt.x(x,y,-angle,0,0);
var vy = Tk.rotatePt.y(x,y,-angle,0,0);

命名{vx,vy}两个描述旋转偏移量的变量似乎让我感到困惑。