AS3 + JavaScript = Resizable SWF in HTML
I recently needed to resize an swf dynamically within an HTML document and could decided to combine JavaScript with AS3. So here is my solution in three simple steps:
- I wrote a simple JavaScript file that allows resizing of elements in an animated fashion.
- I embedded the swf in the HTML file in a <div> container with the style attribute “overflow:hidden;” and an id called “swf_container”.
- To ensure the desired effect I added stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT;
- Now I added a button in the Flash file that caused navigateToURL(new URLRequest(”JavaScript:sizeById(’swf_container’,500,150,5)”), “_self”); and with this calling the JavaScript function and resizing itself.
If you want to try this yourself you can download the JavaScript file sizebyid.js and recreate what I did with it or download the whole sample.zip including the -.Fla, the -.swf, the -.js and the -.html file.
Of course you can try it out first:
Click the arrow in the top left corner to toggle sizes of the sample.
You can also use the following html form that calls the same JavaScript function
Mental rotation training
With this application you can train or simply test your mental rotation skills. One of the few skills that supposedly show a real difference between men and women as well as between students of natural sciences and students of the arts. But the ability to improve, especially a poor skill, has been shown.
So try it out, after 10 exercises you will see your results.
Because the PHP plugin seems not to work with my configuration I am posting the average results as of now (5th November 2008):
easy 4.78 seconds (230 attempts)
medium 6.21 seconds (151 attempts)
hard 6.75 seconds (293 attempts)
Interesting enough the average of the hard level is only slighltly longer than the average on the medium level.
Air volume under water - Flash dive simulation
This application demonstrates the effect of pressure changes when diving:
Visualisation of heuristics to solve the Travelling Salesman Problem (TSP) in Flash
This little Flash application implements four heuristics to solve the Travelling Salesman Problem:
How to use this application:
Type in the number of nodes and press build to set them up randomly (now max. 300 nodes).
Then you can test the different heuristics by clicking on the buttons in the top area. To hide a solution you can click the button again.
To set the nodes up differently, press build again.
NEW functionalities: Now you can edit the nodes and add new ones. Snap to grid and let the application show each step of the process.
Please comment if you are interested in some additional functionalities.
Download the AS3 Class now here. It is licensed under the Creative Commons Attribution 3.0 License. This allows you to use this class in personal and commercial products but you need to leave my name and link in the class, which is actually not seen by anyone after you publish the file. It would be nice to receive an html link, but it is not required.
To use this class do the following:
Unzip the class into the your class root folder (usually you can use the folder your .FLA file resides in). In your FLA import the class, create an object of the TSP class with an adjacency matrix. Call one of the four heuristics to receive a route array. Add the withHistory flag as true to receive the unfinished routes as well.
- import bjornson.Algos.TSP;
- var myTSP:TSP = new TSP(matrix);
- var nextNeighbour:Array = myTSP.returnNextNeighbour();
- var nearestInsertion:Array = myTSP.returnNearestInsertion();
- var farthestInsertion:Array = myTSP.returnFarthestInsertion();
- var cheapestInsertion:Array = myTSP.returnCheapestInsertion();
Now you can use the found path in any way you like. For example simply trace one:
trace(”Route found by cheapest insertion heuristic: “+cheapestInsertion.toString());
An adjacency matrix is simply a two-dimensional array containing the distances from each node to every other node. For example a small matrix containing only three nodes can look like this:
var matrix:Array = new Array( new Array( 0 , 1 , 2 ), new Array( 1, 0 , 3 ), new Array( 2 , 3 , 0 ));
At the moment only symmetric matrices can be used.
A simple Magnifying Class for AS3
It has been a while that I first programmed a magnifier glass in Actionscrtip 2. But since several friends asked over time how to do it I rewrote it in AS 3.
You can download the entire class for free here: magnify.as
There are no license attachments whatsoever. So you can modify it, reuse it, sell it, whatever you like. But of course I would like to receive some feedback. You can also place links in the comments here to your site if you used this class.
The main important part is the following calculation:
_mc_mask.x=mouseX;
_mc_mask.y=mouseY;
_loaderMc.x=(-_loaderMc2.mouseX)+_loaderMc2.mouseX/_scaleRatio;
_loaderMc.y=(-_loaderMc2.mouseY)+_loaderMc2.mouseY/_scaleRatio;
First the image is loaded twice and then it positions the image that is only partially visible in the right spot over the smaller image so that it seems to be the same image.
To include the class in your own project all you need to is:
- unzip bjornson.zip into the same directory your FLA-file is in
- write code similar to this in your actions dialogue of your FLA (this is the very minimum you need):
- import bjornson.Magnify.Magnify;
var m:Magnify = new Magnify(”a.jpg”); //this should be a relative path to a .jpg or .png
addChild(m); - Publish the file, done!
- scr:String - this is the relative path to your image
- w:Number - this is the maximum width of the preview image
- h:Number - this is the maximum height of the preview image (it will be adjusted automatically to fit while keeping the correct aspect ratio)
- s:Boolean - this is to switch from a rectangular magnifier to a round magnifier
- r:Number - this is the radius of a round magnifier or the height of a rectangular magnifier
- rw:Number - this is the width of a rectangular magnifier
- m:Boolean - this defines if the Mouse-Pointer is visible when it moves over the image or not
Hi
This is my new blog. You can still check out my galleries on the HfK Bremen server.