In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

flash on 2011-7-14

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	
	/**
	 * @see http://vimeo.com/26378657
	 * @author Todd Vanderlin & Arturo, open frameworks
	 */
	public class Chain extends Sprite {
		public var angles:Vector.<Number> = new <Number> [];
		public var pts:Vector.<Point> = new <Point> [];
		public function Chain () {
			pts = new Vector.<Point>;
			var nPts:int = 7;
			for (var i:int=0; i<nPts; i++) {
				pts.push (new Point);
				angles.push (0);
			}
			stage.addEventListener (Event.ENTER_FRAME, draw);
			stage.addEventListener (KeyboardEvent.KEY_DOWN, key);
		}
		public function draw (e:Event):void {
			pts[0].x = mouseX;
			pts[0].y = mouseY;
			
			for (var i:int=1; i<pts.length; i++) {
				
				var dx:Number	= pts[i-1].x - pts[i].x;
				var dy:Number	= pts[i-1].y - pts[i].y;
				var a:Number	= Math.atan2(dy, dx);
				var dis:Number	= 60;
				var range:Number = 45 * Math.PI/180;

				if(i == 1) {
					pts[i].x = pts[i-1].x - Math.cos(a) * dis;
					pts[i].y = pts[i-1].y - Math.sin(a) * dis;
				}	
				else {
					
					var prea:Number	= angles[i-1];
					var d:Number	= a - prea;
					var absA:Number = Math.abs(d);
					var s:Number	= (d>=0)?1:-1;
					if (absA > Math.PI) {
						a = s * (2 * Math.PI - Math.abs(a)) * -1;
					}
					
					var newAnlge:Number = Math.min(Math.max(a, prea-range), prea+range);
					
					pts[i].x = pts[i-1].x - Math.cos(newAnlge) * dis;
					pts[i].y = pts[i-1].y - Math.sin(newAnlge) * dis;
					
				}		
				
				angles[i] = a;// ofWrapRadians (a);
			}

			graphics.clear ();
			graphics.lineStyle (1, 0x363328);
			graphics.moveTo(pts[0].x, pts[0].y);
			for (i=1; i<pts.length; i++) {
				graphics.lineTo(pts[i].x, pts[i].y);
			}

			for (i=0; i<pts.length; i++) {
				graphics.drawCircle(pts[i].x, pts[i].y, 4);
				graphics.drawCircle(pts[i].x, pts[i].y, 1);
			}
		}
		public function key (e:KeyboardEvent):void {
			// up & down arrows
			switch (e.keyCode) {
				case 38:
					pts.push (pts [pts.length -1].clone ());
					angles.push (angles [angles.length -1]);
					break;
				case 40:
					if (pts.length > 2) {
						pts.pop ();
						angles.pop ();
					}
					break;
			}
		}
	}
}