14

I have a very complicated site built on CSS3 that has html elements 3d-transformed, rotated, flipped, flopped and just generally distorted.

I'm trying to figure out the on-screen location of one of these elements and don't see any way to do so. I was wondering if anyone has any ingenious ideas.

Alternatively, if anyone can explain the math behind -webkit-perspective, I can figure out the position as that's the only thing I'm not sure how to model.

Dharman
  • 30,962
  • 25
  • 85
  • 135

3 Answers3

13

Have you tried using getBoundingClientRect()?

I've used it successfully in the past to calculate the dimensions of elements that have been transformed with the transform property.

alex
  • 479,566
  • 201
  • 878
  • 984
  • just add . that if element have `transform-style: preserve-3d` it change the value that return from that functions. make it respect 3d world on not – pery mimon Jan 10 '19 at 11:42
1

Using getBoundingClientRect is a good idea but will only give you the coordinates of the rectangle that contains your shape, not the exact coordinates of the 4 topleft, bottomright, bottomleft, topright corners.

You'd only be able to do this by taking each of those non-transformed coordinates and applying the transform via javascript.

1

The problem is, that the CSS3 transformations doesn't actually change the position of the elements in anyway. Of course the browsers "know" that they are repositioned, because it renders them, but this information is not provided back to the DOM/API.

The only thing I can think of, is to calculate the positions based on the transformations yourself, since these are "simple" matrix transformations.

Unfortunately Algebra class has been too long ago, that I can't tell you anymore how to do it - only that it is possible.

RoToRa
  • 37,635
  • 12
  • 69
  • 105
  • 3
    yeah, I know what the issue is :) And I'd be happy to calculate it myself, but as I said I don't know what the math behind `-webkit-perspective` actually is. It acts somewhat like a focal length control, but wtf does `-webkit-perspective:500` actually mean?!? –  Nov 06 '11 at 17:09
  • look at three.js 's CSS3DRenderer. From what I remember, they take the camera's field of view (fov), and multiply it by either the width or the height to get the perspective. So it's in relation to those. – dansch Jun 12 '17 at 20:20