Created
March 5, 2021 09:42
-
-
Save captainbrosset/a4ad2ca8a493fd142a632a1d835faa1c to your computer and use it in GitHub Desktop.
Code snippets for the "How we built the DevTools Tooltips" article
This file contains hidden or 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
// This is our mask element, a 100%/100% semi-transparent | |
// element sitting on the whole UI. | |
const mask = ...; | |
// Start defining a path used to clip the mask. | |
// First, go around the outer perimeter. | |
const polygon = ['0 0', '100% 0', '100% 100%', '0 100%', '0 0']; | |
// And then clip away the highlighted areas from the mask. | |
// To do this, we go around their shapes, in reverse direction. | |
const toClip = allAreas.map(area => area.points); | |
for (const points of allAreas) { | |
polygon.push(`${points[0].x}px ${points[0].y}px`); | |
for (const point of points.reverse()) { | |
polygon.push(`${point.x}px ${point.y}px`); | |
} | |
// Remember to go back to 0/0 between each area. | |
polygon.push('0 0'); | |
} | |
// Apply the clip. | |
mask.setAttribute('style', `clip-path:polygon(${polygon.join(',')});`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment