Doom Damage Flash on Scroll
The video game Doom famously would flash the screen red when you were hit. Chris Johnson not only took that idea, but incorporated a bunch of the UI from Doom into this tounge-in-cheek JavaScript library called Doom Scroller. Get it? Like, doom scrolling, but like, Doom scrolling. It’s funny, trust me.
I extracted bits from Chris’ cool project to focus on the damage animation itself. The red flash is done in HTML and CSS. First, we create a full screen overlay:
#doom-damage {
background-color: red;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
pointer-events: none;
}
Note that it’s not display: none
. It’s much harder to animate that as we have to wait until the animation is completed to apply it. That’s because display
isn’t animatable. It’s doable, just annoying.
To flash it, we’ll apply a class that does it, but only temporarily.
const damage = document.getElementById("doom-damage");
function doomTakeDamage() {
damage.classList.add("do-damage");
setTimeout(function () {
damage.classList.remove("do-damage");
}, 400);
}
When that class activates, we’ll immediately turn the screen red (really giving it that shock appeal) and then fade the red away:
.do-damage {
background-color: red;
animation: 0.4s doom-damage forwards;
}
@keyframes doom-damage {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
The next bit calls the function that does the damage flash. Essentially it tracks the current scroll position, and if it’s past the nextDamagePosition
, it will red flash and reset the next nextDamagePostition
one full screen height length away.
If you want to see all that, I’ve abstracted it into this Pen:
The post Doom Damage Flash on Scroll appeared first on CSS-Tricks.
You can support CSS-Tricks by being an MVP Supporter.