Dev.to WebDev 🛠 Dev 👁 0 📖 8 min read

Recreating Minecraft's XP bar with three SVGs and one @keyframes rule

On the Agoralis player dashboard, the job progress box shows a green XP bar that shimmers exactly like the one above the hotbar in Minecraft. Players recognise it instantly, and it shows their real job XP, pulled from th

On the Agoralis player dashboard, the job progress box shows a green XP bar that shimmers exactly like the one above the hotbar in Minecraft. Players recognise it instantly, and it shows their real job XP, pulled from the game server.

There is no canvas, no JavaScript and no GIF behind it. It is three small SVGs drawn pixel by pixel, one @keyframes rule that swaps two of them, and a Blade loop that decides how many segments to draw. This article shows the production code, why we picked this approach, and the edge cases we know it has.

TL;DR

  • Draw the bar as pixel-art SVGs: one tile for the empty track, two frames for a filled segment. Our second frame is the first one rotated 180°.
  • Tile the empty track with background-repeat: repeat-x and stack filled segments as 20 × 8 px flex children on top of it.
  • Alternate the two frames with a @keyframes rule on the background. We set the duration (0.7 s) by watching the game side by side.
  • Compute the number of filled segments server-side from the player's real values (currentXp / jobXp), so the bar is plain HTML that Livewire re-renders.
  • Guard the edges: zero XP, a zero divisor, rounding and prefers-reduced-motion.

The context

Agoralis is an economic role-play Minecraft server. The game itself is written in Java by the server team; we built everything that runs in the browser: the site, the player web app and the Filament admin, on Laravel and Livewire. The web app talks to the game server over RCON. The full project story (timeline, constraints, team) is here.

One screen shows the player's current job: level, skills, and how much XP is left before the next level. The server team wanted it to look like the game. The XP bar is the element every Minecraft player knows, so we wanted it close to pixel-perfect, including the subtle shimmer of the filled part.

The constraints were plain. We were a team of two on the whole web app, the game server was still being built in parallel, and many players use the dashboard on a phone while the game runs on their main screen. A decorative bar had to cost almost nothing to build, to run and to maintain.

Option What it costs Why we didn't keep it
CSS gradient + width transition Cheap Looks like a generic progress bar, not like Minecraft's pixels
Canvas or JS animation A render loop, resize handling, more code to test Too much machinery for a decorative element
Animated GIF/APNG of the full bar One file Cannot reflect the player's real value without one image per percentage
Pixel-drawn SVG frames + @keyframes Three small files, about 15 lines of CSS Kept: same look as the game, dynamic width, no JS

The design came first: our designer reproduced the bar pixel by pixel in the mockup, already knowing it would be built as frames swapped by CSS rather than as a real animation.

Drawing the frames at pixel level

The bar is made of three SVG files, each one a grid of 1-pixel rectangles exported from the mockup.

File Size Role
xpEmpty.svg 26 × 8 px Empty track tile, repeated horizontally
xpFull.svg 20 × 6 px Filled segment, frame A
xpFull2.svg 20 × 6 px Filled segment, frame B

Each pixel is one <path> with its own fill. The highlights (#8CF139) sit in the middle rows and the shadows (#3F7F07) at the edges, which is what gives the bar its relief. The last 2 px column is a dark separator, like the notches in the game's bar.

<!-- xpFull.svg (excerpt): 18 × 6 coloured pixels + a 2 px dark separator -->
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="6" viewBox="0 0 20 6" fill="none">
    <path d="M18 6H17V5H18V6Z" fill="#418805" />
    <path d="M18 5H17V4H18V5Z" fill="#3F7F07" />
    <path d="M18 4H17V3H18V4Z" fill="#5AA719" />
    <!-- ... 105 more pixels ... -->
    <path d="M18 0H20V6H18V0Z" fill="#2B6100" />
</svg>

The shimmer is a cheap trick: xpFull2.svg uses exactly the same colours as xpFull.svg, rotated 180° inside the 18 × 6 area, with the separator kept on the right. When the two frames alternate, highlights and shadows swap places, and the eye reads it as light moving across the bar. No third colour palette to design, no extra frame to maintain.

One detail that matters: SVG at integer coordinates, displayed at its native size, keeps hard pixel edges. We never scale these files; the bar is always 8 px tall in CSS.

Swapping frames with @keyframes

The track and the segments are two layers. The track is the container's own background, tiled with repeat-x, so it fills any width. The filled segments are flex children laid out left to right on top of it.

/* resources/css/app.css */
.progressBar {
    width: 100%;
    height: 8px;
    overflow: hidden;
    border-radius: 1px;
    display: flex;
    flex-direction: row;
    background-repeat: repeat-x;
    background-image: url('../img/xpEmpty.svg');
}

/* One filled segment = one 20 px frame */
.xpFull {
    width: 20px;
    height: 8px;
    animation: xpAnimation .7s infinite;
}

@keyframes xpAnimation {
    0% {
        background: url('../img/xpFull.svg');
    }
    100% {
        background: url('../img/xpFull2.svg');
    }
}

That is the whole animation. We tuned the 0.7 s duration by eye, with the game open next to the browser, until the two shimmers matched.

The detail that matters is how browsers animate between two url() values. They are not numbers, so there is no tween in the usual sense: the browser blends the two images. In our case players see a light cross-fade from frame A to frame B. Combined with the 180° rotation, it reads as a soft shimmer, close to the game's. If you want a strict pixel swap instead, make the timing explicit:

.xpFull {
    animation: xpAnimation .7s steps(1) infinite;
}

@keyframes xpAnimation {
    0%, 49.99% { background-image: url('../img/xpFull.svg'); }
    50%, 100%  { background-image: url('../img/xpFull2.svg'); }
}

We kept the cross-fade in production because it looks close to the game. This variant also uses background-image instead of the background shorthand.

Feeding the player's real values

The numbers come from the game. The JobManagement Livewire component sends a console command to the server over RCON and parses the text it gets back. The server plugin answers with a list-like string, so three regular expressions pick out the level, the current XP and the XP needed for the next level.

// app/Livewire/Dashboard/JobManagement.php
private function refreshUserJobLevels()
{
    $server = new Server(); // our RCON wrapper

    $response = $server->sendCommand('webapi jobs stats ' . auth()->user()->mojang_username);

    // XP needed for the next level: the decimal right before "]]"
    preg_match('/(\d+\.\d+)(?=\]\])/m', $response, $xpMatches);

    // Current job level: the first integer between commas
    preg_match('/,\s*(\d+),/', $response, $lvlMatches);

    // Current XP: the first decimal in the response
    preg_match('/\b(\d+\.\d+)\b/', $response, $currentMatches);

    $jobLvl = ! empty($lvlMatches) ? intval($lvlMatches[1]) : 1;
    $jobXp = ! empty($xpMatches) ? floatval($xpMatches[0]) : null;
    $currentXp = ! empty($currentMatches) ? $currentMatches[0] : null;

    $this->jobLvl = $jobLvl;
    $this->jobXp = $jobXp;
    $this->currentXp = $currentXp;
}

The bar itself is plain Blade. We turn the ratio into a number of 20 px segments, one per started 10 %, and render that many .xpFull divs. No inline width, no JS: Livewire re-renders the HTML when the values change, and the CSS animation just runs.

{{-- resources/views/livewire/dashboard/job-management.blade.php --}}
@if ($jobLvl !== null && $jobXp !== null && $currentXp !== null)
    {{-- "avant le prochain niveau" = "before the next level": the UI is in French --}}
    <p class="mb-[6px] text-white text-[14px]">
        <span class="text-base font-medium">{{ $jobXp - $currentXp }}XP</span> avant le prochain niveau
    </p>

    <div class="progressBar overflow-hidden rounded-[1px]">
        @if ($currentXp > 0)
            @for ($i = 0; $i < (ceil($currentXp / $jobXp * 100)) / 10; $i++)
                <div class="xpFull"></div>
            @endfor
        @endif
    </div>

    <div class="flex flex-row justify-between mt-1 w-full">
        <span class="text-white opacity-80">Niveau {{ $jobLvl }}</span>
        <span class="text-white opacity-80">Niveau {{ $jobLvl + 1 }}</span>
    </div>
@endif

The loop condition does the rounding: at 34 % the bound is 3.4, so the loop runs for $i = 0 to 3 and draws 4 segments. Any XP above zero shows at least one segment, which feels right to a player who has just started a level.

All segments start their animation in the same render, so they shimmer in sync, like a single bar.

Where it breaks at the edges

The bar has not caused an incident since the server opened in August 2025. Re-reading the code for this article, though, we found a handful of edges worth knowing before you copy it.

Edge case What happens What we would change
$jobXp parsed as 0.0 $currentXp / $jobXp throws DivisionByZeroError on PHP 8 and the component fails Treat $jobXp <= 0 like null and hide the bar
Player at 91 % Rounds up to 10 segments: the bar looks full before the level-up Use floor(), keeping a minimum of 1 segment when XP > 0
RCON output format The regexes depend on the plugin's text output; "first decimal = current XP" silently breaks if the order changes Ask the server team for a JSON response, or test the parser against captured outputs
Users who prefer reduced motion The shimmer never stops The media query below

The last one is a two-line fix: freeze the bar on frame A.

@media (prefers-reduced-motion: reduce) {
    .xpFull {
        animation: none;
        background-image: url('../img/xpFull.svg');
    }
}

None of these changes is in production yet. We list them because a reader copying the code would hit them before we do.

Wrapping up

  • A game-like UI element rarely needs a game-like engine. Two frames and a @keyframes rule were enough to make players recognise the bar.
  • Design with the build cost in mind: rotating one frame 180° gave us the shimmer without drawing a second palette.
  • Keep the logic server-side. The bar is a count of divs computed in Blade, so Livewire re-renders it like any other markup.
  • Decorative code still has edges: a zero divisor, rounding and reduced motion deserve a line each.
📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.