TextGradient
This class creates a text effect using a gradient to show a text sliding from the left. I'll probably put later a version with a tweener and add the directions.
Actionscript:
-
package com.soundstep.effects {
-
-
import flash.display.DisplayObjectContainer;
-
import flash.display.Sprite;
-
import flash.events.EventDispatcher;
-
import flash.geom.Matrix;
-
import flash.display.SpreadMethod;
-
import flash.text.TextField;
-
import flash.display.GradientType;
-
import flash.events.Event;
-
import flash.events.EventDispatcher;
-
import flash.events.TimerEvent;
-
import flash.utils.Timer;
-
-
/**
-
* <b>Author:</b> Romuald Quantin - <a href="http://www.soundstep.com/" target="_blank">www.soundstep.com</a><br />
-
* <b>Class version:</b> 1.0<br />
-
* <b>Actionscript version:</b> 3.0<br />
-
* <b>Copyright:</b> Free to use and change (except to include in a framework), an notification email will be welcome for a commercial use (just for information).<br />
-
* <b>Date:</b> 04-2008<br />
-
* <b>Usage:</b> Show a TextField using a gradient effect sliding from the left (timeline based)
-
* @example
-
* <listing version="3.0">
-
* var t:TextField = new TextField();
-
* t.textColor = 0x004080;
-
* t.selectable = false;
-
* t.multiline = true;
-
* t.wordWrap = true;
-
* t.autoSize = TextFieldAutoSize.LEFT;
-
* t.width = 300;
-
* t.embedFonts = true; // you must create the Arial font in the flash library
-
* t.htmlText = "<font face='Arial' size='11'>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</font>";
-
* t.x = t.y = 100;
-
* addChild(t);
-
* var tg:TextGradient = new TextGradient(this, t, t.textColor, t.alpha, false, .02, 1);
-
* tg.addEventListener(TextGradient.EFFECT_STARTED, handlerStart);
-
* tg.addEventListener(TextGradient.EFFECT_ENDED, handlerEnd);
-
* tg.start();
-
* </listing>
-
* @eventType TextGradient.
-
*/
-
-
public class TextGradient extends EventDispatcher {
-
-
//------------------------------------
-
// private, protected properties
-
//------------------------------------
-
-
private var _target:TextField;
-
private var _mask:Sprite;
-
private var _holder:DisplayObjectContainer;
-
private var _timer:Timer;
-
private var _targetPos:Number;
-
private var _alpha:Number;
-
private var _color:uint;
-
-
//------------------------------------
-
// public properties
-
//------------------------------------
-
-
public static const EFFECT_ENDED:String = "effect_started";
-
public static const EFFECT_STARTED:String = "effect_ended";
-
-
/**
-
* speed of the animation, timeline based, a higher number will run the animation quicker
-
* @default 0.03
-
*/
-
public var speed:Number;
-
-
/**
-
* delay of the animation
-
* @default 0
-
*/
-
public var delay:Number;
-
-
//------------------------------------
-
// constructor
-
//------------------------------------
-
-
/**
-
* Constructor
-
* @param holder DisplayObject that contains the TextField
-
* @param target TextField that will be displayed
-
* @param color color of the TextField
-
* @param alpha alpha of the TextField
-
* @param autoStart wether or not the animation will start straight, if set to false, you must use the public method start
-
* @param Speed speed of the animation, timeline based, a higher number will run the animation quicker
-
* @param delay delay of the animation
-
*/
-
public function TextGradient(holder:DisplayObjectContainer, target:TextField, color:uint = 0x000000, alpha:Number = 1, autoStart:Boolean = true, speed:Number = .03, delay:Number = 0) {
-
EventDispatcher(this);
-
_holder = holder;
-
_target = target;
-
_alpha = alpha;
-
_target.alpha = _alpha;
-
_color = color;
-
this.speed = speed;
-
this.delay = delay;
-
_targetPos = _target.x + _target.width;
-
buildMask();
-
if (autoStart) start();
-
}
-
-
//
-
// PRIVATE, PROTECTED
-
//________________________________________________________________________________________________
-
-
private function buildMask():void {
-
var matr:Matrix = new Matrix();
-
matr.createGradientBox(_target.width, _target.height, 0, 0, 0);
-
_mask = new Sprite();
-
_mask.graphics.beginFill(_color, 1);
-
_mask.alpha = _alpha;
-
_mask.graphics.drawRect(0 - _target.width, 0, _target.width, _target.height);
-
_mask.graphics.beginGradientFill(
-
GradientType.LINEAR,
-
[_color, _color],
-
[1, 0],
-
[0, 255],
-
matr,
-
SpreadMethod.PAD
-
);
-
_mask.graphics.drawRect(0, 0, _target.width, _target.height);
-
_mask.x = _target.x - _mask.width;
-
_mask.y = _target.y;
-
}
-
-
private function frameHandler(e:Event):void {
-
var dx:Number = _targetPos - _mask.x;
-
_mask.x += dx * speed;
-
if (_mask.x> _targetPos - 5) {
-
dispatchEvent(new Event(TextGradient.EFFECT_ENDED));
-
dispose();
-
}
-
}
-
-
private function doStart(e:TimerEvent = null):void {
-
if (_timer) _timer.removeEventListener(TimerEvent.TIMER, doStart);
-
_mask.addEventListener(Event.ENTER_FRAME, frameHandler);
-
dispatchEvent(new Event(TextGradient.EFFECT_STARTED));
-
}
-
-
// PUBLIC
-
//________________________________________________________________________________________________
-
-
/**
-
* start the animation (if autoStart has been set to false in the constructor)
-
*/
-
public function start():void {
-
_mask.mask = _target;
-
_holder.addChild(_mask);
-
if (delay> 0) {
-
_timer = new Timer(delay*1000, 1);
-
_timer.addEventListener(TimerEvent.TIMER, doStart);
-
_timer.start();
-
}
-
else doStart();
-
}
-
-
/**
-
* stop and clean the animation
-
*/
-
public function dispose():void {
-
if (_timer) _timer.removeEventListener(TimerEvent.TIMER, doStart);
-
if (_mask) {
-
_mask.removeEventListener(Event.ENTER_FRAME, frameHandler);
-
_holder.removeChild(_mask);
-
_mask.mask = null;
-
_mask = null;
-
}
-
}
-
-
/**
-
* return the Sprite of the mask used to reveal the TextField
-
* @return return a Sprite
-
*/
-
public function get mask():Sprite {
-
return _mask;
-
}
-
-
}
-
-
}


Entries (RSS)