forked from: Are the new text engine classes available?
Import text engine.
/**
* Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/Ar2Y
*/
// forked from Henry.Tseng's Are the new text engine classes available?
/*
Import text engine.
*/
package {
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.text.engine.*;
public class CanvasType extends Sprite {
private var _oX:Number = stage.mouseX;//last mouse x position
private var _oY:Number = stage.mouseY;//last mouse y position
private var _velX:Number = 0;//velocity x per second
private var _velY:Number = 0;//velocity y per second
private var _speed:Number = 0;//change in position per second
private var _rt:Number = 0; // rotation
public function CanvasType() {
_init();
}
private function _init():void {
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, onUpHandler);
}
private function onDownHandler($e:MouseEvent) : void {
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMoveHandler);
_oX = stage.mouseX;
_oY = stage.mouseY;
}
private function onMoveHandler($e:MouseEvent) : void {
typeText();
calculateRotation();
var nX:Number = stage.mouseX;
var nY:Number = stage.mouseY;
var dx:Number = nX - _oX;
var dy:Number = nY - _oY;
_oX = nX;
_oY = nY;
_velX = dx; //per second
_velY = dy; //per second
_speed = Math.round(Math.sqrt( _velX * _velX + _velY * _velY));
//trace("speed: " + _speed + " rotation: " + _rt);
}
private function onUpHandler($e:MouseEvent) : void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveHandler);
}
/*---------------------------------------------------------------------------------------*/
private var i:int = 0;
private var str:String = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
private var textString:String;
private function calculateRotation() : void {
x = stage.mouseX;
y = stage.mouseY;
var newRotation:Number = Math.atan2(y - _oY, x - _oX) * 180 / Math.PI;
if (newRotation - _rt > 180)
newRotation -= 360;
else if (_rt - newRotation > 180)
newRotation += 360;
_rt -= (_rt - newRotation) / 10;
}
private function typeText() : void{
textString = str.substring(i, i + 1);
if(i == str.length - 1) {
i = 0;
} else {
i++;
}
var yPos:Number = stage.mouseY;
var fontDescription:FontDescription = new FontDescription("Trebuchet MS", FontWeight.NORMAL);
fontDescription.fontLookup = FontLookup.DEVICE;
var textBlock:TextBlock = new TextBlock();
fontDescription.fontPosture = FontPosture.NORMAL;
var format:ElementFormat = new ElementFormat(fontDescription, _speed * 2);
var textElement:TextElement = new TextElement(textString, format);
textBlock.content = textElement;
createLine(textBlock, yPos);
}
private function createLine(textBlock:TextBlock, yPos:Number):void {
var textLine:TextLine = textBlock.createTextLine();
var sp:Sprite = new Sprite();
stage.addChild(sp);
sp.addChild(textLine);
sp.x = stage.mouseX;
sp.y = yPos;
sp.rotation = _rt;
}
}
}