Will iterate a command array to run each command on an canvas 2d context.
fn(canvas node, commands, commandmap)
commandmap = 'beginPath0closePath0moveTo0lineTo0quadraticCurveTo0bezierCurveTo0arcTo0rect0arc0fill0fillRect0stroke0clip0fillText0strokeText0measureText0drawImage0createImageData'.split(0);
Commands can be issued either as long or short commands. Long commands are strings to be evaluated within the Canvas 2d context's scope, while short commands are arrays with the command number and the other arguments in order.
Short Command numbers:
- 0 = beginPath
- 1 = closePath
- 2 = moveTo
- 3 = lineTo
- 4 = quadraticCurveTo
- 5 = bezierCurveTo
- 6 = arcTo
- 7 = rect
- 8 = arc
- 9 = fill
- 10 = fillRect
- 11 = stroke
- 12 = clip
- 13 = fillText
- 14 = strokeText
- 15 = measureText
- 16 = drawImage
- 17 = createImageData
This was done so we could render the logo of the 140byt.es homepage in canvas with a script that's smaller than 140bytes :)
Nevertheless, since we wanted to use as few external data as possible, we reduced this to a tool that only executed strings in the Canvas 2d-context and rendered bezierCurves:
function(a,b,c,d){a=a.getContext('2d');for(c in b)d=b[c],eval('with(a){'+(''+d!==d?((d[2]?'bezierCurveTo(':'moveTo(')+d+')'):d)+'}')}
Cool, but no red triangle. Firefox 24 says:
"TypeError: a is null"