Advertisement
ulfben

Rotate around center (or arbitrary points)

Nov 18th, 2016
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //0. translate the center of the DisplayObject to 0,0 (or an arbitrary offset)
  2. //1. then rotate it
  3. //2. and then translate it back.
  4.  
  5. //using Matrix:
  6. public function centeredRotation(degrees:Number):void {
  7.     if (this.rotation == degrees) { return; }
  8.     var matrix:Matrix = this.transform.matrix;
  9.     var rect:Rectangle = this.getBounds(this.parent);
  10.     matrix.translate(-(rect.left + (rect.width*0.5)), -(rect.top + (rect.height*0.5)));
  11.     matrix.rotate((degrees / 180) * Math.PI);
  12.     matrix.translate(rect.left + (rect.width*0.5), rect.top + (rect.height*0.5));
  13.     this.transform.matrix = matrix;
  14.     this.rotation = Math.round(this.rotation);
  15. }
  16.  
  17. //without Matrix
  18. private function centeredRotation(degrees:Number):void{
  19.     if (degrees == this.rotation){return;}
  20.     var bounds:Rectangle = this.getBounds(this.parent);
  21.     var center:Point = new Point(bounds.x + bounds.width*0.5, bounds.y + bounds.height*0.5);
  22.     this.rotation = degrees;
  23.     bounds = this.getBounds(this.parent);
  24.     var newCenter:Point = new Point(bounds.x + bounds.width*0.5, bounds.y + bounds.height*0.5);
  25.     this.x += center.x - newCenter.x;
  26.     this.y += center.y - newCenter.y;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement