Star Effect
/**
* Copyright luar ( http://wonderfl.net/user/luar )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/vPkI
*/
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.geom.Rectangle;
[SWF(backgroundColor="#5f0e17", width="720", height="405", frameRate="30")]
public class Main extends Sprite {
private var light:StarLight;
public function Main() {
init();
}
private function init():void {
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
graphics.beginFill(0x5f0e17);
graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
graphics.endFill();
var rect:Rectangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
light = new StarLight(rect);
addChild(light);
light.start();
}
}
}
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.display.BlendMode;
import flash.filters.BlurFilter;
import flash.utils.Timer;
import flash.events.TimerEvent;
class StarLight extends Sprite {
private var rect:Rectangle;
private var bitmapData:BitmapData;
private var bitmap:Bitmap;
private var container:Sprite;
private static var blur:BlurFilter;
private static var point:Point = new Point();
private var id:uint = 0;
private var leader:TrackLeader;
private static var max:uint = 1;
private var followers:Array;
private var tracks:Vector.<Point>;
private static var interval:uint = 50; // followers spread area height
private var particles:Array;
private var timer:Timer;
private var initColor:Number = 0xfefe84;
private var movePt:Point;
private var ang:int = 0;
private var dec2rad:Number = Math.PI / 180;
private var sw2:int;
private var sh2:int;
public function StarLight(r:Rectangle){
rect = r;
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
private function init(evt:Event = null):void {
sw2 = stage.stageWidth * 2;
sh2 = stage.stageHeight / 2
removeEventListener(Event.ADDED_TO_STAGE, init);
bitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
bitmap = new Bitmap(bitmapData);
addChild(bitmap);
container = new Sprite();
addChild(container);
blur = new BlurFilter(20, 20, 1);
leader = new TrackLeader();
leader.x = sw2;
leader.y = sh2;
followers = new Array();
tracks = new Vector.<Point>();
for (var n:uint = 0; n < max; n++){
var follower:TrackFollower = new TrackFollower();
follower.x = sw2;
follower.y = sh2;
followers.push(follower);
for (var t:uint = 0; t < interval; t++){
tracks.push(new Point(sw2, sh2));
}
}
particles = new Array();
movePt = new Point(sw2, sh2);
}
public function start():void {
timer = new Timer(2);
timer.addEventListener(TimerEvent.TIMER, create, false, 0, true);
timer.start();
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
}
public function stop():void {
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, create);
removeEventListener(Event.ENTER_FRAME, update);
}
private function create(evt:TimerEvent):void {
var particle:StarUnit = new StarUnit(Math.random() * 10 + 10, initColor);
particle.x = sw2;
particle.y = sh2;
particle.track(leader);
particle.angle = Math.random() * 360;
particle.speed = Math.random() * 5 + 10;
particle.power = 1;
particle.setup();
particle.blendMode = BlendMode.SCREEN;
container.addChild(particle);
particles.push(particle);
for (var n:uint = 0; n < max; n++){
particle = new StarUnit(Math.random() * 10 + 10, initColor);
particle.x = sw2;
particle.y = sh2;
var follower:TrackFollower = followers[n];
particle.track(follower);
particle.angle = Math.random() * 360;
particle.speed = Math.random() * 5 + 10;
particle.power = 1;
particle.setup();
particle.blendMode = BlendMode.SCREEN;
container.addChild(particle);
particles.push(particle);
}
}
private function update(evt:Event):void {
setup();
bitmapData.lock();
for (var n:uint = 0; n < particles.length; n++){
var particle:StarUnit = particles[n];
particle.update();
particle.scale = particle.alpha = particle.power;
if (particle.power < 0){
container.removeChild(particle);
particles.splice(n, 1);
particle = null;
}
}
bitmapData.draw(container, null, null, BlendMode.SCREEN, null, true);
bitmapData.applyFilter(bitmapData, rect, point, blur);
bitmapData.unlock();
id++;
}
private function setup():void {
var isFast:Boolean = false;
movePt.x -= 25;
movePt.y = sh2 + 50 * Math.sin(ang * dec2rad);
if (movePt.x < -400){
movePt.x = 2500
movePt.y = -6000
isFast = true;
}
ang += 10;
leader.update(movePt, isFast);
for (var n:uint = 0; n < max; n++){
var follower:Object = followers[n];
follower.update(tracks[interval * (n + 1) - 1]);
}
tracks.unshift(new Point(leader.x, leader.y));
tracks.pop();
}
}
import flash.display.Shape;
class StarUnit extends Shape {
private var radius:uint = 10;
private var color:uint = 0xFFFFFF;
private var target:Object;
public var angle:Number = 0;
public var speed:Number = 0;
private var tx:Number = 0;
private var ty:Number = 0;
private var vx:Number = 0;
private var vy:Number = 0;
public var power:Number = 0;
private static var radian:Number = Math.PI / 180;
private static var friction:Number = 0.96;
private static var deceleration:Number = 0.025;
private static var acceleration:Number = 0.05;
private var _scale:Number = 1;
private var rotDir:int = 1;
public function StarUnit(r:uint = 10, c:uint = 0xFFFFFF){
cacheAsBitmap = true;
radius = r;
color = c;
graphics.lineStyle(0, color);
graphics.beginFill(color);
if (Math.random() > 0.2){
drawStar();
} else {
radius *= 3;
drawSnowFlower();
rotDir = -1;
}
rotDir = Math.random()*10-Math.random()*10
//alpha = Math.random();
//rotation = Math.random() * 360;
}
private function drawStar():void {
graphics.moveTo(radius, 0);
for (var i:int = 1; i < 11; i++){
var radius2:Number = radius;
if (i % 2 > 0){
radius2 = radius / 2;
}
var angle:Number = Math.PI * 2 / 10 * i;
graphics.lineTo(Math.cos(angle) * radius2, Math.sin(angle) * radius2);
}
}
private function drawSnowFlower():void {
graphics.moveTo(radius/10, 0);
for (var i:int = 0; i < 24; i++){
var radius2:Number = radius;
var mod:int = i % 4;
if (mod == 1){
radius2 = radius;
} else if (mod == 3){
radius2 = radius / 2;
} else {
radius2 = radius / 10;
}
var angle:Number = Math.PI * 2 / 24 * i;
graphics.lineTo(Math.cos(angle) * radius2, Math.sin(angle) * radius2);
}
}
public function track(t:Object):void {
target = t;
}
public function setup():void {
x = target.x;
y = target.y;
vx = speed * Math.cos(angle * radian);
vy = speed * Math.sin(angle * radian);
}
public function update():void {
tx += vx;
ty += vy;
x += (target.x + tx - x) * acceleration;
y += (target.y + ty - y) * acceleration;
vx *= friction;
vy *= friction;
power -= deceleration;
rotation += rotDir;
}
public function get scale():Number {
return _scale;
}
public function set scale(param:Number):void {
_scale = param;
scaleX = scaleY = _scale;
}
}
class TrackLeader {
public var x:Number = 0;
public var y:Number = 0;
private static var deceleration:Number = 0.36;
public function TrackLeader(){
}
public function update(track:Point, isFast:Boolean = false):void {
if (!isFast) {
x += (track.x - x) * deceleration;
y += (track.y - y) * deceleration;
} else {
x = track.x;
y = track.y;
}
}
}
class TrackFollower {
public var x:Number = 0;
public var y:Number = 0;
private static var deceleration:Number = 0.36;
public function TrackFollower(){
}
public function update(track:Point):void {
x = track.x;
y = track.y;
}
}