我創建了一個js函數,它應該爲碰撞檢測旋轉圖像,但函數不能正確工作,當旋轉超過π* 1.5(Pi)時,圖像作物。在javascript中旋轉圖像的碰撞檢測
function rotateImage(image , rotation)
{
// Creates a canvas
var rotatedImage = document.createElement("canvas") . getContext("2d"),
// Setting a bounding box size
boundingWidth = Math . abs(image . height * Math . cos(rotation) + image . width * Math . sin(rotation)),
boundingHeight = Math . abs(image . height * Math . sin(rotation) + image . width * Math . cos(rotation));
// Changing canvas size
rotatedImage . canvas.width = boundingWidth;
rotatedImage . canvas.height = boundingHeight;
// Translating canvas
rotatedImage . translate(boundingWidth/2 , boundingHeight/2);
// Rotate canvas
rotatedImage . rotate(rotation);
// Un-translating canvas
rotatedImage . translate(-boundingWidth/2 , -boundingHeight/2);
// Draws image
rotatedImage . drawImage(image , 0 , 0);
// Returns canvas
return rotatedImage . canvas;
}
謝謝:)
出於好奇,如果旋轉是所有你需要爲什麼不使用CSS變換? –
如果您有權訪問HTML畫布,則可以訪問將爲您旋轉圖像的CSS動畫。 – runspired