• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Rotate an Image

Using JavaScript to Rotate an Image

February 14, 2022 Leave a Comment

We can use JavaScript to rotate an image easily by targeting the Style transform property and using the getElementById() method.

document.getElementById("image1").style.transform = "rotate(20deg)";

In the code above, you can change “20deg” to any degree amount you want to rotate the image by.


Let’s say we have the following HTML:

<div id="div1">
  <img id="image1" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
</div>

If we want to rotate the image by 90 degrees we can use the getElementById() method to target the image and then change its transform property in the following JavaScript code.

document.getElementById("image1").style.transform = "rotate(90deg)";

You can also rotate an image easily using jQuery.

Using JavaScript to Rotate an Image with a Click

To rotate an image using JavaScript, we can combine the getElementById() method with an onclick event.

In this example, we will have an image that we will want to rotate by 30 degrees. We will have a button that will allow the user to rotate the image by 30 degrees each time.

Here is our simple HTML code:

<div id="div1">
  <img id="image1" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
  <div id="click-me" onclick="rotateImage()">Rotate image</div>
</div>

We can use an onclick event to run the function “rotateImage” when the user clicks a button.

We can then use the getElementById() method to target the transform property and use it to rotate our image.

Below is the JavaScript code which will allow the user to be able to rotate our image by 30degrees each time:

var rotateDegrees = 0;  
function rotateImage(){
  rotateDegrees += 30;
  document.getElementById("image1").style.transform = "rotate(" + rotateDegrees + "deg)";
};

The final code and output for this example of how to use JavaScript to rotate an image is below:

Code Output:

Rotate image

Full Code:

<div id="div1">
  <img id="image1" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
  <div id="click-me" onclick="rotateImage()">Rotate image</div>
</div>

<script>

var rotateDegrees = 0;  
function rotateImage(){
  rotateDegrees += 30;
  document.getElementById("image1").style.transform = "rotate(" + rotateDegrees + "deg)";
};

</script>

Hopefully this article has been useful for you to understand how to use JavaScript to rotate an image.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Add Item to Array if it Does Not Exist
  • 2.  JavaScript Random Boolean – How to Generate Random Boolean Values
  • 3.  Set Hidden Field Value In JavaScript
  • 4.  How to Split a Number into Digits in JavaScript
  • 5.  Using JavaScript to Stop a Timer
  • 6.  Using JavaScript to Convert Double to Integer
  • 7.  Using JavaScript to Set the Id of an Element
  • 8.  How to Change an Image on Hover Using JavaScript
  • 9.  Remove All Instances of Value From an Array in JavaScript
  • 10.  Using JavaScript to Check if a Number is Divisible by 2

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy