충돌 감지를 위해 이미지를 회전해야하는 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 애니메이션에 액세스 할 수 있습니다. – runspired