四角いBitmapを三角にする
ポリゴンを使わずに四角を三角にする
* 極座標変換へのプロローグ
/*
* ポリゴンを使わずに四角を三角にする
* 極座標変換へのプロローグ
*/
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Loader;
import flash.display.BitmapData;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.net.URLRequest;
import flash.system.Security;
import flash.events.Event;
//import flash.events.ProgressEvent;
import flash.events.MouseEvent;
[SWF(backgroundColor=0x000000)]
public class Take05 extends Sprite {
private const SW:Number = stage.stageWidth;
private const SH:Number = stage.stageHeight;
private var loader:Loader;
private var textfield:TextField;
private var topWidth:Number;
private var topCenter:Number;
private var myRect:Sprite;
public function Take05():void {
// infomation
var textFormat:TextFormat = new TextFormat();
textFormat.color = 0xffffff;
textfield = new TextField();
textfield.defaultTextFormat = textFormat;
textfield.setTextFormat(textFormat);
textfield.autoSize = TextFieldAutoSize.LEFT;
textfield.text = 'NowLoading...';
addChild(textfield);
// Bitmap読み込み
var url:String = "http://www.seeda.jp/";
Security.loadPolicyFile(url + "crossdomain.xml");
loader = new Loader();
loader.load(new URLRequest(url + "flash/visitor.jpg"));
//loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
}
private function sceenMotion(e:Event):void {
var v:uint;
var shape:Object;
if (topWidth > 0) {
// 四角から三角へ
topWidth*=0.8;
if (topWidth < 0.5) {
topWidth = 0;
topCenter = -myRect.width/2;
}
for (v = 0; v < myRect.height; v++) {
shape = myRect.getChildAt(v);
shape.width = topWidth + (myRect.width-topWidth) * ((v+1)/myRect.height);
}
} else if (topCenter != 0) {
// 三角の頂点を真ん中へ
topCenter*=0.48;
if (Math.abs(topCenter) < 0.5) {
topCenter = 0;
}
for (v = 0; v < myRect.height; v++) {
shape = myRect.getChildAt(v);
shape.x = (myRect.width/2 + topCenter) * ((myRect.height-v)/myRect.height);
}
}
}
private function sceenChange(e:MouseEvent):void {
textfield.text = "from Rect to Triangle";
e.target.removeEventListener(MouseEvent.CLICK, sceenChange);
e.target.addEventListener(Event.ENTER_FRAME, sceenMotion);
}
/*
private function onProgress(e:ProgressEvent):void {
//trace ("Loaded:" + e.bytesLoaded);
//trace ("Total:" + e.bytesTotal);
//trace ("Persent:" + Math.floor(e.bytesLoaded / e.bytesTotal * 100));
}
*/
private function onComplete(e:Event):void {
// 見た目は普通のBitmapだけど、実体は横線状のSpriteの集合体
var bitmapData:BitmapData = new BitmapData(loader.content.width, loader.content.height,true);
bitmapData.draw(loader);
myRect = new Sprite();
for (var v:uint = 0; v < bitmapData.height; v++) {
var shape:Shape = new Shape();
shape.graphics.beginBitmapFill(bitmapData);
shape.graphics.drawRect(0,v,bitmapData.width,1);
shape.graphics.endFill();
myRect.addChild(shape);
}
myRect.x = (SW-myRect.width)/2;
myRect.y = (SH-myRect.height)/2;
addChild(myRect);
topWidth = myRect.width;
// Start message
textfield.text = "Click Screen";
stage.addEventListener(MouseEvent.CLICK, sceenChange);
}
}
}