Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:09:33+00:00 2026-06-15T17:09:33+00:00

I have a canvas which I draw an image onto. The image can be

  • 0

I have a canvas which I draw an image onto. The image can be rotated to a certain amount of degrees before being drawn (EG: 30).

I need to draw a box to contain the drawn image; this would be easy but the width and height change depending on the angle the image is rotated to.

I have seen some very similar questions here, but I couldn’t implement the answers into my code because they are either written in another language like ActionScript or they are rotating a div or other element not within a canvas. I haven’t been able to work out the main math behind it.

I have created a JSFiddle for you to adapt:

http://jsfiddle.net/ZY7Ux/

I have created two functions in this JSFiddle:

drawImageToCanvasRotated(URL, X, Y, Angle);
drawRectangle(X, Y, Width, Height);

You should note that the position values are for the center of the image / rectangle.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:09:34+00:00Added an answer on June 15, 2026 at 5:09 pm

    You rotate the image, so the new width becomes a combination of:

    • the x dimension of the width, which gets a little smaller than the full width (using the cosine)
    • the x dimension of the height, which gets a little higher than 0 (using the sine)

    An analogous calculation applies for the new height. http://jsfiddle.net/ZY7Ux/1/

    var a = Width * Math.cos(Angle);
    var b = Height * Math.sin(Angle);
    var c = a + b;
    
    var p = Width * Math.sin(Angle);
    var q = Height * Math.cos(Angle);
    var r = p + q;
    
    context.strokeRect(
        X - c / 2,
        Y - r / 2,
        c,
        r
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an app in which i have to draw image in canvas
I have some objects which I draw onto a Canvas as part of a
I have a large module written in JS which uses Canvas to draw and
I have a class which inherits from Canvas. On the OnRender method I draw
So I have a rotating canvas element which has an arc drawn inside it
I have an isometric map which I draw to the canvas. When I try
I'm trying to draw a timer in a canvas game using an image which
i have made a canvas and draw an image on canvas i have also
I have a pixel-array and I would like to draw it onto the canvas,
I have a canvas which contains a single myComponentA. A myComponentA contains a MyComponentB.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.