You are on page 1of 2

/** * Sin Wave Image Effect * @author Brandon Fogerty * @version 0.

1 * * This example demonstrates how to play * with pixel data with Flash and Action Script 3.0. * * May GOD Bless you Always! */ package { import import import import import flash.display.Bitmap; flash.display.BitmapData; flash.display.MovieClip; flash.utils.Timer; flash.events.TimerEvent;

public class SinePicApp extends MovieClip { // We will take the smile.jpg image and use it as a reference to access // pixels from. Next we will use a bmp to write our newly calcu lated pixels to. // Import my image. Create a class for it as well. [Embed(source='smile.JPG')] private var Smile:Class; private var smile:Bitmap; // We need a new bitmap to contain our newly calculated pixels r eferenced from our // Smile JPEG. private var bmp:Bitmap; // We will need a timer do animate the wave effect. private var time:Timer; private var angle:Number = 0.0; public function SinePicApp() { // Create a new Smile image instance. smile = new Smile(); smile.x = 0; smile.y = 0; // Create an empty bitmap filled with a black color. // We also set the size to that of our smile JPEG. bmp = new Bitmap(new BitmapData(smile.width, smile.heigh t,false,0x000000)); // Add our black bmp to the scene. addChild(bmp); // Create a timer and execute our step method every mili second. // the Step method is the real meat and potatoes of this

effect. time = new Timer(1); time.addEventListener(TimerEvent.TIMER,this.step); time.start(); // That's it, we are done! =) } public function step(timeEvent:TimerEvent):void { // Go through every pixel in our smile.jpg. // j represents the Y value. // i represents the X value. for(var j:Number=0; j < bmp.height; j++) { // Calculate our new X offset for each row of p ixels in our new bmp. // We use the trig sine function to make a wavy effect. This is // not very efficient at all. However to keep things simple, we will // use the Sine method. // The angle we use must be converted to radian s. That explains the // (angle * Math.PI) / 180 // The 10 represents the amplitude of the sine method. Play around with // this constant for various strengths. var x:Number = Math.round(10 * Math.sin(((angle) * Math.PI) / 180)); for(var i:Number=0; i<bmp.width; i++) { // Reference our original smile JPEG and place each pixel, one by one, // in our newly calculated spot. bmp.bitmapData.setPixel32(x,j,smile.bitm apData.getPixel32(i,j)); x++; } // Increment our angle. angle++; } // That's it! We are finished! =) } } }

You might also like