Last active
May 20, 2016 11:39
-
-
Save qur2/d70becace1003ddd9a2911750309ce85 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
var fs = require('fs'); | |
var path = require('path'); | |
var jsdom = require('jsdom'); | |
function template (js) { | |
return ( | |
`<!DOCTYPE html> | |
<html> | |
<head> | |
<script>${fs.readFileSync(require.resolve('raphael'))}</script> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<div id="surface"></div> | |
<script> | |
// @see https://www.smashingmagazine.com/2014/05/love-generating-svg-javascript-move-to-server/ | |
window.Raphael.prototype.renderfix = function () {}; | |
${js} | |
</script> | |
</body> | |
</html>` | |
); | |
} | |
module.exports = function(source) { | |
// this.cacheable && this.cacheable(); | |
var callback = this.async(); | |
var toRender = template(source) | |
// fs.writeFileSync('./test.html', toRender) | |
jsdom.env({ | |
html: toRender, | |
features: { QuerySelector: true }, | |
done: function (err, window) { | |
if (err) { | |
throw err | |
callback(err) | |
} | |
var svg = window.document.body.children[0].innerHTML; | |
// fs.writeFileSync('./uest.html', svg) | |
callback(null, svg) | |
}, | |
}) | |
}; |
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
// Here is the raphael code to be executed by the loader to create the SVG | |
var w = 320, h = 330; | |
var paper = new Raphael("surface", w, h) | |
paper.canvas.setAttribute('height', '100%') | |
paper.canvas.setAttribute('width', '100%') | |
paper.setViewBox(0, 0, w+5, h+5) | |
paper.canvas.setAttribute('preserveAspectRatio', 'none'); | |
paper.drawnRect(10, 10, w-10, h-10, 6) |
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
// An example module loader for webpack | |
{ | |
test: /\.raph\.js$/, | |
loaders: ['file?name=static/[name].svg', path.join(__dirname, './raphael-loader')], | |
exclude: /node_modules/ | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment