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.