I'm using Mike Swanson's AI > Canvas plugin for Illustrator (found here) and it's great for exporting an Illustrator creation to an HTML5 Canvas element, but the exported JS that builds the Canvas element builds it in a way that is read-only.
I'm trying to figure out a streamlined way to convert the AI2Canvas exported JS into jCanvas JS so that I'm able to dynamically update elements within the canvas (e.g. Update text, change colors, etc).
The following 2 comparisons are for a basic rectangle:
AI2Canvas exports to something like:
// layer1/Rectangle
ctx.save();
ctx.beginPath();
ctx.moveTo(1000.8, 1000.0);
ctx.lineTo(0.8, 1000.0);
ctx.lineTo(0.8, 0.0);
ctx.lineTo(1000.8, 0.0);
ctx.lineTo(1000.8, 1000.0);
ctx.closePath();
ctx.fillStyle = "rgb(237, 28, 36)";
ctx.fill();
jCanvas would be something like:
$('canvas').drawLine({
name: 'firstLine', // this allows me to access later for edits
closed: true,
fillStyle: "rgb(237, 28, 36)",
x1: 0, y1: 1000,
x2: 0, y2: 0,
x3: 1000, y3: 0,
x4: 1000, y4: 1000
});
In this instance, a basic rectangle, converting myself is not difficult. Where it gets trickier is when AI2Canvas exports out to something that includes BezierCurves which is not as easy to manually convert over to jCanvas properties.
I'm also open to something besides AI2Canvas & jCanvas -- I just need to to figure out the best way to get from something designed in Illustrator into a <canvas>
element that I can then access specific elements later with JS.
Alright, after digging more into the jCanvas code & docs I figured out how to dump the raw output from AI2Canvas into jCanvas.
jCanvas
.draw()
has a function property that lets you pass in regular canvas drawing code and it will create a layer based off this. There's some caveats though:layer: true
is set so that the shape is created as a layer that jCanvas can accessThen, we just pass a unique
name
to the Object so we can reference it later and make any adjustments we want.Here's an example of building a single shape layer with jCanvas using the output from AI2Canvas:
And then updating some property like the fill color: