Created
July 15, 2014 15:25
-
-
Save nonsense/688f3ff65e294d89eb2e 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 input_file = args[0]; | |
var output_file = args[1]; | |
var doit = function() { | |
fs.readFile(input_file, 'utf8', function (err,data) { | |
if (err) { | |
return console.log(err); | |
} | |
svg_input = data; | |
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" ]; | |
function convert() { | |
//var svgString = $.trim($('#svg-input').val()); | |
var svgString = svg_input; | |
//var canvas = $("#output")[0]; | |
var fakeCanvas = { | |
_output: "", | |
getContext: function() { | |
return this; | |
}, | |
canvas: { | |
clientWidth: 600, | |
clientHeight: 600 | |
} | |
} | |
cvd = { | |
logCommand: function(c) { | |
fakeCanvas._output += c + "\n"; | |
}, | |
getOutput: function() { | |
return fakeCanvas._output; | |
} | |
} | |
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 }); | |
var result = 'var draw = function(ctx) {\n' + cvd.getOutput() + '};\n' | |
return result; | |
} | |
var retVal = convert(); | |
fs.writeFile(output_file, retVal, function(err) { | |
if(err) { | |
console.log(err); | |
} else { | |
console.log("The file was saved!"); | |
} | |
}); | |
}); | |
console.log ("finished"); | |
}; | |
doit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment