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

  • Home
  • SEARCH
  • 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 7836841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:20:20+00:00 2026-06-02T14:20:20+00:00

I am experiencing troubles concerning the position of mouse cursor inside my SVG document.

  • 0

I am experiencing troubles concerning the position of mouse cursor inside my SVG document. I’d like to design a potentiometer that will follow the cursor when dragged, using JavaScript in an HTML page.

I tried evt.clientX/Y and evt.screenX/Y but as my SVG is in autoscale, coordinates inside my SVG are different. I have been searching for an answer for days now but I couldn’t find any solution (either knowing my SVG rescaling factor in real time or have a function for mouse location in SVG coordinates system).

The rotation will follow a simple rule:

if (evt.screenX < xc)
  ang = Math.atan((evt.screenY - yc) / (evt.screenX - xc)) * 360/(2*Math.PI) - 90;  

if (evt.screenX > xc)
  ang = Math.atan((evt.screenY - yc) / (evt.screenX - xc)) * 360/(2*Math.PI) + 90;  

With (xc;yc) as center of rotation and replacing all evt.screenX/Y by the coordinates of the mouse inside my SVG.

  • 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-02T14:20:28+00:00Added an answer on June 2, 2026 at 2:20 pm

    See this code, which not only shows how to transform from screen space to global SVG space, but also how to transform a point from SVG space into the transformed space of an element:
    http://phrogz.net/svg/drag_under_transformation.xhtml

    In short:

    // Find your root SVG element
    var svg = document.querySelector('svg');
    
    // Create an SVGPoint for future math
    var pt = svg.createSVGPoint();
    
    // Get point in global SVG space
    function cursorPoint(evt){
      pt.x = evt.clientX; pt.y = evt.clientY;
      return pt.matrixTransform(svg.getScreenCTM().inverse());
    }
    
    svg.addEventListener('mousemove',function(evt){
      var loc = cursorPoint(evt);
      // Use loc.x and loc.y here
    },false);
    

    Edit: I’ve created a sample tailored to your needs (albeit only in global SVG space):
    http://phrogz.net/svg/rotate-to-point-at-cursor.svg

    It adds the following method to the above:

    function rotateElement(el,originX,originY,towardsX,towardsY){
      var angle = Math.atan2(towardsY-originY,towardsX-originX);
      var degrees = angle*180/Math.PI + 90;
      el.setAttribute(
        'transform',
        'translate('+originX+','+originY+') ' +
          'rotate('+degrees+') ' +
          'translate('+(-originX)+','+(-originY)+')'
      );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that may seem weird and looking for troubles but I think experiencing
I'm experiencing some troubles with Qt's signals and slots mechanism. I have something like
Anybody having trouble using AVFoundation with GC ON??? What I am experiencing is that
When experiencing networking problems on client machines, I'd like to be able to run
I'm experiencing something that I can't explain with Rails 2.3.2. I've created a new
On my multithreaded server I am experiencincg troubles with connections that are not coming
I have a contact that is experiencing trouble with SAX when parsing RSS and
I am new to WPF environment and I am experiencing some problems like if
I've experiencing some troubles when running applications in Visual Web Developer 2010 with the
Im reading some RSS from an URL and are experiencing some troubles. Initially I

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.