forked from: doraを描いてみた
// forked from chabudai's doraを描いてみた
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.events.Event;
[SWF(width="500", height="500", backgroundColor="0xFFFFFF", frameRate="30")]
public class Sketch extends Sprite
{
private const matrix:Matrix = new Matrix();
private const rect:Rectangle = new Rectangle(0, 0, stage.stageWidth >> 1, stage.stageHeight >> 1);
private var _target:Sprite;
private var _box:Sprite;
private var _bmpList:Array;
private var _bmpDat:BitmapData;
public function Sketch()
{
createTarget();
createBitmap();
addEFListener();
}
private function createTarget():void
{
_target = new Sprite();
_target.graphics.beginFill(0xF0F0F0);
_target.graphics.drawRect(0, 0, 500, 500);
_target.graphics.endFill();
}
private function createDora():void
{
var randomScale:Number = Math.random() + 2;
dora();
_box = sp;
_box.scaleX = _box.scaleY = randomScale;
_target.addChild(_box);
}
private function createBitmap():void
{
var $bmp:Bitmap;
_bmpList = new Array();
_bmpDat = new BitmapData(stage.stageWidth >> 2, stage.stageHeight >> 2, true, 0xFFFFFF);
for (var i:int = 0; i < 4; ++i)
{
for (var j:int = 0; j < 4; ++j)
{
$bmp = new Bitmap(_bmpDat);
$bmp.x = i * stage.stageWidth >> 2;
$bmp.y = j * stage.stageHeight >> 2;
addChild($bmp);
_bmpList.push($bmp);
}
}
}
private function addEFListener():void
{
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(evt:Event):void
{
createDora();
_box.x = mouseX;
_box.y = mouseY;
updateBitmap();
}
private function updateBitmap():void
{
var tmpS:Number = _target.scaleX;
var $bmp:Bitmap;
matrix.a = 1 / 4;
matrix.d = 1 / 4;
_target.scaleX = _target.scaleY = .1;
_bmpDat.lock();
_bmpDat.fillRect(rect, 0xFFFFFFFF);
_bmpDat.draw(_target, matrix);
_bmpDat.unlock();
for (var i:int = 0; i < _bmpList.length; ++i)
Bitmap(_bmpList[i]).bitmapData = _bmpDat;
_target.scaleX = _target.scaleY = tmpS;
}
public var sp:Sprite;
private var centerX:Number;
private var centerY:Number;
public function dora() {
centerX = stage.stageWidth / 2;
centerY = stage.stageHeight / 2;
sp = new Sprite();
sp.x = centerX;
sp.y = centerY;
addChild(sp);
var head:Sprite = new Sprite();
var w:Number = 94;
var h:Number = 82;
head.graphics.beginFill(0xFFFFFF * Math.random());
head.graphics.moveTo(-w/2, 0);
head.graphics.curveTo(-w/2+2, -h/2+1, 0, -h/2);
head.graphics.curveTo(w/2-2, -h/2+1, w/2, 0);
head.graphics.curveTo(w/2-1, h/2-1, 0, h/2);
head.graphics.curveTo(-w/2+1, h/2-1, -w/2, 0);
head.graphics.endFill();
var face:Sprite = new Sprite();
face.y = 7;
w = 86;
h = 67;
face.graphics.beginFill(0xFFFFFF);
face.graphics.moveTo(-w/2, 0);
face.graphics.curveTo(-w/2+2, -h/2+1, 0, -h/2);
face.graphics.curveTo(w/2-2, -h/2+1, w/2, 0);
face.graphics.curveTo(w/2-1, h/2-1, 0, h/2);
face.graphics.curveTo( -w/2+1, h/2 - 1, -w/2, 0);
var lefteye:Sprite = new Sprite();
lefteye.x = -17.5;
lefteye.y = -35;
drawOval(lefteye, 17, 19);
var eye1:Sprite = new Sprite();
eye1.graphics.beginFill(0x000000);
eye1.graphics.drawEllipse(0, 0, 8, 2);
eye1.x = -14;
eye1.y = -27;
var righteye:Sprite = new Sprite();
righteye.x = 0.5;
righteye.y = -35;
drawOval(righteye, 17, 19);
var eye2:Sprite = new Sprite();
eye2.graphics.beginFill(0x000000);
eye2.graphics.drawEllipse(0, 0, 8, 2);
eye2.x = 6;
eye2.y = -27;
var nose:Sprite = new Sprite();
nose.x = -5.5;
nose.y = -20;
drawOval(nose, 11, 9);
var noseline:Sprite = new Sprite();
noseline.graphics.lineStyle(1, 0x000000);
noseline.graphics.moveTo( -0.5, -18);
noseline.graphics.lineTo( -0.5, 24);
var mouse:Sprite = new Sprite();
mouse.graphics.lineStyle(1, 0x000000);
mouse.graphics.moveTo( -31, 6);
mouse.graphics.curveTo( -24, 24, 0, 24);
mouse.graphics.curveTo(24, 24, 31, 6);
var higeR:Sprite = new Sprite();
higeR.graphics.lineStyle(1, 0x000000);
higeR.graphics.moveTo( -16, -13);
higeR.graphics.lineTo( -35, -16);
higeR.graphics.moveTo( -17, -9);
higeR.graphics.lineTo( -36, -9);
higeR.graphics.moveTo( -16, -5);
higeR.graphics.lineTo( -35, -2);
var higeL:Sprite = new Sprite();
higeL.graphics.lineStyle(1, 0x000000);
higeL.graphics.moveTo( 16, -13);
higeL.graphics.lineTo( 35, -16);
higeL.graphics.moveTo( 17, -9);
higeL.graphics.lineTo( 36, -9);
higeL.graphics.moveTo( 16, -5);
higeL.graphics.lineTo( 35, -2);
sp.addChild(head);
sp.addChild(face);
sp.addChild(lefteye);
sp.addChild(righteye);
sp.addChild(eye1);
sp.addChild(eye2);
sp.addChild(nose);
sp.addChild(noseline);
sp.addChild(mouse);
sp.addChild(higeR);
sp.addChild(higeL);
}
private function drawOval(s:Sprite, w:Number, h:Number):void {
s.graphics.beginFill(0xFFFFFF, 100);
s.graphics.moveTo(0, h/2);
s.graphics.curveTo(0.5, 0.5, w/2, 0);
s.graphics.curveTo(w-0.5, 0.5, w, h/2);
s.graphics.curveTo(w-0.5, h-0.5, w/2, h);
s.graphics.curveTo(0.5, h-0.5, 0, h/2);
s.graphics.endFill();
}
}
}