Skip to content

Instantly share code, notes, and snippets.

@HarryStevens
Last active Nov 27, 2018
Embed
What would you like to do?
Mask
license: gpl-3.0
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
}
#hole rect {
fill: white;
}
#hole circle {
fill: black;
}
.foreground {
fill: tomato;
}
.background {
fill: steelblue;
}
</style>
</head>
<body>
<svg>
<mask id="hole">
<rect></rect>
<circle></circle>
</mask>
<rect class="foreground"></rect>
<rect class="background" mask="url(#hole)"></rect>
</svg>
<script src="https://d3js.org/d3-selection.v1.min.js"></script>
<script>
function draw(){
var width = window.innerWidth;
var height = window.innerHeight;
d3.select("svg").attr("width", width).attr("height", height);
d3.select("#hole rect").attr("width", width).attr("height", height);
d3.select("#hole circle").attr("cx", width / 2).attr("cy", height / 2).attr("r", Math.min(width, height) / 2);
d3.select(".foreground").attr("width", width).attr("height", height);
d3.select(".background").attr("width", width).attr("height", height);
}
window.onload = draw;
window.onresize = draw;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment