Я создал функцию 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