Right well, I have seen many different demos and examples of how to create unique, random art by code. Now this looks very cool, but how on earth do you recreate this? Well the is what I am going to show you. There final piece can be downloaded here. As you can see, this is quite a cool piece of generative art. Now I am going to show you how to achieve this.
Step 1:
First of fire up Flash CS3. If you don’t have a copy you can download a trial from Adobes site. Right, now create a new Flash Actionscript 3 document. The next thing to do is set up the stage. I kept my dimensions of my Flash file as the default, 550px x 400px. Also set the frame rate to about 30fps. This stops the animation from appearing to be slow, and chuggy.
Now for this tutorial you need a simple graphic that will be used as our starting point for the animation. So, draw a simple circle on the stage, make sure its not very bit (maximum 100×100) and then select it all and press F8. This will present you with a box that will let you create a movie clip. Set the name to ‘Circle’, and then set the type to Movie Clip. Now, there should be a Linkage section, but if its not present, click the Advanced button in the bottom right hand corner.
In the linkage section there should be a check-box saying “Export for Actionscript”. Once this is clicked, some of the boxes that were greyed out should be come editable. There should be a box saying Linkage ID (or Symbol name), or something similar. If it is not already containing the word ‘Circle’, adjust it accordingly.
So now click OK. There might be a warning come up, but just click OK. The next thing to do is the delete the graphic that you created from the stage. All you need to do is select it (single click) and then press DELETE. This will delete it from the stage, but it will still be in the library.
Step 2:
Now we have the flash file set up, click on the first frame of the timeline and press F9 to access the actionscript editor.
So, now to set up everything that we need for the tweening etc. So click in the first line and add this code:
import gs.TweenLite; import gs.easing.Expo;
For this to work you need to have TweenLite in your class path. You can download it from here. If you don’t have a special class path just unzip the file that you have just downloaded and copy the folders to the same directory that your Flash file is.
So with the appropriate imports done, now we need to set up some basic variables that will make our development much easier:
var amountOfCircles:Number = 30; var stageW:Number = stage.stageWidth; var stageH:Number = stage.stageHeight; var ranX:Number = Math.random() * stageW - 300; var ranY:Number = Math.random() * stageH - 300; var ranAlpha:Number = Math.random() * .5;
Now here’s an explanation of what these lines are for.
The first one is fairly obvious. This is how many difference instances of the graphic you created earlier we will create. As you can see from the completed version there are multiple circles flying around.
The next two contain the width and height of the stage. This is purley to make it easier to reference to the value of the width of the stage. Basically it saves more typing.
The two after that are variables that hold a random X and Y value that are within the width of the stage.
The last one is a random alpha value between 0 - 50. This means that the graphic will never be completely opaque.
Step 3:
Now we need to create the 3 filters that we are going to create. There are two blur filters and a glow filter. You will see soon how we animate and apply them to out graphic.
var blur:BlurFilter = new BlurFilter(10, 10, 1); var blurBM:BlurFilter = new BlurFilter(20, 0, 1); var glowBM:GlowFilter = new GlowFilter(Math.round( Math.random()*0xFFFFFF ));
It is fairly obvious which filters are what, but the parameters are not! So here we go.
For a blur filter it is as follows new BlurFilter(BlurX, BlurY, Quality). Now the BlurX and BlurY are fairly straight forward, it sets how much you want the filter to blur the graphic on the respective axis. The next is the quality, and I usually set this to one for high quality. You can set it 1 - 15, and the X and Y can go from 0 - 255.
Later we will apply these filters to out bitmap data.
Step 4:
Now we are getting to the more exciting bits. We are going to create and add the bitmap and bitmap data objects.
So to do this we use:
var bmd:BitmapData = new BitmapData(550, 500, true, 0x000000); var bm:Bitmap = new Bitmap(bmd); bm.x = 0; bm.y = 0; addChild(bm);
Right now, this is most probably a new bit for you. So first a bit on bitmap and bitmap data objects.
- Bitmap Data - This is basically something that holds all you data that you are going to insert into your bitmap object. So this includes the width, height, whether it is transparent or not, if so what color should be displayed in the transparent area. This will also hold all the data we want to draw, as you will see late.
- Bitmap - This is the actual bitmap that all the data from the bitmap data will be inserted into and be drawn out. Think of this as the container and the bitmap data being the cargo.
So in the first line we create the bitmap data object and assign all the necessary data for now. This is what I explained earlier.
Next we create the bitmap will is where all the data from the bitmap data will be drawn out into.
After this will align the bitmap to the top left corner of the stage as the top left of the stage is always at X = 0 and Y = 0.
Finally we actually add it to the stage so we can visually see it.
Step 5:
Now we come to an exciting part, creating and adding all the instances of the graphic we created earlier to the stage.
Add this code straight after the last one.
for (var i:uint = 0; i <= amountOfCircles; i++) { var circle:Circle = new Circle(); circle.x = ranX + Math.random() * 100; circle.y = ranY + Math.random() * 100; circle.alpha = ranAlpha; circle.filters = [blur, glowBM]; addChild(circle); }
So what we are doing here is creating a simple loop that will keep running until it has looped over the same about of times as we set in the amoutOfCircles.
Here we also set the current circle to have random X and Y positions, and also random alpha value.
Now, this is where we are starting to use the filters we created earlier. An movie clip or graphic has an array that the filters can be assigned to, so what we are doing is assigning an array of filters to the filters property of circle. Lastly we add it to the stage.
Step 6:
Next we need to add an event listener that will call a function every time a frame is executed. This is an ENTER_FRAME event. Also, we call the function that will randomly move the circles we created earlier to a new random position with a random alpha and colour.
mover(); addEventListener(Event.ENTER_FRAME, enterFrame);
Step 7:
Now this is a main part of our special effect, the mover() function. This is where part of the magic happens.
function mover():void { for (var i:uint = 0; i <= amountOfCircles; i++) { var object:DisplayObject = getChildAt(i); if (object == bm) { i++; } else { object.alpha = ranAlpha; TweenLite.to(object, 1, {x: ranX + Math.random() * 1000, y: ranX + Math.random() * 1000, alpha: 0, ease:Expo.easeInOut, onComplete:mover, blurFilter:{blurX:Math.random()*100, blurY:Math.random() * 100}, tint:Math.round( Math.random()*0xFFFFFF )}); glowBM.color = Math.round( Math.random()*0xFFFFFF ); glowBM.alpha = ranAlpha / 2; } } }
So, here goes.
First of we create a loop that will iterate through all the circles. Then within this we create a variable called object and then we find a circle by using the getChildAt() which finds an object in the display list at the depth of i . We then assign the result of this to object . This gives us a handle on the current circle we are working with.
As the getChildAt() is designed to return anything that is on the stage so we need to check if it has returned the bitmap. We don’t want that, so what we do is skip to the next object in the display list. Once this has happened or if we didn’t have a handle to the bitmap in the first place we move on the code animates the circle.
First of we assign a new alpha value, then we tween it. Now this is beyond the scope of this tutorial, showing you how to use TweenLite, but here is a good tutorial. Just scroll down to the usage section. It is fairly self explanatory anyway.
Next we assign a new colour to the glow filter and also a new alpha value.
Step 8:
Now we are on the last bit of the tutorial. This defines the ENTER_FRAME function.
function enterFrame(evt:Event):void { bmd.draw(stage); bmd.applyFilter(bmd, bmd.rect, new Point(0,0), blurBM); bmd.applyFilter(bmd, bmd.rect, new Point(0,0), glowBM); bmd.scroll(10, 0); }
So, the first line captures what you can see and assigns the data that was captured to the bitmap data, and in turn then to the bitmap.
Next we apply to filters to the bitmap data. These look a little weird to here is an explanation.
The first parameter is which source bitmap data that you want to apply the filter to. The next is what area you want to apply the filter to, and we want to apply it to the whole bitmap data, then the destination point (99.9% of the time you won’t need to change this), and then finally which filter you want to apply to it.
Finally the last one moves everything in the bitmap 10px to the right.
Final Code:
import gs.TweenLite; import gs.easing.Expo; var amountOfCircles:Number = 30; var stageW:Number = stage.stageWidth; var stageH:Number = stage.stageHeight; var ranX:Number = Math.random() * stageW - 300; var ranY:Number = Math.random() * stageH - 300; var ranAlpha:Number = Math.random() * .5; var blur:BlurFilter = new BlurFilter(10, 10, 1); var blurBM:BlurFilter = new BlurFilter(20, 0, 1); var glowBM:GlowFilter = new GlowFilter(Math.round( Math.random()*0xFFFFFF )); var bmd:BitmapData = new BitmapData(550, 500, true, 0x000000); var bm:Bitmap = new Bitmap(bmd); bm.x = 0; bm.y = 0; addChild(bm); for (var i:uint = 0; i <= amountOfCircles; i++) { var circle:Circle = new Circle(); circle.x = ranX + Math.random() * 100; circle.y = ranY + Math.random() * 100; circle.alpha = ranAlpha; circle.filters = [blur, glowBM]; addChild(circle); } mover(); addEventListener(Event.ENTER_FRAME, enterFrame); function mover():void { for (var i:uint = 0; i <= amountOfCircles; i++) { var object:DisplayObject = getChildAt(i); if (object == bm) { i++; } else { object.alpha = ranAlpha; TweenLite.to(object, 1, {x: ranX + Math.random() * 1000, y: ranX + Math.random() * 1000, alpha: 0, ease:Expo.easeInOut, onComplete:mover, blurFilter:{blurX:Math.random()*100, blurY:Math.random() * 100}, tint:Math.round( Math.random()*0xFFFFFF )}); glowBM.color = Math.round( Math.random()*0xFFFFFF ); glowBM.alpha = ranAlpha / 2; } } } function enterFrame(evt:Event):void { bmd.draw(stage); bmd.applyFilter(bmd, bmd.rect, new Point(0,0), blurBM); bmd.applyFilter(bmd, bmd.rect, new Point(0,0), glowBM); bmd.scroll(10, 0); }
So there you go, phew! I hope I explained it enough and you now understand how to create these sort of effects in Flash CS3.
discuss this topic to forum
