title: Zoom to and from point
body: <p>I'm trying to zoom a DisplayObject into a certain point. I figured it would be easy, but I've spent a day now trying to figure it out.</p>
  
  <p>Basically, I think this <em>should</em> work. Emphasis on should.</p>
  
  <pre><code>//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;
  </code></pre>
  
  <p>It centers the point if scale is 1, but it gets further and further away from center as you increase the scale. I've tried dozens of different methods. <a href="https://gist.github.com/3092911" rel="nofollow noreferrer">This method</a>, which I have seen on several sites, produced the same exact results. Anyone have any idea how to get this to work?</p>
  
  <p><strong>EDIT 10-1-12</strong>:
  As a followup, I took the <a href="https://stackoverflow.com/questions/12571958/as3-scale-parent-mcs-center-to-child-mcs-center/12572155#12572155">code snippet</a> that LondonDrugs_MediaServices provided as a basis for my original issue. I needed to be able to zoom to a specific point at a specific scale relative to the unscaled image (think how Google Maps zooms to a specific location). To do this, I had to center my image on the point before running the translation code. I've posted the additional code below. For other uses (pinch to zoom, scrolling, and double click), I used the code provided by Vesper, which worked quite well.</p>
  
  <pre><code>//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;
  </code></pre>