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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:57:18+00:00 2026-05-21T17:57:18+00:00

I am trying to get the screen pixel coordinates of a rectangle in SVG

  • 0

I am trying to get the screen pixel coordinates of a rectangle in SVG via java script.
When the rectangle has been clicked, I can figure out its width, height, x and y position with getBBox().

But these positions are the original positions. I want the screen position.

For example if I manipulate the viewBox of the whole SVG, these getBBox coordinates are not any more the same than the screen pixels. Is there a function or way to get the
coordinates considering the current viewBox and the pixel size of svg element?

  • 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-21T17:57:18+00:00Added an answer on May 21, 2026 at 5:57 pm

    Demo: http://phrogz.net/SVG/screen_location_for_element.xhtml

    var svg = document.querySelector('svg');
    var pt  = svg.createSVGPoint();
    function screenCoordsForRect(rect){
      var corners = {};
      var matrix  = rect.getScreenCTM();
      pt.x = rect.x.animVal.value;
      pt.y = rect.y.animVal.value;
      corners.nw = pt.matrixTransform(matrix);
      pt.x += rect.width.animVal.value;
      corners.ne = pt.matrixTransform(matrix);
      pt.y += rect.height.animVal.value;
      corners.se = pt.matrixTransform(matrix);
      pt.x -= rect.width.animVal.value;
      corners.sw = pt.matrixTransform(matrix);
      return corners;
    }
    

    The magenta squares are absolutely-positioned divs in the HTML element, using screen space coordinates. When you drag or resize the rectangles this function is called and moves the corner divs over the four corners (lines 116-126). Note that this works even when the rectangles are in arbitrary nested transformation (e.g. the blue rectangle) and the SVG is scaled (resize your browser window).

    For fun, drag one of the rectangles off the edge of the SVG canvas and notice the screen-space corners staying over the unseen dots.

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

Sidebar

Related Questions

I have a 2 part splash screen that I've been trying to get to
I'm trying to get the screen coordinates (that is, relative to the top left
I have been trying to get it to work using this thread but can't
I'm trying to use DrawingContext.DrawGeometry get a rectangle with 1 device pixel wide sides.
I am trying to get started with iOS Airplay (split screen and mirroring) and
I am trying to get a layout that always takes up the entire screen,
I'm trying to use this to create a splash screen, but I only get
I'm trying to draw on the screen pixel-by-pixel using XNA, but am having problems
I'm trying to stack media queries in CSS, and can't seem to get it
I'm trying to get an image centered on the screen, and I want it

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.