わかめDora
http://wonderfl.kayac.com/code/0c466cd7da658de776c22d5c5cc86f02a0148d21
// forked from trick7's わかめペン簡易版
// forked from trick7's code on 2008-12-18
// http://wonderfl.kayac.com/code/0c466cd7da658de776c22d5c5cc86f02a0148d21
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Sprite;
import flash.filters.ColorMatrixFilter;
import flash.filters.ConvolutionFilter;
import flash.filters.DisplacementMapFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
[SWF(backgroundColor="0x000000", frameRate="60")]
public class WakameDora extends Sprite{
private var _sp:Sprite;
private var _drawingBoard:Sprite;
private var _drawingStockBMD:BitmapData;
private var _drawingStock:Bitmap;
private var _baseBmpd:BitmapData;
private var _viewBm:Bitmap;
private var _displaceImage:BitmapData;
private var _clrMatrix:ColorMatrixFilter;
private var _seed:Number = Math.random();
private var _offsets:Array = [new Point(), new Point()];
private var _mousedown:Boolean = false;
public function WakameDora() {
_sp = new Sprite();
_drawingBoard = new Sprite();
//ペンの太さと初期色
_drawingBoard.graphics.lineStyle(5, 0x00ffffff);
addChild(_sp);
addEventListener(Event.ENTER_FRAME, run);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownListener);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpListener);
//描画スプライトのストック用ビットマップ。背景はアルファ0
_drawingStockBMD = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
_drawingStock = new Bitmap(_drawingStockBMD);
_sp.addChild(_drawingStock)
_sp.addChild(_drawingBoard);
//表示用bitmap
_baseBmpd = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xff000000);
_viewBm = new Bitmap(_baseBmpd,"auto",true);
_viewBm.blendMode = BlendMode.SCREEN;
addChild(_viewBm);
_displaceImage = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFFFF);
//消去ボタン
var btn:Sprite = new Sprite();
btn.graphics.beginFill(0x666666);
btn.graphics.drawCircle(20, 20, 15);
btn.buttonMode = true;
addChild(btn);
btn.addEventListener(MouseEvent.CLICK, onEraceListener);
}
private function run(e:Event):void {
if (_mousedown) {
//_drawingBoard.graphics.lineStyle(5,Math.random()*0xffffff)
//_drawingBoard.graphics.lineTo(mouseX, mouseY);
var tmpdora:Dora = new Dora();
_drawingBoard.addChild(tmpdora);
tmpdora.x = mouseX;
tmpdora.y = mouseY;
}
//置き換えマップを動かす。
for(var i:int = 0; i < 2; ++i){
_offsets[i].x += 5; //X方向
_offsets[i].y += 1; //Y方向
}
//もやもやした白黒画像を生成
_displaceImage.perlinNoise(50, 80, 1, _seed, false, true, 1, true, _offsets);
var f:DisplacementMapFilter = new DisplacementMapFilter(_displaceImage, null, 1, 1, 10, 20, "color", 0x0);
//displacementMapFilter
_baseBmpd.applyFilter(_baseBmpd,new Rectangle(0,0,_baseBmpd.width,_baseBmpd.height),new Point(0,0),f);
//convolutionFilter
_baseBmpd.applyFilter(_baseBmpd,new Rectangle(0,0,_baseBmpd.width,_baseBmpd.height),new Point(0,0),set_cf());
//描画(ここをコメントアウトするとわかめフィルタ効果をオフにできる。完全に切るときは上の該当処理も消すこと。)
_baseBmpd.draw(_sp);
}
private function set_cf():ConvolutionFilter{//ConvolutionFilter
var m1:Number = 1; var m2:Number = 0; var m3:Number = 0;
var m4:Number = 1; var m5:Number = 1; var m6:Number = 1;
var m7:Number = 0; var m8:Number = 0; var m9:Number = 0;
var ma:Number = .3;
var mt:Number = m1 + m2 +m3 + m4 +m5 + m6 +m7 + m8 +m9 + ma;
var cf:ConvolutionFilter = new ConvolutionFilter(3,3, [m1,m2,m3, m4,m5,m6, m7,m8,m9 ],mt,0,false);
return cf;
}
private function onMouseDownListener(event:MouseEvent):void {
_mousedown = true;
//ペン先を移動。+1しているのは点が描けるようにする処理
//_drawingBoard.graphics.moveTo(mouseX, mouseY+1 );
/*
var tmpdora:Dora = new Dora();
_drawingBoard.addChild(tmpdora);
tmpdora.x = mouseX;
tmpdora.y = mouseY;
*/
}
private function onMouseUpListener(event:MouseEvent):void {
_mousedown = false;
//lineTo で描画されたグラフィックをキャプチャ
_drawingStockBMD.draw(_drawingBoard);
//キャプチャ後、ラインは不要なのでクリアしておく
_drawingBoard.graphics.clear();
}
private function onEraceListener(event:MouseEvent):void {
//キャプチャ用ビットマップをクリア
trace("hoge")
_drawingStockBMD.fillRect(new Rectangle(0,0,stage.stageWidth, stage.stageHeight),0x000000);
_drawingBoard.graphics.clear();
while ( _drawingBoard.numChildren > 0 ) { _drawingBoard.removeChildAt( 0 ); }
}
}
}
import flash.display.*;
import flash.geom.*;
class Dora extends Sprite{
public function Dora() {
var sp:Sprite = new Sprite();
//sp.filters = [new GlowFilter(0xBB44BB,0.4,32,32)];
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();
sp.addChild(head);
//顔の輪郭を描く
var face:Sprite = new Sprite();
face.y = 7;
w = 86;
h = 67;
face.graphics.beginFill(0x000000);
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);
sp.addChild(face);
//目を描く
var lefteye:Sprite = new Sprite();
lefteye.x = -17.5;
lefteye.y = -35;
drawOval(lefteye, 17, 19);
sp.addChild(lefteye);
var eye1:Sprite = new Sprite();
eye1.graphics.beginFill(0x000000);
eye1.graphics.drawEllipse(0, 0, 8, 2);
eye1.x = -14;
eye1.y = -27;
sp.addChild(eye1);
var righteye:Sprite = new Sprite();
righteye.x = 0.5;
righteye.y = -35;
drawOval(righteye, 17, 19);
sp.addChild(righteye);
var eye2:Sprite = new Sprite();
eye2.graphics.beginFill(0x000000);
eye2.graphics.drawEllipse(0, 0, 8, 2);
eye2.x = 6;
eye2.y = -27;
sp.addChild(eye2);
//鼻を描く
var nose:Sprite = new Sprite();
nose.x = -5.5;
nose.y = -20;
drawOval(nose, 11, 9);
sp.addChild(nose);
//鼻下を描く
var noseline:Sprite = new Sprite();
noseline.graphics.lineStyle(1, 0xFFFFFF);
noseline.graphics.moveTo( -0.5, -18);
noseline.graphics.lineTo( -0.5, 24);
sp.addChild(noseline);
//口を描く
var mouse:Sprite = new Sprite();
mouse.graphics.lineStyle(1, 0xFFFFFF);
mouse.graphics.moveTo( -31, 6);
mouse.graphics.curveTo( -24, 24, 0, 24);
mouse.graphics.curveTo(24, 24, 31, 6);
sp.addChild(mouse);
//ヒゲを描く
var higeR:Sprite = new Sprite();
higeR.graphics.lineStyle(1, 0xFFFFFF);
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);
sp.addChild(higeR);
var higeL:Sprite = new Sprite();
higeL.graphics.lineStyle(1, 0xFFFFFF);
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(higeL);
var bmpd:BitmapData = new BitmapData(width, height, true, 0x00ffffff);
var m:Matrix = new Matrix();
m.scale(0.5, 0.5);
m.translate(width/2, height/2);
bmpd.draw(this, m);
var b:Bitmap = addChild(new Bitmap(bmpd)) as Bitmap;
removeChild(sp);
sp = null;
b.x -= width/2;
b.y -= height/2;
}
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();
}
}