我正在尝试将 DisplayObject 缩放到某个点。我认为这很容易,但我现在花了一天时间试图弄清楚。
基本上,我认为这应该有效。强调应该。
//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,它将使点居中,但随着比例的增加,它会越来越远离中心。我已经尝试了几十种不同的方法。我在几个站点上看到的这种方法产生了相同的确切结果。有人知道如何让它工作吗?
编辑 10-1-12:作为后续,我将 LondonDrugs_MediaServices 提供的代码片段作为我最初问题的基础。我需要能够以相对于未缩放图像的特定比例缩放到特定点(想想谷歌地图如何缩放到特定位置)。为此,我必须在运行翻译代码之前将我的图像集中在该点上。我已经在下面发布了附加代码。对于其他用途(捏缩放、滚动和双击),我使用了 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;