Transform with drawTriangles on 2009-1-14
package
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.system.LoaderContext;
[SWF(width="465", height="465", frameRate="24", backgroundColor="0xFFFFFF")]
public class ErasticPoint extends MovieClip
{
private var numRow:int = 31;
private var numCol:int = 31;
private var bmp:Bitmap;
private var vertices:Vector.<Number>;
private var uvtData:Vector.<Number>;
private var indices:Vector.<int>;
private var pts:Vector.<Point>;
public function ErasticPoint()
{
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
private function addedToStage(e:Event):void
{
removeEventListener(e.type, arguments.callee);
var l:Loader = new Loader();
var c:LoaderContext = new LoaderContext(true);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
l.load(new URLRequest('http://www.matzmtok.com/wonderfl/black_QP.png'), c);
}
private function loaderCompleteHandler(e:Event):void
{
var li:LoaderInfo = e.target as LoaderInfo;
bmp = li.content as Bitmap;
var w:Number = bmp.bitmapData.width;
var h:Number = bmp.bitmapData.height;
var iw:Number = w / numCol;
var ih:Number = h / numRow;
pts = new Vector.<Point>();
vertices = new Vector.<Number>();
uvtData = new Vector.<Number>();
indices = new Vector.<int>();
for ( var j:int = 0; j < numRow + 1; j++) {
for (var i:int = 0; i < numCol + 1; i++) {
pts.push(new Point(i * iw, j * ih));
vertices.push(i * iw);
vertices.push(j * ih);
uvtData.push(i * iw / w);
uvtData.push(j * ih / h);
}
}
for ( j = 0; j < numRow; j++) {
for ( i = 0; i < numCol; i++) {
indices.push(
j * (numCol+1) + i,
j * (numCol+1) + i + 1,
(j + 1) * (numCol+1) + i,
j * (numCol+1) + i + 1,
(j + 1) * (numCol+1) + i,
(j + 1) * (numCol+1) + i + 1);
}
}
addEventListener(Event.ENTER_FRAME, enterFrame);
}
private function enterFrame(e:Event):void
{
var f:Point;
graphics.clear();
for ( var i:int = 0; i < pts.length; i++) {
f = getForce(pts[i]);
vertices[int(i * 2)] = pts[i].x + f.x;
vertices[int(i * 2) + 1] = pts[i].y + f.y;
}
graphics.beginBitmapFill(bmp.bitmapData);
graphics.drawTriangles(vertices, indices, uvtData);
graphics.endFill();
}
private function getForce(p:Point):Point {
var f:Point = new Point();
var dx:Number = p.x - mouseX;
var dy:Number = p.y - mouseY;
var pow:Number = dx * dx + dy * dy;
if (pow < 20000) {
pow = 20000;
}
f.x = dx;
f.y = dy;
f.normalize(1000000/pow);
return f;
}
}
}