PDA

View Full Version : Image Distortion Class



rai_kane3
March 20th, 2008, 09:07 AM
I have been racking my brain with trying to convert http://www.rubenswieringa.com/blog/distortimage into as3 im am very unfamilier with flex so I have become somewhat stuck can anyone help me.

I have set the buttons for transform and loading the image but its just not working at all.

update here is the image distortion class (http://www.flashsandy.org/download)


package
{
import flash.display.Shape;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Loader;

import flash.events.Event;
import flash.events.MouseEvent;

import flash.geom.Point;

import flash.net.URLRequest;
import flash.net.URLLoader;

import DistortImage;

public class PrTransform extends MovieClip
{
private var shape:Shape;
private var image:Sprite;
private var loader:Loader;
private var bitmapData:BitmapData;

private var preparationRecursion:int;
private var distortion:DistortImage;
private var showGrid:Boolean;

private var dragTL:dragBtn;
private var dragTR:dragBtn;
private var dragBL:dragBtn;
private var dragBR:dragBtn;

private var center:Point;

public function PrTransform()
{
loader = new Loader();
image = new Sprite();
shape = new Shape();
//bitmapData = new BitmapData();

preparationRecursion = 0;
distortion = new DistortImage(574, 377);
showGrid = false;

addChild(image);
creationComplete();

dragTL = new dragBtn();
dragTR = new dragBtn();
dragBL = new dragBtn();
dragBR = new dragBtn();

this.addChild(dragTL);
this.addChild(dragTR);
this.addChild(dragBL);
this.addChild(dragBR);

dragTL.addEventListener(MouseEvent.MOUSE_DOWN, tlStart);
dragTL.addEventListener(MouseEvent.MOUSE_UP, tlStop);
dragTR.addEventListener(MouseEvent.MOUSE_DOWN, trStart);
dragTR.addEventListener(MouseEvent.MOUSE_UP, trStop);
dragBL.addEventListener(MouseEvent.MOUSE_DOWN, blStart);
dragBL.addEventListener(MouseEvent.MOUSE_UP, blStop);
dragBR.addEventListener(MouseEvent.MOUSE_DOWN, brStart);
dragBR.addEventListener(MouseEvent.MOUSE_UP, brStop);

center = new Point();
}

private function creationComplete():void
{
trace("CREATION: ");
shape.x = shape.y = 50;
addChild(shape);
image.visible = true;
addChildAt(image, 0);
loadImage();

}

private function onEnterFrame(event:Event):void
{
trace("SET TRANSFORM: ");
shape.graphics.clear();
distortion.setTransform(shape.graphics, bitmapData,
getDisplayObjectCenter(dragTL, -50, -50),
getDisplayObjectCenter(dragTR, -50, -50),
getDisplayObjectCenter(dragBL, -50, -50),
getDisplayObjectCenter(dragBR, -50, -50));

}


private function getDisplayObjectCenter (child:DisplayObject, x:Number = 0, y:Number = 0):Point
{
center.x = child.x + child.width/2 + x;
center.y = child.y + child.height/2 + y;
return center;
}

private function loadImage():void
{
trace("LOAD IMAGE: ")
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, onLoadComplete);
loader.load(new URLRequest("4m.png"));
}

private function onLoadComplete(event:Event):void
{
image.addChild(this.loader);
dragTL.x = 0;
dragTL.y = 0;
dragTR.x = image.width;
dragTR.y = 0;
dragBL.x = 0;
dragBL.y = image.height;
dragBR.x = image.width;
dragBR.y = image.height;

}

private function prepareDistortion (event:Event):void {
// if this method has called itself for more than 25 times something must be wrong:
this.preparationRecursion++;
if (this.preparationRecursion >= 25){
return;
}
// fetch BitmapData:
this.bitmapData = new BitmapData(image.width, image.height);
this.bitmapData.draw(this.image);
// remove Image from Stage and start calling the onEnterFrame() method:
this.removeChild(this.image);
this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
}

private function tlStart(e:MouseEvent):void
{
startDrag();
}
private function trStart(e:MouseEvent):void
{
startDrag();
}
private function blStart(e:MouseEvent):void
{
startDrag();
}
private function brStart(e:MouseEvent):void
{
startDrag();
}
private function tlStop(e:MouseEvent):void
{
stopDrag();
}
private function trStop(e:MouseEvent):void
{
stopDrag();
}
private function blStop(e:MouseEvent):void
{
stopDrag();
}
private function brStop(e:MouseEvent):void
{
stopDrag();
}


}
}

rai_kane3
March 21st, 2008, 12:35 PM
push--- please anyone????

Riddler?
March 21st, 2008, 12:43 PM
I'm pretty sure this has already been done.. http://www.flashsandy.org/blog/distortimage-in-as3.html
Is that what you are needing?

rai_kane3
March 22nd, 2008, 06:08 AM
yeah I have seen those before however all those examples are built in FLEX which I absolutly no experience in I have tried convering it which is the code you see attached but it just doesnt work. its supposed to work like this (http://www.rubenswieringa.com/code/as3/flex/DistortImage/)
but no joy.

rai_kane3
March 25th, 2008, 10:13 AM
ok after 0 help and a nightmare of looking at tutorials and flex source code finally got this working so here (http://www.bennraistrick.co.uk/transform/image_distort.zip)for those who would like it [AS3] (NO FLEX CODE)

example in source and experiments

thats all folks
March 25th, 2008, 10:28 AM
rai_kane3,

Wish I had the knowledge to have help, but thanks for sharing! It's nice to see even with no help that you posted a solution for others to learn from!!!

tastydesignboy
December 2nd, 2009, 08:41 AM
ok after 0 help and a nightmare of looking at tutorials and flex source code finally got this working so here (http://www.bennraistrick.co.uk/transform/image_distort.zip)for those who would like it [AS3] (NO FLEX CODE)

example in source and experiments

Hi rai_kane3 (http://www.kirupa.com/forum/member.php?u=28124) your link seems to be broken! Any chance of a new one?

tastydesignboy
December 18th, 2009, 09:57 AM
Any chance of a re post?