基於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