Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nazeeruddinikram/497fd6fcddad0347c8afdba3039ae4f6 to your computer and use it in GitHub Desktop.
Save nazeeruddinikram/497fd6fcddad0347c8afdba3039ae4f6 to your computer and use it in GitHub Desktop.
simple as fuck svg creator
<canvas id="x" width=100 height=100 style="border: 1px solid black;"></canvas>
<p>hold ctrl to start a new path, click, click, click, hold shift to create an svg (check console output)</p>
<script>
var C = document.getElementById('x'), w = C.width, h = C.height, c = C.getContext("2d"), points = [];
c.strokeStyle = 'rgb(0,0,0)';
c.lineWidth = 1;
C.onclick = function(e){
if(e.shiftKey){
var s = '<?xml version="1.0" ?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg enable-background="new 0 0 '+w+' '+h+'" width="'+w+'px" height="'+h+'px" id="Icons" version="1.1" viewBox="0 0 '+w+' '+h+'" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g><polygon points="'+points.map(function(p){return p.join(',')}).join(' ')+'"/></g></svg>';
console.log(s);
console.log(btoa(s));
return;
}
if(e.ctrlKey){
c.closePath();
points = [];
c.beginPath();
c.moveTo(e.offsetX, e.offsetY);
}
c.lineTo(e.offsetX, e.offsetY);
points.push([e.offsetX, e.offsetY]);
c.stroke();
c.clearRect(0, 0, w, h);
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment