Here’s a self‑contained JavaScript snippet that, once included on any page, will:
• Create a full‑window canvas
• Spawn a little “helicopter” (drawn with canvas primitives) right in the center
• Make it wander around “realistically” using a simple steering behaviour
• Spin its rotor continuously
Just drop this into a <script> tag (or your console) — no external libs or images needed:
(function() {
// ========== Setup canvas ==========
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
document.body.style.margin = 0;
document.body.style.overflow = 'hidden';
document.body.appendChild(canvas);
window.addEventListener('resize', resizeCanvas);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resizeCanvas();
// ========== Vector helper ==========
class Vec {
constructor(x=0, y=0) { this.x = x; this.y = y; }
add(v) { this.x += v.x; this.y += v.y; return this; }
sub(v) { this.x -= v.x; this.y -= v.y; return this; }
mult(s) { this.x *= s; this.y *= s; return this; }
div(s) { this.x /= s; this.y /= s; return this; }
mag() { return Math.hypot(this.x, this.y); }
normalize() { let m = this.mag()||1; return this.div(m); }
limit(max) { if(this.mag() > max) this.normalize().mult(max); return this; }
copy() { return new Vec(this.x, this.y); }
}
// ========== Helicopter Class ==========
class Helicopter {
constructor() {
// start in center
this.pos = new Vec(canvas.width/2, canvas.height/2);
this.vel = new Vec();
this.acc = new Vec();
this.maxSpeed = 2.0;
this.maxForce = 0.05;
this.target = this.randomTarget();
this.rotorAngle = 0;
}
randomTarget() {
return new Vec(
Math.random()*canvas.width,
Math.random()*canvas.height
);
}
// steering towards target
steer() {
let desired = this.target.copy().sub(this.pos);
let d = desired.mag();
if (d < 20) {
this.target = this.randomTarget();
}
desired.normalize();
desired.mult(this.maxSpeed);
let steer = desired.sub(this.vel);
steer.limit(this.maxForce);
this.acc.add(steer);
}
update() {
this.steer();
this.vel.add(this.acc).limit(this.maxSpeed);
this.pos.add(this.vel);
this.acc.mult(0);
// rotor spins faster when moving
this.rotorAngle += 0.3 + this.vel.mag()*0.1;
}
draw(ctx) {
ctx.save();
// translate & tilt helicopter in flight direction
ctx.translate(this.pos.x, this.pos.y);
let angle = Math.atan2(this.vel.y, this.vel.x);
ctx.rotate(angle);
// Draw body
ctx.fillStyle = '#333';
ctx.strokeStyle = '#000';
ctx.lineWidth = 1;
// main cabin
ctx.beginPath();
ctx.rect(-20, -10, 40, 20);
ctx.fill(); ctx.stroke();
// tail boom
ctx.beginPath();
ctx.rect(20, -4, 30, 8);
ctx.fill(); ctx.stroke();
// cockpit glass (blue semi-circle)
ctx.fillStyle = '#4af';
ctx.beginPath();
ctx.arc(-15, 0, 8, Math.PI/2, Math.PI*3/2);
ctx.fill(); ctx.stroke();
// wheels/skids
ctx.strokeStyle = '#111';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(-15, 10);
ctx.lineTo(-5, 14);
ctx.lineTo(5, 14);
ctx.lineTo(15, 10);
ctx.stroke();
// rotor mast
ctx.strokeStyle = '#222';
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(0, -10);
ctx.lineTo(0, -20);
ctx.stroke();
// rotating blades
ctx.strokeStyle = '#555';
ctx.lineWidth = 3;
ctx.save();
ctx.translate(0, -20);
ctx.rotate(this.rotorAngle);
ctx.beginPath();
ctx.moveTo(-30, 0);
ctx.lineTo(30, 0);
ctx.moveTo(0, -30);
ctx.lineTo(0, 30);
ctx.stroke();
ctx.restore();
ctx.restore();
}
}
// ========== Main Loop ==========
const heli = new Helicopter();
function animate() {
ctx.clearRect(0,0,canvas.width,canvas.height);
heli.update();
heli.draw(ctx);
requestAnimationFrame(animate);
}
animate();
})();
Explanation of the key parts:
- We dynamically make a full‑screen
<canvas> and adjust on resize.
- A tiny
Vec class gives us 2D‑vector math (add, sub, normalize, limit, etc.).
- The
Helicopter “agent” uses a basic “steering toward a target” behavior—once it’s near, it picks a new random point.
- We draw the fuselage, tail, cockpit, skids, mast and two crossing rotor blades—all with simple canvas paths.
- We spin the blades by incrementing
rotorAngle each frame, and tilt the heli body in the direction of motion.
Feel free to tweak speeds, sizes or colors to suit your taste! 🚁