// forked from borealkiss's 毛文字 package { /** * どんな仕組みになっているか気になったので勉強をかねて、使いやすくリファクタリング */ import flash.display.BitmapData; import flash.display.DisplayObject; import flash.display.Sprite; import flash.text.TextFormat; [SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#ffffff")] public class Main extends Sprite { private const TEXT:String = "毛文字"; public function Main() { var hairyText:DisplayObject = createHairyText( TEXT, new TextFormat( null, 100 ), 0.5 ); hairyText.x = ( stage.stageWidth - hairyText.width ) / 2; hairyText.y = ( stage.stageHeight - hairyText.height ) / 2; addChild( hairyText ); } } } import flash.display.Bitmap; import flash.display.DisplayObject; import flash.display.Sprite; import flash.display.Graphics; import flash.display.BitmapData; import flash.geom.Matrix; import flash.geom.Rectangle; import flash.text.TextFormat; function createHairyText( text:String, textFormat:TextFormat = null, limit:Number = 1, longHairLength:Number = 0, alpha:Number = 0.5 ):DisplayObject { if ( !textFormat ) textFormat = new TextFormat(); if ( !longHairLength ) longHairLength = ( ( textFormat.size ) ? Number( textFormat.size ) : 12 ) / 10; // 12 は Flash Player 固有のフォーマットのフォントサイズ var bitmapData:BitmapData = textToBitmapData( text, textFormat ); var sprite:Sprite = new Sprite(); var graphics:Graphics = sprite.graphics; graphics.clear(); graphics.lineStyle( 0, uint( textFormat.color ), alpha ); for ( var i:uint = 0; i < ( bitmapData.width * bitmapData.height ) * limit; i++ ) { var x:uint = ( limit != 1 ) ? Math.random() * bitmapData.width : i % bitmapData.width; var y:uint = ( limit != 1 ) ? Math.random() * bitmapData.height : uint( i / bitmapData.width ); var color32:uint = bitmapData.getPixel32( x, y ); if ( color32 != 0 ) { //Anchor point so that the hair goes outer. var hairLength:Number = longHairLength * Math.random(); var hairRadian:Number = Math.random() * 2 * Math.PI; var anchorX:Number = x + hairLength * Math.cos( hairRadian ); var anchorY:Number = y + hairLength * Math.sin( hairRadian ); //Control point for the Bezier curve. var controlDistance:Number = longHairLength * Math.random(); var controlRadian:Number = Math.random() * 2 * Math.PI; var controlX:Number = x + controlDistance * Math.cos( controlRadian ); var controlY:Number = y + controlDistance * Math.sin( controlRadian ); graphics.moveTo( x, y ); graphics.curveTo( controlX, controlY, anchorX, anchorY ); } } return sprite; } import flash.display.BitmapData; import flash.geom.Matrix; import flash.text.TextField; import flash.text.TextFormat; /** * テキストをビットマップデータに変換する関数 */ function textToBitmapData( text:String, textFormat:TextFormat = null ):BitmapData { // テキストフィールドの作成 var textField:TextField = new TextField(); textField.text = text; // テキストフォーマットの適用 if( textFormat ) textField.setTextFormat( textFormat ); // テキストフィールドの幅、高さを調節。2 * 2 の意味は参考リンク参照。 textField.width = textField.textWidth + 2 * 2; textField.height = textField.textHeight + 2 * 2; // ビットマップデータの作成 var bitmapData:BitmapData = new BitmapData( textField.width - 2 * 2, textField.height - 2 * 2, true, 0 ); // テキストフィールドをビットマップデータ上に描画。 bitmapData.draw( textField, new Matrix(1, 0, 0, 1, -2, -2) ); return bitmapData; } forked from: 毛文字