Skip to content

Instantly share code, notes, and snippets.

@CarstenHoyer
Created June 30, 2020 22:00
Show Gist options
  • Select an option

  • Save CarstenHoyer/465d88d305644a8c2a179bebf266aa9c to your computer and use it in GitHub Desktop.

Select an option

Save CarstenHoyer/465d88d305644a8c2a179bebf266aa9c to your computer and use it in GitHub Desktop.
const draw = (rc, svg) => {
hatches = [
"hachure",
"zigzag",
"cross-hatch",
"dots",
"sunburst",
"dashed",
"zigzag-line",
];
const figuesPerLine = 8;
const diameter = 100;
const margin = 10;
[...Array(40).keys()].map((i) => {
const index = i % hatches.length;
const x = i % figuesPerLine;
const y = Math.floor(i / figuesPerLine);
let node = rc.rectangle(
margin + x * (diameter * 1.2),
margin + y * (diameter * 1.2),
diameter,
diameter,
{
fill: "black",
fillWeight: 3,
hachureAngle: Math.random() * 360,
hachureGap: 6 + y * 2,
strokeWidth: 3,
bowing: 1 + y * 0.2,
roughness: 1 + y * 1.2,
fillStyle: hatches[index],
}
);
svg.appendChild(node);
});
};
const touchEvents = (event, svg) => {
// Save on S.
if (event.keyCode === 83) {
encodeSvgAndLink(svg);
}
};
const init = () => {
const svg = document.querySelector("#canvas");
const rc = rough.svg(svg);
draw(rc, svg);
document.addEventListener("keyup", (event) => {
touchEvents(event, svg);
});
};
const encodeSvgAndLink = (svg) => {
var svgBlob = new Blob([svg.outerHTML], {
type: "image/svg+xml;charset=utf-8",
});
var svgUrl = URL.createObjectURL(svgBlob);
var downloadLink = document.createElement("a");
downloadLink.href = svgUrl;
downloadLink.download = "test.svg";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
};
document.addEventListener("DOMContentLoaded", init);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment