基于this Stack Overflow answer,改变操纵画布您的JS发送至:
var img = new Image();
var canvas = document.getElementById("hello");
var context = canvas.getContext("2d");
img.onload = function(){
context.save(); // Saves current canvas state (includes transformations)
context.rotate(30); // This rotates canvas context around its top left corner...
context.translate(-275, 125); // ...so you need to make a correction.
context.drawImage(img, 0, 0, 500, 300);
context.restore(); // Restore canvas state
};
img.src = "https://www.enterprise.com/content/dam/global-vehicle-images/cars/CHRY_200_2015.png";
T在绘制图像之后,他基本上旋转画布的内容。
由于此旋转,图像的一部分是非画布的,因此您可能还需要缩放画布以适应旋转的图像。
https://jsfiddle.net/Lcyksjoz/
你需要比其它数值CSS长度值的单元'0' – dandavis
它应该是'context.drawImage(IMG,topLeftCornerHorizontalPosition,topLeftCornerVerticalPosition,imageWidth,imageHeight);'。请参阅https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage – jkdev
至于向画布添加旋转图像,可能无法使用'img.style.transform'。请参阅[旋转并保存JavaScript的图像](http://stackoverflow.com/q/34080762/3345375)。 – jkdev