Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save StoneCypher/b002be929be96303f666af7ace90e0a2 to your computer and use it in GitHub Desktop.
Save StoneCypher/b002be929be96303f666af7ace90e0a2 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<script type="text/javascript">
window.onload = function() {
paintSvgToCanvas(document.getElementById('source'), document.getElementById('tgt'));
}
function paintSvgToCanvas(uSvg, uCanvas) {
var ctx = uCanvas.getContext('2d'),
DOMURL = window.URL || window.webkitURL || window;
var img = new Image();
var svg = new Blob([uSvg.outerHTML], {type: 'image/svg+xml;charset=utf-8'});
var url = DOMURL.createObjectURL(svg);
img.onload = function () {
ctx.drawImage(img, 0, 0);
DOMURL.revokeObjectURL(url);
}
img.src = url;
uCanvas.getContext('2d').drawImage(img, 0, 0);
}
</script>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" width="467" height="462" id="source">
<rect x="80" y="60" width="250" height="250" rx="20" style="fill:#ff0000; stroke:#000000;stroke-width:2px;" />
<rect x="140" y="120" width="250" height="250" rx="40" style="fill:#0000ff; stroke:#000000; stroke-width:2px; fill-opacity:0.7;" />
</svg>
<canvas height="462px" width="467px" id="tgt"></canvas>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment