Rotating Conic Radar & Hologram Scanners: Canvas HUD Sweep & Target Tracking Guide
Military and sci-fi radar sweeps in HTML5 Canvas are constructed using rotating polar coordinate beams and conic gradient shaders. Targets intercepted by the sweep beam trigger high-intensity luminescence that decays exponentially through phosphor persistence simulation, complemented by concentric azimuth range rings and interactive cursor locking at 60 FPS.
Step 1: Polar Coordinate Rotating Sweep Beam Math
A radar sweep rotates continuously around a central pivot point (cx, cy). We calculate the sweep angle θ = (time · speed) mod 2π and draw a trailing conic arc fading from peak phosphor intensity down to transparent.
function drawRadarSweep(ctx, cx, cy, radius, sweepAngle) {
ctx.save();
ctx.translate(cx, cy);
// Conic angular fan gradient trailing behind sweep
const grad = ctx.createConicGradient(sweepAngle, 0, 0);
grad.addColorStop(0, 'rgba(0, 255, 136, 0.45)');
grad.addColorStop(0.08, 'rgba(0, 255, 136, 0.15)');
grad.addColorStop(0.2, 'rgba(0, 255, 136, 0)');
grad.addColorStop(1, 'rgba(0, 255, 136, 0)');
ctx.beginPath();
ctx.arc(0, 0, radius, 0, Math.PI * 2);
ctx.fillStyle = grad;
ctx.fill();
// Crisp leading scanner beam line
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(Math.cos(sweepAngle) * radius, Math.sin(sweepAngle) * radius);
ctx.strokeStyle = '#00ff88';
ctx.lineWidth = 2;
ctx.shadowColor = '#00ff88';
ctx.shadowBlur = 10;
ctx.stroke();
ctx.restore();
}
Step 2: Target Interception and Exponential Phosphor Decay
Target blips possess polar coordinates (r, θ). When the sweep beam passes over a target's angular position, the target is energized to maximum luminosity (alpha = 1.0). The blip then decays over time following an exponential decay curve: alpha = alpha · e^(-λt).
class RadarBlip {
constructor(distance, angle, label) {
this.r = distance;
this.theta = angle;
this.label = label;
this.alpha = 0;
this.lastHit = 0;
}
ping() {
this.alpha = 1.0;
}
update(decayRate = 0.985) {
this.alpha *= decayRate;
if (this.alpha < 0.02) this.alpha = 0;
}
render(ctx, cx, cy) {
if (this.alpha <= 0) return;
const x = cx + Math.cos(this.theta) * this.r;
const y = cy + Math.sin(this.theta) * this.r;
ctx.save();
ctx.beginPath();
ctx.arc(x, y, 4, 0, Math.PI * 2);
ctx.fillStyle = `rgba(0, 255, 136, ${this.alpha})`;
ctx.shadowColor = '#00ff88';
ctx.shadowBlur = 12 * this.alpha;
ctx.fill();
// Target callout label
ctx.font = '10px monospace';
ctx.fillStyle = `rgba(0, 255, 136, ${this.alpha * 0.8})`;
ctx.fillText(`[${this.label}]`, x + 8, y + 3);
ctx.restore();
}
}
Step 3: Azimuth Range Rings and HUD Crosshair Telemetry
To provide scale, we render concentric distance range rings with labeled kilometer markers, 30-degree azimuth bearing lines, and dynamic cursor lock reticles.
function drawRadarHUD(ctx, cx, cy, radius, rings = 4) {
ctx.save();
ctx.strokeStyle = 'rgba(0, 255, 136, 0.2)';
ctx.lineWidth = 1;
// Concentric range rings
for (let i = 1; i <= rings; i++) {
const r = (radius / rings) * i;
ctx.beginPath();
ctx.arc(cx, cy, r, 0, Math.PI * 2);
ctx.stroke();
// Range distance badge
ctx.font = '9px monospace';
ctx.fillStyle = 'rgba(0, 255, 136, 0.4)';
ctx.fillText(`${(i * 25)}NM`, cx + r - 22, cy - 4);
}
// Crosshair axes
ctx.beginPath();
ctx.moveTo(cx - radius, cy); ctx.lineTo(cx + radius, cy);
ctx.moveTo(cx, cy - radius); ctx.lineTo(cx, cy + radius);
ctx.stroke();
ctx.restore();
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Here's my golden rule when implementing this in production: test across multiple devices and browsers early. Keep your DOM hierarchy shallow, optimize assets with modern formats (like WebP/AVIF), and verify keyboard navigation for accessibility.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
How does `createConicGradient` work in Canvas 2D contexts?
The `createConicGradient(startAngle, x, y)` method creates a gradient that wraps colors angularly around a center point (like a clock face). By setting the startAngle to the current radar beam angle and defining color stops that fade rapidly, you create a sweeping radar cone without manual wedge polygons.
How do you detect when the radar sweep hits a target?
By comparing the target's polar angle with the radar sweep's current angular delta. If `sweepAngle - previousAngle` encompasses the target's angle modulo 2π, an interception event fires, setting the target's alpha opacity to 1.0.
What is phosphor persistence in CRT radar screens?
Traditional cathode-ray radar screens used zinc sulfide phosphors that emitted light when struck by electrons, remaining illuminated for several seconds before gradually decaying. Replicating this in software requires multiplying target blip alpha by an exponential decay factor (e.g. `alpha *= 0.98`) each frame.
How can targets be made to move autonomously across the radar scope?
Give each target polar velocity vectors (radial speed `dr` and angular speed `dθ`) or standard Cartesian velocities (vx, vy). On each frame update, modify the target coordinates and clamp them within the outer radar range boundary circle.
Can interactive targets be acquired by clicking or hovering on the radar?
Yes. Convert cursor coordinates (clientX, clientY) into radar center offsets (dx = mouseX - cx, dy = mouseY - cy), and compute distance `Math.hypot(dx, dy)`. If the distance is within 10px of any target blip, lock onto the target and render an illuminated targeting box with telemetry readout.