うずまき
マウスドラッグでもっと渦巻き
/**
* マウスドラッグでもっと渦巻き
*/
package {
import com.flashdynamix.utils.SWFProfiler;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BitmapFilter;
import flash.filters.DisplacementMapFilter;
import flash.filters.DisplacementMapFilterMode;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.system.LoaderContext;
[SWF(backgroundColor="0xFFFFFF", width="465", height="465", frameRate="60")]
public class Main7 extends Sprite {
private static const WIDTH_STAGE:uint = 465;
private static const HEIGHT_STAGE:uint = 465;
public function Main7() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private var _jpeg:Bitmap;
private var _canvas:Sprite = new Sprite();
private var _nowDmfsValue:uint = 0;
private var _mapBitmaps:Vector.<BitmapData> = new Vector.<BitmapData>();
private var _mapBitmapNo:uint = 0;
private var _radius:Number = 100;
private var _dmf:DisplacementMapFilter;
private var _maxDistance:Number;
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
SWFProfiler.init(this);
Wonderfl.capture_delay( 3 );
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
var loader:Loader = new Loader();
loader.load(new URLRequest("http://dust.heteml.jp/wonderfl/image/beck.jpg"), context);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function():void {
_jpeg = Bitmap(loader.content);
loader.contentLoaderInfo.removeEventListener(this, arguments.callee);
start();
});
}
private function start():void {
_canvas.addChild(_jpeg);
_canvas.x = (WIDTH_STAGE - _canvas.width) / 2;
_canvas.y = (HEIGHT_STAGE - _canvas.height) / 2;
addChild(_canvas);
_maxDistance = _radius * 1.2;//適当 適宜調整
_dmf = new DisplacementMapFilter(
null,
new Point(),
BitmapDataChannel.BLUE,//x方向の移動に使う色
BitmapDataChannel.GREEN,//y方向の移動に使う色
_maxDistance * 2,
_maxDistance * 2,
DisplacementMapFilterMode.COLOR
);
_mapBitmaps.push(createMapBitmap(180));
_mapBitmaps.push(createMapBitmap(360));
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function createMapBitmap(maxRaddian:Number):BitmapData {
var mapBitmap:BitmapData = new BitmapData(_radius * 2, _radius * 2, false);
var yMax:int = _radius * 2;
var xMax:int = _radius * 2;
var center:Number = _radius;
var maxRaddian:Number = (Math.PI / 180) * maxRaddian;
var m:Matrix = new Matrix();
var p:Point = new Point();
mapBitmap.lock();
for (var xx:Number = 0; xx < xMax; ++xx) {
for (var yy:Number = 0; yy < yMax; ++yy) {
var dx:Number = xx - center;
var dy:Number = yy - center;
var distance:Number = Math.sqrt(dx * dx + dy * dy);
if(distance < _radius){
var powerRate:Number = 1 - (distance / _radius);
//円の真ん中を中心として、回転させる行列をつくる。
//円の中心に行くにしたがって回転が強くなるようにする。
m.identity();
m.translate(-center, -center);
m.rotate(maxRaddian * powerRate);
m.translate(center, center);
//現在の(xx, yy)の地点が移動先と考える。
//移動前の座標を求めるので、逆行列を求める
m.invert();
p.x = xx;
p.y = yy;
var applyPoint:Point = m.transformPoint(p);
var rateX:Number = (applyPoint.x - xx) / _maxDistance;
var rateY:Number = (applyPoint.y - yy) / _maxDistance;
var colorX:Number = Math.round(rateX * 0x7F) + 0x80;
var colorY:Number = Math.round(rateY * 0x7F) + 0x80;
var color:uint = colorY << 8 | colorX;
}
else {
color = 0x808080;
}
mapBitmap.setPixel(xx, yy, color);
}
}
mapBitmap.unlock();
return mapBitmap;
}
private function mouseDownHandler(e:MouseEvent):void {
_mapBitmapNo = 1;
}
private function mouseUpHandler(e:MouseEvent):void {
_mapBitmapNo = 0;
}
//マウス中心に動かす
private function enterFrameHandler(e:Event):void {
_dmf.mapBitmap = _mapBitmaps[_mapBitmapNo];
var p:Point = _dmf.mapPoint;
p.x = _canvas.mouseX - _radius;
p.y = _canvas.mouseY - _radius;
_dmf.mapPoint = p;
_jpeg.filters = [_dmf];
}
}
}