Calculate transform-origin from original and scaled boxes

吃可爱长大的小学妹 提交于 2019-12-11 06:53:54

问题


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 %).


回答1:


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.)



来源:https://stackoverflow.com/questions/13837655/calculate-transform-origin-from-original-and-scaled-boxes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!