0

I want to rotate an image IN javascript. The variable used for the image in javascript is heroImage. I am using jquery rotate, it works fine only when rotating images in html, but not in javascript. Normally it looks something like this

    $("#image").rotate(angle);

But that only works for images created in html.

This is part of the code im currently using

var heroImage = new Image();
heroImage.src = "images/hero.png";

and its drawn of canvas , if that helps. the problem is #image can only refer to a html div element afaik.

3
  • 2
    what have you tried so far? Have you some sample code of what you have tried? Commented Aug 19, 2013 at 11:15
  • Draw it onto a canvas. Commented Aug 19, 2013 at 11:16
  • maybe try raphael. Not too sure what you're actually looking for though, you should clarify. How is your image defined in javascript, as a bitmap, or are you using a library helper function? Commented Aug 19, 2013 at 11:16

1 Answer 1

2

If the heroImage variable is a string that contains a file name, just create a new image and append it to the DOM before calling rotate(), f.ex:

var heroImage = 'one.jpg';
var image = new Image();
image.src = heroImage;
$('#image').append(image).rotate(angle);

EDIT If the #image element is supposed to be the IMG itself, try something like:

var heroImage = 'one.jpg';
var image = new Image();
image.src = heroImage;
document.body.appendChild(image); // or wherever you want to put it
$(image).rotate(angle);
Sign up to request clarification or add additional context in comments.

2 Comments

So, in this case #image is a div or similar, not an <img/>, correct?
I assumed so, but it’s hard to tell from the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.