【发布时间】:2013-04-13 14:50:49
【问题描述】:
我创建了一个 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;
}
谢谢:)
【问题讨论】:
-
出于好奇,如果您只需要旋转,为什么不使用 CSS 变换?
-
如果您可以访问 html 画布,则可以访问为您旋转图像的 css 动画。
标签: html image math canvas rotation