forked from: flash on 2010-6-28
Forked but now all new! Uses open source BetweenAS3 library for tweening
Change constants to modify behaviour
/**
* Copyright dominic_w ( http://wonderfl.net/user/dominic_w )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/5lf1
*/
// Forked but now all new! Uses open source BetweenAS3 library for tweening
// Change constants to modify behaviour
package
{
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.text.AntiAliasType;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.setTimeout;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.ITween;
import org.libspark.betweenas3.easing.*;
[SWF(width="465", height="465")]
public class tags extends Sprite {
protected const MAX_CLOUD_SWIVEL: Number = 15; // max degrees to swivel fulcrum
protected const PARALLEL_TWEENS: Number = 3; // number of parallel tweens changing position tags
protected const MAX_TAG_X_RANGE: Number = 350; // central x area containing tags (e.g. keeps tags inside movie on swivel)
protected const MAX_TAG_Y_RANGE: Number = 175; // central y area containing tags (e.g. keeps tags inside movie on swivel)
protected const MAX_TAG_Z_DEPTH: Number = 450; // max depth tags may be behind fulcrum
protected const MAX_TAG_ALPHA: Number = 0.9; // alpha for tag at min depth
protected const MIN_TAG_ALPHA: Number = 0.65; // alpha for tag at max depth
protected const MAX_TAG_DISTANCE: Number = getDistance(0, 0, 0, MAX_TAG_X_RANGE, MAX_TAG_Y_RANGE, MAX_TAG_Z_DEPTH);
protected const MIN_TWEEN_DURATION: Number = 2.3; // minimum time to tween position of tag
protected const MAX_TWEEN_DURATION: Number = 4.3; // maximum time to tween position of tag
protected var tagList:Array ="User1 TestUser3 CharlieCool Theo11 DoomSlayer AstroDino19 BlueDom Phantom77".split(" ");
protected var pivot:Sprite = new Sprite;
public function tags() {
super();
addEventListener(Event.ADDED_TO_STAGE, function (e:Event):void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// Create fulcrum for tags in center of space
pivot.x = stage.stageWidth/2;
pivot.y = stage.stageHeight/2;
addChild(pivot);
// Make tags children of fulcrum
for each (var tagName:String in tagList) {
var t:TextField = new TextField();
t.antiAliasType = AntiAliasType.ADVANCED;
t.backgroundColor = 0x3BB44A;
t.background = true;
t.defaultTextFormat = new TextFormat(" MS", 20, 0x222222);
t.text = tagName;
t.autoSize = TextFieldAutoSize.LEFT;
t.selectable = false;
t.cacheAsBitmap = true;
pivot.addChild(t);
}
// Offset tags from fulcrum
randomlyDistributeTags();
stage.addEventListener(Event.RESIZE, randomlyDistributeTags);
// On mouse move, swivel fulcrum to face mouse pointer
stage.addEventListener(MouseEvent.MOUSE_MOVE, function(e:MouseEvent):void {
pivot.rotationY = -MAX_CLOUD_SWIVEL * (mouseX - stage.stageWidth/2)/(stage.stageWidth/2);
pivot.rotationX = MAX_CLOUD_SWIVEL * (mouseY - stage.stageHeight/2)/(stage.stageHeight/2);
});
// Randomly move tags
for (var i:int=0; i < PARALLEL_TWEENS; i++)
randomlyMoveTags(i);
});
}
protected function randomlyDistributeTags(e:Event = null):void {
for (var i:int=0; i < pivot.numChildren; i++) {
var t:TextField = pivot.getChildAt(i) as TextField;
t.x = getRandomX(t);
t.y = getRandomY(t);
t.z = getRandomZ();
t.alpha = getAlphaForDepth(z);
}
}
protected function randomlyMoveTags(tweenIdx: int):void {
// Choose tag to play with
var targets:Array= new Array();
for (var i:int=0; i < pivot.numChildren; i++)
if (i % PARALLEL_TWEENS == tweenIdx)
targets.push(pivot.getChildAt(i));
var t:TextField = targets[Math.floor(Math.random()*targets.length)] as TextField;
// Tween chosen tag
var changeX:Boolean = Math.random() > 0.5;
var xx:Number = Math.random() < 0.7 ? getRandomX(t) : t.x;
var yy:Number = Math.random() < 0.2 ? getRandomY(t) : t.y;
var zz:Number = Math.random() < 0.8 ? getRandomZ() : t.z;
var distance:Number = getDistance(t.x, t.y, t.z, xx, yy, zz);
var duration:Number = MIN_TWEEN_DURATION + distance / MAX_TAG_DISTANCE * (MAX_TWEEN_DURATION-MIN_TWEEN_DURATION);
var tween:ITween = BetweenAS3.tween(
t,
{x: xx, y: yy, z: zz, alpha: getAlphaForDepth(zz)},
null,
duration,
Back.easeInOut
);
tween.onComplete = function():void { randomlyMoveTags(tweenIdx) };
tween.play();
}
protected function getDistance(x1: Number, y1: Number, z1: Number, x2: Number, y2: Number, z2: Number): Number {
var x:Number = x2 - x1;
var y:Number = y2 - y1;
var z:Number = z2 - z1;
return Math.sqrt(x * x + y * y + z * z);
}
protected function getRandomX(t: TextField): Number {
return (Math.random() > 0.5 ? 1 : -1) * (Math.random() * MAX_TAG_X_RANGE/2 - t.width/2);
}
protected function getRandomY(t: TextField): Number {
return (Math.random() > 0.5 ? 1 : -1) * (Math.random() * MAX_TAG_Y_RANGE/2 - t.height/2);
}
protected function getRandomZ(): Number {
return Math.random() * MAX_TAG_Z_DEPTH;
}
protected function getAlphaForDepth(d: Number): Number {
return MIN_TAG_ALPHA + (MAX_TAG_ALPHA - MIN_TAG_ALPHA) * (1 - d / MAX_TAG_Z_DEPTH);
}
}
}