2

I want to rotate an image in 3D and this is possible in css with

transform: perspective(50px) rotateX(10deg) rotateY(10deg) rotateZ(10deg);

Currently I am using a canvas and the .style.transform property does not work for me. Example: https://jsfiddle.net/chung9ey/31/ . Note: I don't want to have a css file or style tags at all.

2

1 Answer 1

1

Your code is basically correct, but instead of img.style.transform, transform the canvas with canvas.style.transform:

var img = new Image;
var canvas = document.getElementById("hello");
var context = canvas.getContext("2d");

img.onload = function(){
	canvas.style.transform = "perspective(50px) rotateX(10deg) rotateY(10deg) rotateZ(10deg)";
	context.drawImage(img, 0 ,0, 500, 300);	
}

img.src = "https://www.enterprise.com/content/dam/global-vehicle-images/cars/CHRY_200_2015.png";
<canvas id = "hello" width = "500px" height = "300px" style="border: 1px solid BLACK";></canvas>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.