我有一个奇怪的问题,当我通过文本角度值执行旋转时(我获取控制台打印的上一个角度并将其赋值给变量),它能完美地工作。但是如果我像下面代码中那样传递一个变量,图像会被画得“更靠上和更靠左”(抱歉我的英语不好)。
function setImg(src,x,y,angle)
{
var TO_RADIANS = Math.PI/180;
var base_image = new Image();
base_image.src = src
base_image.onload = function () {
console.log("-->->"+parseFloat(angle))
ctx.save(); //saves the state of canvas
ctx.translate(x+(base_image.width/2), y+(base_image.height/2)); //let's translate
ctx.rotate(angle*TO_RADIANS); //increment the angle and rotate the image
ctx.drawImage(base_image, -(base_image.width/2),-(base_image.height/2)); //draw the image ;)
ctx.restore(); //restore the state of canvas
};
}
谢谢!
base_image
是一个全局变量。如果没有涉及到该变量的声明和其他使用方式的代码,我们无法提供帮助,因为代码中没有其他问题。 - Blindman67ctx.transform
、ctx.rotate
、ctx.scale
和ctx.translate
通过乘以现有的变换来工作。尝试在ctx.save
后使用ctx.setTransform(1,0,0,1,0,0)
将变换重置为默认值,这将解决问题。 - Blindman67