To achieve this effect, one can utilize SVG in conjunction with a small snippet of JavaScript code.
Utilizing a linear-gradient that essentially acts as a sharp step allows for filling the text dynamically.
Here is a functional example:
const userInput = document.querySelector("input");
const gradientStopsArray = Array.from(
document.querySelectorAll("#fillGradient stop")
);
userInput.addEventListener("input", event => {
const sliderValue = event.target.valueAsNumber;
gradientStopsArray.forEach(stopItem => {
stopItem.offset.baseVal = sliderValue;
});
});
body { margin: 0; background: red; font-family: sans-serif; }
svg { width: 100%; height: auto; color: white; }
svg text {
font-size: 60px;
font-weight: bold;
stroke: currentColor;
stroke-width: 1px;
fill: url(#fillGradient);
}
input { position: fixed; bottom: 1em; display: block; width: 90%; margin: 0 5%; }
<svg viewBox="0 0 400 200">
<defs>
<linearGradient id="fillGradient">
<stop offset="50%" stop-color="currentColor" />
<stop offset="50%" stop-color="transparent" />
</linearGradient>
</defs>
<text x="0" y="60">10D:20:42:12</text>
</svg>
<input type="range" min=0 max=1 step=0.001 />
If a canvas-based approach is preferred, a simple method could involve:
- Rendering the text with a white stroke and fill
- Using
clearRect
to erase a portion of the text
- Re-rendering the text with a transparent fill