In my previous entry, I asked about the possiblity of using Flash as a vector graphics engine, for scripting by client-side JavaScript code. With help from Geoff Stearns and his blog Deconcept I've figured out how to do it.
I've figured out how to do it in Flash 8, at least. Flash 8 has a new ExternalInterface API that transparently makes ActionScript methods callable from JavaScript. (It is possible, though not nearly so simple to do similar things in earlier versions of Flash.) Here is my trivial ActionScript file, which I named Canvas.as:
import flash.external.ExternalInterface;
class Canvas {
static function main() {
Stage.scaleMode = "noScale";
Stage.align = "TL";
ExternalInterface.addCallback("beginFill", _root, _root.beginFill);
ExternalInterface.addCallback("beginGradientFill", _root,
_root.beginGradientFill);
ExternalInterface.addCallback("clear", _root, _root.clear);
ExternalInterface.addCallback("curveTo", _root, _root.curveTo);
ExternalInterface.addCallback("endFill", _root, _root.endFill);
ExternalInterface.addCallback("lineTo", _root, _root.lineTo);
ExternalInterface.addCallback("lineStyle", _root, _root.lineStyle);
ExternalInterface.addCallback("moveTo", _root, _root.moveTo);
}
}
I compiled this code with the open-source mtasc ActionScript compiler like this:
mtasc -swf Canvas.swf -main -version 8 Canvas.as
This produced the file Canvas.swf which you can download by clicking the link.
The ActionScript code doesn't do anything except make the Flash drawing API accesible to JavaScript. If you embed this SWF file in your web page, it is simply blank, until you draw something to it yourself. Here is a demo:
<embed src="Canvas.swf" quality=high bgcolor=#FFFFFF width="400" height="200"
name="canvas" align="" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
<p><button onclick="draw()">Draw in the Canvas</button>
<script>
function draw() {
document.canvas.beginFill(0xAAAAFF, 100);
document.canvas.lineStyle(5, 0x0000FF, 100);
document.canvas.moveTo (10, 10);
document.canvas.lineTo (200, 100);
document.canvas.lineTo (10, 190);
document.canvas.lineTo (10, 10);
document.canvas.endFill();
}
</script>
This demo uses the <embed> tag instead of the <object> tag, so it won't run in IE. But I have tested it in Firefox, and it works. But only if you have Flash 8 installed. And Flash 8 is not available for Linux yet. If you've got Flash 8 running in Firefox (or Safari?) give the demo a try!
Comments, suggestions, improvements, or whatever are very welcome. I plan to tweak Canvas.as a bit to expose methods for adding text, but I think that will be straightforward. I was disappointed to discover that the Flash drawing API is not nearly as robust as that of the <canvas> tag or of SVG of VML. In particular all curves must be done with quadratic beziers. Cubic beziers, circles, arcs and so on must all be approximated with a single curveTo() function. So it might be worth adding some basic shape-drawing primitives (circles, rounded rectangles, etc) to Canvas.as as well.
Feel free to use this code (and the swf file) for any purpose, with attribution.




Yeah, it works in Safari.
For the IE users, here's a page that will work for you:
http://blog.deconcept.com/2005/10/13/drawing-javascript-flash/
Not sure if the following is of any use to you - it details how to embed a SWF in a page without using Macromedia's messy code, in a way that will work for IE, Mozilla, Safari, etc., and is nice and standards-compliant to boot.
http://www.alistapart.com/articles/flashsatay/
Thanks Stephen,
I'd heard about the "satay" method, but didn't know what it was... Geoff's comment above also provides a scripted cross-browser solution to the problem. If I put an example like this in the next edition of my book, I'll use a simple scripted solution to put an or tag in.
Stephen,
Thinking it through some more, I realize that my Canvas.swf file is a tiny one and doesn't stream, so I could use the tag described in the flash satay article without the satay sauce. That is, I don't need to complexity of the container movie, and (if the article's findings are still valid) can portably embed my flash canvas with a single tag...
Thanks!
http://blog.deconcept.com/flashobject/
I've started using this since yesterday and it kind of takes care of embedding flash objects into web-pages. Superb stuff...
you might have a look at this link for using flash to do this scripting realtime... www.aflax.org
-Mark