Claude Game Studio

Health bar generator

This health bar generator lets you style a game health bar by shape, segments, colors, border and damage trail, preview it taking damage, and export PNG frames plus a copy-paste canvas and CSS snippet of the same bar.

Health 100 of 100

Colors and effects

Start from

The frames the export holds, at 100, 75, 50, 25 and 0%

Press Hit 25 to see the damage flash and the trail, then change the shape, size or colors.

// Health bar, 260 x 30. Call drawHealthBar(ctx, x, y, hp, maxHp, trailHp, flash) every frame. const BAR = { w: 260, h: 30, border: 3, radius: 8, skew: 0, segments: 0, gap: 3, borderColor: "#1a1c2c", back: "#333c57", high: "#38b764", low: "#b13e53", shift: true, trail: "#ffcd75", trailOn: true, shine: true }; const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v)); function mixHex(a, b, t) { const p = (s, i) => parseInt(s.slice(1 + i * 2, 3 + i * 2), 16), c = (i) => Math.round(p(a, i) + (p(b, i) - p(a, i)) * t).toString(16).padStart(2, "0"); return "#" + c(0) + c(1) + c(2); } function barPath(ctx, x, y, w, h, r, skew) { ctx.beginPath(); if (skew) { ctx.moveTo(x + skew, y); ctx.lineTo(x + w, y); ctx.lineTo(x + w - skew, y + h); ctx.lineTo(x, y + h); ctx.closePath(); return; } r = Math.min(r, w / 2, h / 2); ctx.moveTo(x + r, y); ctx.arcTo(x + w, y, x + w, y + h, r); ctx.arcTo(x + w, y + h, x, y + h, r); ctx.arcTo(x, y + h, x, y, r); ctx.arcTo(x, y, x + w, y, r); ctx.closePath(); } function drawHealthBar(ctx, x, y, hp, maxHp, trailHp = hp, flash = 0) { const b = BAR, iw = b.w - 2 * b.border, ih = b.h - 2 * b.border, ratio = clamp(hp / maxHp, 0, 1), tr = clamp(trailHp / maxHp, 0, 1); ctx.save(); ctx.translate(x, y); barPath(ctx, 0, 0, b.w, b.h, b.radius, b.skew); ctx.fillStyle = b.borderColor; ctx.fill(); barPath(ctx, b.border, b.border, iw, ih, Math.max(0, b.radius - b.border), b.skew ? Math.round(b.skew * ih / b.h) : 0); ctx.clip(); ctx.fillStyle = b.back; ctx.fillRect(b.border, b.border, iw, ih); const n = b.segments, segW = n > 0 ? (iw - b.gap * (n - 1)) / n : iw; function paint(amount, color) { ctx.fillStyle = color; if (n <= 0) { ctx.fillRect(b.border, b.border, Math.round(iw * amount), ih); return; } for (let i = 0; i < n; i++) { const part = clamp(amount * n - i, 0, 1); if (part > 0) ctx.fillRect(Math.round(b.border + i * (segW + b.gap)), b.border, Math.round(segW * part), ih); } } if (b.trailOn && tr > ratio) paint(tr, b.trail); paint(ratio, b.shift ? mixHex(b.low, b.high, clamp((ratio - 0.2) / 0.3, 0, 1)) : b.high); if (b.shine) { const g = ctx.createLinearGradient(0, b.border, 0, b.border + ih * 0.5); g.addColorStop(0, "rgba(255,255,255,0.38)"); g.addColorStop(1, "rgba(255,255,255,0.04)"); ctx.fillStyle = g; ctx.fillRect(b.border, b.border, iw, Math.round(ih * 0.5)); } if (flash > 0) { ctx.fillStyle = "rgba(255,255,255," + (flash * 0.85).toFixed(3) + ")"; ctx.fillRect(b.border, b.border, iw, ih); } ctx.restore(); }

32 lines. Scroll the box to read all of it; the copy button takes all of it.

What each control changes

Shape and segments

Rounded uses a corner radius of a quarter of the height, capped at 8 pixels. Pill uses half the height, Slanted leans the bar by 45% of its height (about 24 degrees) and Square has no corners. Segments split the bar into equal blocks with the gap you choose, and a block fills in part as health falls inside it.

Color that follows health

With the color change on, the bar holds its Full color from full health to half, then slides to the Low color by one fifth of health and stays there. Turn it off for a bar that never changes, such as a boss bar.

Flash and trail

A hit paints white over the bar at 85% strength and fades in a quarter of a second. The trail holds the lost chunk in its own color for about 450 milliseconds, then drains at 60 health points a second. Both are optional and both are in the exported code.

Canvas code and CSS

The canvas code redraws the same pixels as the preview, so use it when the bar must match exactly. The CSS version is the quick route for a bar in a web page UI: it sets the size, border, colors, segments and trail, and your game sets --hp and data-level.

The four starting bars

Line counts are for the snippet the tool shows for each preset. The canvas code is one function and needs no image file. Pair the bar with the pixel art palette tool for colors that match the rest of your art, and with the tilemap editor for a level to play it in.

Other ways to get a health bar

Niobium Face's Health Bar Generator on itch.io is a browser tool where you set width, height, steps, progress and the filled and unfilled colors, round the corners, and export one PNG. This page adds shapes, a damage flash and trail you can watch, a frame strip, and code you can paste into a game.

Unity's Make a Health Bar with UI Toolkit is a 30 minute beginner tutorial that builds a bar inside a Unity project and animates its fill. This page makes the bar for a browser game or as a PNG.

Questions

How do I make a health bar for a game?

Pick a preset, then change the shape, size, colors and segments until it matches your game. Press Hit 25 to check the damage flash and the trail. Download the frames as a PNG sheet for an engine, or copy the canvas code or the CSS to draw the bar in a browser game.

How does the delayed damage trail work?

Two values are tracked: the real health, which drops at once, and a trail value that waits about 450 milliseconds after a hit and then shrinks toward the real health at 60 points a second. The trail is drawn behind the health in its own color, so the player sees the chunk they just lost. The canvas code takes the trail as its fifth argument.

How many segments should a health bar have?

Use segments when health is a count, such as 10 hits or 5 hearts, and a smooth bar when health is a pool of points. A segment fills in part as health falls inside it. The gap between segments is 1 to 8 pixels. The Retro arcade preset uses 10 segments with a 3 pixel gap.

How do I use the canvas code in my game?

Paste it once, then call drawHealthBar(ctx, x, y, hp, maxHp, trailHp, flash) every frame. The first two numbers place the bar, the next two are health and maximum health, trailHp is the value of the delayed trail, and flash runs from 0 to 1 right after a hit and fades. Nothing is loaded and there are no dependencies.

What do the exported PNG frames look like?

One PNG with the bar repeated from full health to empty, stacked top to bottom with a transparent background. At 10 steps that is 11 frames, so the 260 × 30 pixel Soft RPG bar at 2 times is a 520 × 660 pixel image. Cut it into frames by the bar's height and show the frame for the player's health.

Put your assets into a playable game. Try the ten game templates or get the kit.