我正在尝试将 DisplayObject 缩放到某个点。我认为这很容易,但我现在花了一天时间试图弄清楚。
基本上我认为这个should工作。强调应该。
//newPoint is the point being centered. There is no initial scaling, so I do not need to compensate for that (yet)
//scale is the zoom level
//container is the parent of the obj
//obj is the object being scaled/panned
var p:Point = new Point(
( this.container.width - this.obj.width * scale + newPoint.x * scale ) / 2,
( this.container.height - this.obj.height * scale + newPoint.y * scale ) / 2
);
this.obj.scaleX = this.obj.scaleY = scale;
this.obj.x = p.x;
this.obj.y = p.y;
如果比例为 1,它将以点为中心,但随着比例的增加,它会离中心越来越远。我尝试了几十种不同的方法。这个方法 https://gist.github.com/3092911,我在几个网站上看到过,产生了完全相同的结果。有人知道如何让它发挥作用吗?
编辑 10-1-12:
作为后续行动,我采取了代码片段 https://stackoverflow.com/questions/12571958/as3-scale-parent-mcs-center-to-child-mcs-center/12572155#12572155LondonDrugs_MediaServices 提供的内容作为我最初问题的基础。我需要能够相对于未缩放的图像以特定比例缩放到特定点(想想 Google 地图如何缩放到特定位置)。为此,我必须在运行翻译代码之前将图像集中在该点上。我在下面发布了附加代码。对于其他用途(捏合缩放、滚动和双击),我使用了 Vesper 提供的代码,效果非常好。
//obj is the object being translated
//container is its parent
//x and y are the coordinates to be zoomed to, in untranslated scaling
//obj.scaleX and obj.scaleY are always identical in my class, so there is no need to account for that
//calculates current center point, with scaling
var center:Point = new Point( ( this.container.width - this.obj.width * this.obj.scaleX ) / 2, ( this.container.height - this.obj.height * this.obj.scaleX ) / 2 );
//calulcates the distance from center the point is, with scaling
var distanceFromCenter:Point = new Point( this.obj.width * this.obj.scaleX / 2 - x * this.obj.scaleX, this.obj.height * this.obj.scaleX / 2 - y * this.obj.scaleX );
//center the object on that specific point
this.obj.x = center.x + distanceFromCenter.x;
this.obj.y = center.y + distanceFromCenter.y;