1

Based on an original rect1 (x1, y1, w1, h1) and a known scaled rect2 (x2, y2, w2, h2) what is the formula to find out what transform-origin was used? I need a reusable formula because rect2 can be positioned anywhere.

Put another way, I know the bounds of a little rectangle and want it to transition to a bigger rectangle by applying the correct transform-origin coordinates (in px or %).

AlexG
  • 3,617
  • 1
  • 23
  • 19
  • I've just post it here, because really need help with it. http://stackoverflow.com/q/34958498/4838099 –  Jan 23 '16 at 22:54

1 Answers1

2

In a simple scale, the ratios between the distances x1 -> tx and tx -> (x1+w1) where the transform origin's x coordinate is tx remain the same after the scale. In order words:

(tx-x1)/w1 = (tx-x2)/w2

solving for tx, you get:

tx = (w2/(w2-w1)) * (x1 - (x2 * w1)/w2)

Analagous formula will yield your ty.

(Note that if you're using perspective with a z transform then this formula will be wrong, and if you don't know your perspective origin either, then I believe the result will be indeterminate because there will be infinite combinations of perspective origin and transform origin that will yield the same result. If you do know the perspective origin then the math is still pretty complicated.)

Michael Mullany
  • 30,283
  • 6
  • 81
  • 105
  • Hummm that looks good; Since in my case x1 is always 0, I can even simplify the formula to (w1 * x2) / (w1 - w2); I will confirm soon :) – AlexG Dec 12 '12 at 17:30