roughjs-es5
Version:
Create graphics using HTML Canvas or SVG with a hand-drawn, sketchy, appearance.
57 lines (53 loc) • 1.69 kB
HTML
<html>
<head>
<title>RoughJS Map example with D3.js - SVG</title>
<script src="https://rawgit.com/pshihn/workly/master/dist/workly.min.js"></script>
<script src="https://rawgit.com/pshihn/rough/master/dist/rough.min.js"></script>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<style>
svg {
display: block;
width: 960px;
height: 500px;
}
</style>
</head>
<body>
<h2>RoughJS Map with D3.js using WebWorker (SVG)</h2>
<svg id="svg"></svg>
<script>
const svg = document.getElementById('svg');
const rc = rough.svg(svg, {
async: true,
options: {
simplification: 0.2, roughness: 0.65
}
});
const width = 960, height = 500;
const projection = d3.geo.albersUsa().scale(1070).translate([width / 2, height / 2]);
const path = d3.geo.path().projection(projection);
const randomColor = () => {
let r = `rgb(${Math.round(Math.random() * 255)}, ${Math.round(Math.random() * 255)}, ${Math.round(Math.random() * 255)})`;
return r;
}
const randomAngle = () => {
return (Math.random() > 0.5 ? -1 : 1) * (1 + Math.random() * 88);
}
const randomStyle = () => {
return (Math.random() > 0.5 ? 'solid' : '');
}
d3.json("./us.json", async (error, us) => {
if (error) throw error;
let topo = topojson.feature(us, us.objects.states).features;
for (let feature of topo) {
svg.appendChild(await rc.path(path(feature), {
fill: randomColor(),
fillStyle: randomStyle(),
hachureAngle: randomAngle()
}));
}
});
</script>
</body>
</html>