DistortImage
//////////////////////////////////////////////////////////////////////////////
[AS3.0] DistortImageを試すのだ!
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1136
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/aBDr
*/
////////////////////////////////////////////////////////////////////////////////
// [AS3.0] DistortImageを試すのだ!
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1136
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.system.LoaderContext;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var loader:Loader;
private var bitmapData:BitmapData;
private var canvas:Shape;
private var distortion:DistortImage;
private static var cx:uint = 232;
private static var cy:uint = 232;
private static var basePath:String = "http://assets.wonderfl.net/images/related_images/";
private static var imagePath:String = "c/c5/c5c1/c5c18f40978ea4273202e5b2ed211caf404ec9bf";
public function Main() {
//Wonderfl.capture_delay(1);
init();
}
private function init():void {
//bitmapData = new BitmapData(400, 400, false);
canvas = new Shape();
addChild(canvas);
canvas.x = cx;
canvas.y = cy;
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, initialize, false, 0, true);
loader.load(new URLRequest(basePath + imagePath), new LoaderContext(true));
}
private function initialize(evt:Event):void {
//bitmapData = Bitmap(evt.target.content).bitmapData.clone();
bitmapData = Bitmap(evt.target.content).bitmapData;
distortion = new DistortImage(bitmapData.rect.width, bitmapData.rect.height, 5, 5);
distort();
}
private function distort():void {
canvas.graphics.clear();
var p1:Point = new Point(-120, -160);
var p2:Point = new Point(120, -160);
var p3:Point = new Point(200, 160);
var p4:Point = new Point(-200, 160);
distortion.setTransform(canvas.graphics, bitmapData, p1, p2, p3, p4);
}
}
}
//////////////////////////////////////////////////
// DistortImageクラス
//////////////////////////////////////////////////
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.geom.Matrix;
import flash.geom.Point;
class DistortImage {
protected var _sMat:Matrix, _tMat:Matrix;
protected var _xMin:Number, _xMax:Number, _yMin:Number, _yMax:Number;
protected var _hseg:uint, _vseg:uint;
protected var _hsLen:Number, _vsLen:Number;
protected var _p:Array;
protected var _tri:Array;
protected var _w:Number, _h:Number;
public var smoothing:Boolean = true;
public function DistortImage (w:Number, h:Number, hseg:uint=2, vseg:uint=2):void {
_w = w;
_h = h;
_vseg = vseg;
_hseg = hseg;
__init();
}
protected function __init():void {
_p = new Array();
_tri = new Array();
var ix: Number;
var iy: Number;
var w2: Number = _w / 2;
var h2: Number = _h / 2;
_xMin = _yMin = 0;
_xMax = _w; _yMax = _h;
_hsLen = _w / ( _hseg + 1 );
_vsLen = _h / ( _vseg + 1 );
var x: Number, y: Number;
for ( ix = 0 ; ix <_vseg + 2 ; ix++ ){
for ( iy = 0 ; iy <_hseg + 2 ; iy++ ){
x = ix * _hsLen;
y = iy * _vsLen;
_p.push( { x: x, y: y, sx: x, sy: y } );
}
}
for ( ix = 0 ; ix <_vseg + 1 ; ix++ ){
for ( iy = 0 ; iy <_hseg + 1 ; iy++ ){
_tri.push([ _p[ iy + ix * ( _hseg + 2 ) ] , _p[ iy + ix * ( _hseg + 2 ) + 1 ] , _p[ iy + ( ix + 1 ) * ( _hseg + 2 ) ] ] );
_tri.push([ _p[ iy + ( ix + 1 ) * ( _hseg + 2 ) + 1 ] , _p[ iy + ( ix + 1 ) * ( _hseg + 2 ) ] , _p[ iy + ix * ( _hseg + 2 ) + 1 ] ] );
}
}
}
public function setTransform(graphics:Graphics, bmd:BitmapData, tl:Point, tr:Point, br:Point, bl:Point):void {
var dx30:Number = bl.x - tl.x;
var dy30:Number = bl.y - tl.y;
var dx21:Number = br.x - tr.x;
var dy21:Number = br.y - tr.y;
var l:Number = _p.length;
while( --l> -1 ){
var point:Object = _p[ l ];
var gx:Number = ( point.x - _xMin ) / _w;
var gy:Number = ( point.y - _yMin ) / _h;
var bx:Number = tl.x + gy * ( dx30 );
var by:Number = tl.y + gy * ( dy30 );
point.sx = bx + gx * ( ( tr.x + gy * ( dx21 ) ) - bx );
point.sy = by + gx * ( ( tr.y + gy * ( dy21 ) ) - by );
}
__render(graphics, bmd);
}
protected function __render(graphics:Graphics, bmd:BitmapData):void {
var t: Number;
var vertices: Array;
var p0:Object, p1:Object, p2:Object;
var a:Array;
_sMat = new Matrix();
_tMat = new Matrix();
var l:Number = _tri.length;
while( --l> -1 ){
a = _tri[ l ];
p0 = a[0];
p1 = a[1];
p2 = a[2];
var x0: Number = p0.sx;
var y0: Number = p0.sy;
var x1: Number = p1.sx;
var y1: Number = p1.sy;
var x2: Number = p2.sx;
var y2: Number = p2.sy;
var u0: Number = p0.x;
var v0: Number = p0.y;
var u1: Number = p1.x;
var v1: Number = p1.y;
var u2: Number = p2.x;
var v2: Number = p2.y;
_tMat.tx = u0;
_tMat.ty = v0;
_tMat.a = ( u1 - u0 ) / _w;
_tMat.b = ( v1 - v0 ) / _w;
_tMat.c = ( u2 - u0 ) / _h;
_tMat.d = ( v2 - v0 ) / _h;
_sMat.a = ( x1 - x0 ) / _w;
_sMat.b = ( y1 - y0 ) / _w;
_sMat.c = ( x2 - x0 ) / _h;
_sMat.d = ( y2 - y0 ) / _h;
_sMat.tx = x0;
_sMat.ty = y0;
_tMat.invert();
_tMat.concat( _sMat );
graphics.beginBitmapFill( bmd, _tMat, false, smoothing );
graphics.moveTo( x0, y0 );
graphics.lineTo( x1, y1 );
graphics.lineTo( x2, y2 );
graphics.endFill();
}
}
}