Created
July 15, 2014 15:24
-
-
Save nonsense/ce40a8278d6e1893e365 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/node | |
var canvg = require("canvg"); | |
var Canvas = require("canvas"); | |
fs = require('fs') | |
var svg_input; | |
var args = process.argv.slice(2); | |
var canvasMethods = [ "arc", "arcTo", "beginPath", "bezierCurveTo", "clearRect", "clip", "closePath", "createImageData", "createLinearGradient", "createPattern", "createRadialGradient", "drawCustomFocusRing", "drawImage", "drawSystemFocusRing", "fill", "fillRect", "fillText", "getImageData", "getLineDash", "isPointInPath", "isPointInStroke", "lineTo", "measureText", "moveTo", "putImageData", "quadraticCurveTo", "rect", "restore", "rotate", "save", "scale", "scrollPathIntoView", "setLineDash", "setTransform", "stroke", "strokeRect", "strokeText", "transform", "translate" ]; | |
var input_file = args[0]; | |
var output_file = args[1]; | |
var fakeCanvas = { | |
_output: "", | |
getContext: function() { | |
return this; | |
}, | |
canvas: { | |
clientWidth: 600, | |
clientHeight: 600 | |
} | |
} | |
var cvd = { | |
logCommand: function(c) { | |
fakeCanvas._output += c + "\n"; | |
}, | |
getOutput: function() { | |
return fakeCanvas._output; | |
} | |
} | |
var convert = function(svgString) { | |
for (var i = 0; i < canvasMethods.length; i++) { | |
(function() { | |
var method = canvasMethods[i]; | |
fakeCanvas[method] = function() { | |
var args = Array.prototype.slice.call(arguments); | |
this._output += "ctx." + method + "(" + args.join(", ") + ");\n"; | |
} | |
})() | |
} | |
canvg(fakeCanvas, svgString, { ignoreMouse: true, ignoreDimensions: true }); | |
return 'var draw = function(ctx) {\n' + cvd.getOutput() + '};\n' | |
} | |
var processSVG = function() | |
{ | |
var retVal = ""; | |
data = fs.readFileSync(input_file).toString(); | |
retVal = convert(data); | |
fs.writeFileSync(output_file, retVal); | |
}; | |
processSVG(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment