Skip to content

Instantly share code, notes, and snippets.

@acbart
Last active May 23, 2016 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acbart/dcda677555e97b59c1c91554270dc80b to your computer and use it in GitHub Desktop.
Save acbart/dcda677555e97b59c1c91554270dc80b to your computer and use it in GitHub Desktop.
Blockly SVG -> Image
function renderSimple(workspace) {
aleph = workspace.svgBlockCanvas_.cloneNode(true);
aleph.removeAttribute("width");
aleph.removeAttribute("height");
if (aleph.children[0] !== undefined) {
aleph.removeAttribute("transform");
aleph.children[0].removeAttribute("transform");
aleph.children[0].children[0].removeAttribute("transform");
var linkElm = document.createElementNS("http://www.w3.org/1999/xhtml", "style");
linkElm.textContent = Blockly.Css.CONTENT.join('') + '\n\n';
aleph.insertBefore(linkElm, aleph.firstChild);
//$(aleph).find('rect').remove();
var bbox = document.getElementsByClassName("blocklyBlockCanvas")[0].getBBox();
var xml = new XMLSerializer().serializeToString(aleph);
xml = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="'+bbox.width+'" height="'+bbox.height+'" viewBox="0 0 '+bbox.width+' '+bbox.height+'"><rect width="100%" height="100%" fill="white"></rect>'+xml+'</svg>';
var data = "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(xml)));
var img = document.createElement("img");
console.log(xml);
img.setAttribute('src', data);
document.body.appendChild(img);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment