/**
* VERSION: 0.4 (beta)
* DATE: 2010-12-22
* AS3
* UPDATES AND DOCS AT: http://www.GreenSock.com
**/
package com.greensock.motionPaths {
import flash.display.Graphics;
import flash.events.Event;
import flash.geom.Matrix;
/**
* A CirclePath2D defines a circular path on which a PathFollower can be placed, making it simple to tween objects
* along a circle or oval (make an oval by altering the width/height/scaleX/scaleY properties). A PathFollower's
* position along the path is described using its progress
property, a value between 0 and 1 where
* 0 is at the beginning of the path, 0.5 is in the middle, and 1 is at the very end of the path. So to tween a
* PathFollower along the path, you can simply tween its progress
property. To tween ALL of the
* followers on the path at once, you can tween the CirclePath2D's progress
property. PathFollowers
* automatically wrap so that if the progress
value exceeds 1 or drops below 0, it shows up on
* the other end of the path.
*
* Since CirclePath2D extends the Shape class, you can add an instance to the display list to see a line representation
* of the path drawn which can be helpful especially during the production phase. Use lineStyle()
* to adjust the color, thickness, and other attributes of the line that is drawn (or set the CirclePath2D's
* visible
property to false or don't add it to the display list if you don't want to see the line
* at all). You can also adjust all of its properties like radius, scaleX, scaleY, rotation, width, height, x,
* and y
. That means you can tween those values as well to achieve very dynamic, complex effects with ease.
*
* @example Example AS3 code:
progress
* property which will provide better performance than tweening each follower independently.mc
on the CirclePath2D at 90 degrees
* (bottom), you'd do:
*
* var follower:PathFollower = myCircle.addFollower(mc, myCircle.angleToProgress(90));
*
*
*
* @param angle The angle whose progress value you want to determine
* @param useRadians If you prefer to define the angle in radians instead of degrees, set this to true (it is false by default)
* @return The progress value associated with the angle
*/
public function angleToProgress(angle:Number, useRadians:Boolean=false):Number {
var revolution:Number = useRadians ? Math.PI * 2 : 360;
if (angle < 0) {
angle += (int(-angle / revolution) + 1) * revolution;
} else if (angle > revolution) {
angle -= int(angle / revolution) * revolution;
}
return angle / revolution;
}
/**
* Translates a progress value (typically between 0 and 1 where 0 is the beginning of the path,
* 0.5 is in the middle, and 1 is at the end) to the associated angle on the CirclePath2D.
* For example, to find out what angle a particular PathFollower is at, you'd do:
*
* var angle:Number = myCircle.progressToAngle(myFollower.progress, false);
*
*
*
* @param progress The progress value to translate into an angle
* @param useRadians If you prefer that the angle be described in radians instead of degrees, set this to true (it is false by default)
* @return The angle (in degrees or radians depending on the useRadians value) associated with the progress value.
*/
public function progressToAngle(progress:Number, useRadians:Boolean=false):Number {
var revolution:Number = useRadians ? Math.PI * 2 : 360;
return progress * revolution;
}
/**
* Simplifies tweening by determining a relative change in the progress value of a follower based on the
* endAngle, direction, and extraRevolutions that you define. For example, to tween myFollower
* from wherever it is currently to the position at 315 degrees, moving in the COUNTER_CLOCKWISE direction
* and going 2 extra revolutions, you could do:
*
* TweenLite.to(myFollower, 2, {progress:myCircle.followerTween(myFollower, 315, Direction.COUNTER_CLOCKWISE, 2)});
*
*
* @param follower The PathFollower (or its associated target) that will be tweened (determines the start angle)
* @param endAngle The destination (end) angle
* @param direction The direction in which to travel - options are Direction.CLOCKWISE
("clockwise"), Direction.COUNTER_CLOCKWISE
("counterClockwise"), or Direction.SHORTEST
("shortest").
* @param extraRevolutions If instead of going directly to the endAngle, you want the target to travel one or more extra revolutions around the path before going to the endAngle, define that number of revolutions here.
* @param useRadians If you prefer to define the angle in radians instead of degrees, set this to true (it is false by default)
* @return A String representing the amount of change in the progress
value (feel free to cast it as a Number if you want, but it returns a String because TweenLite/Max/Nano recognize Strings as relative values.
*/
public function followerTween(follower:*, endAngle:Number, direction:String="clockwise", extraRevolutions:uint=0, useRadians:Boolean=false):String {
var revolution:Number = useRadians ? Math.PI * 2 : 360;
return String(anglesToProgressChange(getFollower(follower).progress * revolution, endAngle, direction, extraRevolutions, useRadians));
}
/**
* Returns the amount of progress
change between two angles on the CirclePath2D, allowing special
* parameters like direction and extraRevolutions.
*
* @param startAngle The starting angle
* @param endAngle The ending angle
* @param direction The direction in which to travel - options are Direction.CLOCKWISE
("clockwise"), Direction.COUNTER_CLOCKWISE
("counterClockwise"), or Direction.SHORTEST
("shortest").
* @param extraRevolutions If instead of going directly to the endAngle, you want the target to travel one or more extra revolutions around the path before going to the endAngle, define that number of revolutions here.
* @param useRadians If you prefer to define the angle in radians instead of degrees, set this to true (it is false by default)
* @return A Number representing the amount of change in the progress
value.
*/
public function anglesToProgressChange(startAngle:Number, endAngle:Number, direction:String="clockwise", extraRevolutions:uint=0, useRadians:Boolean=false):Number {
var revolution:Number = useRadians ? Math.PI * 2 : 360;
var dif:Number = endAngle - startAngle;
if (dif < 0 && direction == "clockwise") {
dif += (int(-dif / revolution) + 1) * revolution;
} else if (dif > 0 && direction == "counterClockwise") {
dif -= (int(dif / revolution) + 1) * revolution;
} else if (direction == "shortest") {
dif = dif % revolution;
if (dif != dif % (revolution * 0.5)) {
dif = (dif < 0) ? dif + revolution : dif - revolution;
}
}
if (dif < 0) {
dif -= extraRevolutions * revolution;
} else {
dif += extraRevolutions * revolution;
}
return dif / revolution;
}
/** radius of the circle (does not factor in any transformations like scaleX/scaleY) **/
public function get radius():Number {
return _radius;
}
public function set radius(value:Number):void {
_radius = value;
_redrawLine = true;
update();
}
}
}