Simple SVG mask.
Last active
November 27, 2018 17:35
-
-
Save HarryStevens/afa509b72ef8c08cdc0a5eddeeeab8f8 to your computer and use it in GitHub Desktop.
Mask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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