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 7053141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:28:07+00:00 2026-05-28T03:28:07+00:00

Okay, this is getting complicated… Given situation: I have a canvas with the dimensions

  • 0

Okay, this is getting complicated…

Given situation:
I have a canvas with the dimensions of 800×600.
My mouse is at canvas position 100×200 (for example).

I save my canvas state.
Now I rotate and translate the canvas, I draw a square.
I restore my canvas state.

Is there any way to determine if my mouse is over the square?
I think I would have to translate/rotate my mouse position as well – in the opposite direction, but how would I do this?

  • 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-05-28T03:28:07+00:00Added an answer on May 28, 2026 at 3:28 am

    You can get hold of an objects world position/rotation by recursively applying this formula:

    worldX = parentX + x * Math.cos( parentR ) - y * Math.sin( parentR );
    worldY = parentY + x * Math.sin( parentR ) + y * Math.cos( parentR );
    worldR = parentR + r;
    

    A javascript implementation would be:

    var deg2rad, rad2deg, getXYR;
    
    deg2rad = function ( d ) { return d * Math.PI / 180 };
    rad2deg = function ( r ) { return r / Math.PI * 180 };
    
    getXYR = function ( node ) {
      var x, y, r,
          parentXYR, pX, pY, pR,
          nX, nY;
    
      x = y = r = 0;
    
      if ( node ) {
        parentXYR = getXYR( node.parent );
        pX = parentXYR.x;
        pY = parentXYR.y;
        pR = deg2rad( parentXYR.r );
        nX = node.x;
        nY = node.y;
    
        x = pX + nX * Math.cos( pR ) - nY * Math.sin( pR );
        y = pY + nX * Math.sin( pR ) + nY * Math.cos( pR );
        r = rad2deg( pR + deg2rad( node.r ) );
      }
    
      return { x:x, y:y, r:r };
    };
    

    Try it out with these objects:

    el1 = {x:3,y:0,r:45};
    el2 = {x:0,y:0,r:45};
    el1.parent = el2;
    getXYR(el1);
    

    It won’t be long before you want to calculate the shortest angle between two objects, if you get the deltaX (x2-x1) and deltaY (y2-y1) between the two objects you can get the angle with this function:

    var getAngle = function ( dx, dy ) {
      var r = Math.atan2( dy, dx ) * 180 / Math.PI;
      return ( r > 180 )  ? r-360 :
             ( r < -180 ) ? r+360 :
             r;
    }
    

    In the long run it’s better to learn using matrices instead. The equivalence of getting the world pos/rot is a world matrix. Here’s some good info about matrices (in the SVG doc, but it’s not relevant): http://www.w3.org/TR/SVG/coords.html#NestedTransformations

    This is how you would do it with matrices (and the gl-matrix lib https://github.com/toji/gl-matrix):

    var getWorldMatrix = function ( node ) {
        var parentMatrix;
    
        if ( !node )
            return mat4.identity();
    
        parentMatrix = getWorldMatrix( node.parent );
        return mat4.multiply( parentMatrix, node.matrix );
    };
    

    Oh, i forgot, now to finally register a click you just get the screen coordinates of the mouse and compare them to the objects position + the canvas viewport offset.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, this is getting stupid, I have Microsoft Visual Studio 2008, was working fine,
Okay this is werid, i keep getting the error, randomly. ValueError: matrix must be
Okay this is a real headscratcher. I have an application which calls a web
Okay this may be a simple question but I have yet to come with
Okay - this is the dumbest glitch I have seen in a while: <!DOCTYPE
Okay, now things are getting a little complicated. I need to build a Flash
I have a rather complicated toolchain so prepare for a lengthy post until getting
Okay, i am new for android developping, or any developping and getting this error.
Okay, this is the problem: I am getting this error message when I am
Okay so this is for a school assignment. I have had no problems doing

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.