Skip to content

Instantly share code, notes, and snippets.

@kyoncy
Last active December 8, 2023 08:55
Show Gist options
  • Save kyoncy/b662159121ad2ee5523ed4c750193707 to your computer and use it in GitHub Desktop.
Save kyoncy/b662159121ad2ee5523ed4c750193707 to your computer and use it in GitHub Desktop.
Download Blockly.Workspace as SVG or PNG file
import Blockly from 'blockly';
declare interface CustomNode extends Node {
removeAttribute(arg0: string);
}
const DOMURL = window.URL || window.webkitURL;
const getSvgBlob = (workspace: Blockly.WorkspaceSvg) => {
const canvas = workspace.svgBlockCanvas_.cloneNode(true) as CustomNode;
canvas.removeAttribute("transform");
const css = `<defs><style type="text/css" xmlns="http://www.w3.org/1999/xhtml"><![CDATA[${Blockly.Css.CONTENT.join('')}]]></style></defs>`;
const bboxElement = document.getElementsByClassName("blocklyBlockCanvas")[0] as SVGGraphicsElement;
const bbox = bboxElement.getBBox();
const content = new XMLSerializer().serializeToString(canvas);
const 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=" ${bbox.x} ${bbox.y} ${bbox.width} ${bbox.height}">${
css}">${content}</svg>
`;
return new Blob([xml], { type: 'image/svg+xml;base64' });
};
const download = (url: string, filename: string) => {
const element = document.createElement('a');
element.href = url;
element.download = filename;
element.click();
DOMURL.revokeObjectURL(element.href);
};
export const downloadBlocklySvg = (workspace: Blockly.WorkspaceSvg) => {
download(DOMURL.createObjectURL(getSvgBlob(workspace)), 'blocks.svg');
};
export const downloadBlocklyPng = (workspace: Blockly.WorkspaceSvg) => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
canvas.getContext("2d").drawImage(img, 0, 0);
download(canvas.toDataURL("image/png"), 'blocks.png');
};
img.src = DOMURL.createObjectURL(getSvgBlob(workspace));
};
@MarcusRiemer
Copy link

MarcusRiemer commented Oct 1, 2020

This almost works for me, but some gradient or path in the resulting SVG is off. These are the blocks on the SVG workspace:

Screenshot_20201001_113717

This is how the SVG is rendered:
Screenshot_20201001_113836

Do you have any idea why that happens? The resulting SVG code reads as follows:

<svg version="1.1"
	xmlns="http://www.w3.org/2000/svg"
	xmlns:xlink="http://www.w3.org/1999/xlink" width="106" height="203" viewBox=" 323.5 84 106 203">
	<defs>
		<style type="text/css"
			xmlns="http://www.w3.org/1999/xhtml">
			<![CDATA[]]>
		</style>
	</defs>">
	<g
		xmlns="http://www.w3.org/2000/svg" class="blocklyBlockCanvas">
		<g class="blocklyMarker">
			<g width="100" height="5">
				<rect width="100" height="5" style="display: none"/>
				<rect class="blocklyVerticalMarker" rx="10" ry="10" style="display: none"/>
				<path transform="" style="display: none"/>
				<path transform="" style="display: none" fill="none" stroke-width="4"/>
			</g>
		</g>
		<g class="blocklyCursor">
			<g width="100" height="5">
				<rect width="100" height="5" style="display: none">
					<animate attributeType="XML" attributeName="fill" dur="1s" values="#cc0a0a;transparent;transparent;" repeatCount="indefinite"/>
				</rect>
				<rect class="blocklyVerticalMarker" rx="10" ry="10" style="display: none"/>
				<path transform="" style="display: none">
					<animate attributeType="XML" attributeName="fill" dur="1s" values="#cc0a0a;transparent;transparent;" repeatCount="indefinite"/>
				</path>
				<path transform="" style="display: none" fill="none" stroke-width="4">
					<animate attributeType="XML" attributeName="stroke" dur="1s" values="#cc0a0a;transparent;transparent;" repeatCount="indefinite"/>
				</path>
			</g>
		</g>
		<g data-id="QJ+V9,QadW}DFgZ%,Kf^" class="blocklyDraggable blocklySelected" style="display: block;" transform="translate(323.5,83.99999999999989)">
			<path class="blocklyPathDark" transform="translate(1,1)" fill="#648449" d=" m 0,0  m 0,8 a 8 8 0 0,1 8,-8  h 7  l 6,4  3,0  6,-4  h 53  v 5  H 83  V 5  H 83  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 3  H 83  V 33  H 83  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 0  H 83  V 48  V 52  h -53.5  l -6,4  -3,0  -6,-4  h -14.5  H 0 z&#xA;"/>
			<path class="blocklyPath" stroke="none" fill="#7da55b" d=" m 0,0  m 0,8 a 8 8 0 0,1 8,-8  h 7  l 6,4  3,0  6,-4  h 53  v 5  H 83  V 5  H 83  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 3  H 83  V 33  H 83  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 0  H 83  V 48  V 52  h -53.5  l -6,4  -3,0  -6,-4  h -14.5  H 0 z&#xA;"/>
			<path class="blocklyPathLight" stroke="#a4c08c" d=" m 0,0  m 0.5,8 a 7.5 7.5 0 0,1 8,-7.5  H 14.5  h 0.5  l 6,4  3,0  6,-4  H 82.5  H 82.5  M 83,5  m -5,14.3  l 3.68,-2.1  M 83,33  m -5,14.3  l 3.68,-2.1  M 0.5,51.5  V 8 &#xA;"/>
			<g transform="translate(10,5)">
				<text class="blocklyText" x="0" y="14">SELECT</text>
			</g>
			<g data-id="o:]%O=VV?TzxwzE@^GGo" class="blocklyDraggable" style="display: block;" transform="translate(0,53)">
				<path class="blocklyPathDark" transform="translate(1,1)" fill="#5d4984" d=" m 0,0  h 15  l 6,4  3,0  6,-4  h 42  v 5  H 72  V 5  H 72  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 3  H 72  V 27  H 50  l -6,4  -3,0  -6,-4  h -7 a 8 8 0 0,0 -8,8  v 8 a 8 8 0 0,0 8,8  H 72  H 72  V 51  V 61  h -42.5  l -6,4  -3,0  -6,-4  h -14.5  H 0 z&#xA;"/>
				<path class="blocklyPath" stroke="none" fill="#745ba5" d=" m 0,0  h 15  l 6,4  3,0  6,-4  h 42  v 5  H 72  V 5  H 72  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 3  H 72  V 27  H 50  l -6,4  -3,0  -6,-4  h -7 a 8 8 0 0,0 -8,8  v 8 a 8 8 0 0,0 8,8  H 72  H 72  V 51  V 61  h -42.5  l -6,4  -3,0  -6,-4  h -14.5  H 0 z&#xA;"/>
				<path class="blocklyPathLight" stroke="#9e8cc0" d=" m 0,0  m 0.5,0.5  H 14.5  h 0.5  l 6,4  3,0  6,-4  H 71.5  H 71.5  M 72,5  m -5,14.3  l 3.68,-2.1  M 20,51  m 1.9895923599143455,-1.9895923599143455 a 8.5 8.5 0 0,0 6.0104076400856545,2.4895923599143455  l 43.5,0  H 71.5  M 0.5,60.5  V 0.5 &#xA;"/>
				<g transform="translate(10,5)">
					<text class="blocklyText" x="0" y="14">FROM</text>
				</g>
				<g data-id="{x(Jhna}^LVM(rK+eZo_" class="blocklyDraggable" style="display: block;" transform="translate(0,62)">
					<path class="blocklyPathDark" transform="translate(1,1)" fill="#844a49" d=" m 0,0  h 15  l 6,4  3,0  6,-4  h 52  v 5  H 82  V 5  H 82  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 3  H 82  V 23  V 27  h -52.5  l -6,4  -3,0  -6,-4  h -14.5  H 0 z&#xA;"/>
					<path class="blocklyPath" stroke="none" fill="#a55c5b" d=" m 0,0  h 15  l 6,4  3,0  6,-4  h 52  v 5  H 82  V 5  H 82  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 3  H 82  V 23  V 27  h -52.5  l -6,4  -3,0  -6,-4  h -14.5  H 0 z&#xA;"/>
					<path class="blocklyPathLight" stroke="#c08d8c" d=" m 0,0  m 0.5,0.5  H 14.5  h 0.5  l 6,4  3,0  6,-4  H 81.5  H 81.5  M 82,5  m -5,14.3  l 3.68,-2.1  M 0.5,26.5  V 0.5 &#xA;"/>
					<g transform="translate(10,5)">
						<text class="blocklyText" x="0" y="14">WHERE</text>
					</g>
					<g data-id=":+XgbiT]CM6e(76zveqO" class="blocklyDraggable" style="display: block;" transform="translate(0,28)">
						<path class="blocklyPathDark" transform="translate(1,1)" fill="#498452" d=" m 0,0  h 15  l 6,4  3,0  6,-4  h 75  v 5  H 105  V 5  H 105  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 3  H 105  V 23  V 27  h -75.5  l -6,4  -3,0  -6,-4  h -14.5  H 0 z&#xA;"/>
						<path class="blocklyPath" stroke="none" fill="#5ba566" d=" m 0,0  h 15  l 6,4  3,0  6,-4  h 75  v 5  H 105  V 5  H 105  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 3  H 105  V 23  V 27  h -75.5  l -6,4  -3,0  -6,-4  h -14.5  H 0 z&#xA;"/>
						<path class="blocklyPathLight" stroke="#8cc094" d=" m 0,0  m 0.5,0.5  H 14.5  h 0.5  l 6,4  3,0  6,-4  H 104.5  H 104.5  M 105,5  m -5,14.3  l 3.68,-2.1  M 0.5,26.5  V 0.5 &#xA;"/>
						<g transform="translate(10,5)">
							<text class="blocklyText" x="0" y="14">GROUP BY</text>
						</g>
						<g data-id="*fi*-16SfEBV[yWJkhWh" class="blocklyDraggable" style="display: block;" transform="translate(0,28)">
							<path class="blocklyPathDark" transform="translate(1,1)" fill="#49844a" d=" m 0,0  h 15  l 6,4  3,0  6,-4  h 73  v 5  H 103  V 5  H 103  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 3  H 103  V 23  V 27  h -73.5  l -6,4  -3,0  -6,-4  h -6.5 a 8 8 0 0,1 -8,-8 z&#xA;"/>
							<path class="blocklyPath" stroke="none" fill="#5ba55c" d=" m 0,0  h 15  l 6,4  3,0  6,-4  h 73  v 5  H 103  V 5  H 103  c 0,10  -8,-8  -8,7.5  s 8,-2.5  8,7.5  v 3  H 103  V 23  V 27  h -73.5  l -6,4  -3,0  -6,-4  h -6.5 a 8 8 0 0,1 -8,-8 z&#xA;"/>
							<path class="blocklyPathLight" stroke="#8cc08d" d=" m 0,0  m 0.5,0.5  H 14.5  h 0.5  l 6,4  3,0  6,-4  H 102.5  H 102.5  M 103,5  m -5,14.3  l 3.68,-2.1  M 0,27  m 2.6966991411008934,-2.6966991411008934 a 7.5 7.5 0 0,1 -2.1966991411008934,-5.303300858899107  V 0.5 &#xA;"/>
							<g transform="translate(10,5)">
								<text class="blocklyText" x="0" y="14">ORDER BY</text>
							</g>
						</g>
					</g>
				</g>
			</g>
		</g>
	</g>
</svg>
  

And is there any chance you could explicitly attach a permissive License to this? My own code is AGPL 3 and I would love to use your snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment