我创建了一个 js 函数,它应该旋转图像以进行碰撞检测,但是该函数不能正常工作,当旋转超过 π*1.5 (Pi) 时,图像会裁剪。
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;
}
谢谢 :)