You're absolutely right — the rotor blades are currently drawn a bit too high above the helicopter body, making them appear disconnected. Let's fix that by adjusting the vertical position of the rotor mast and blades so they align properly with the top of the helicopter body.
Here’s the updated JavaScript code with the rotor blades correctly positioned:
✅ Fixes:
- The mast now starts at the top of the helicopter body (
-10) and goes up to -20.
- The rotor blades are centered at the top of the mast (
-20), so they appear attached.
📌 Paste this entire block into your Firefox console and hit Enter:
(function() {
// ========== Setup full‑window canvas ==========
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.style.position = 'fixed';
canvas.style.top = '0';
canvas.style.left = '0';
canvas.style.zIndex = '9999';
document.body.style.margin = '0';
document.body.style.overflow = 'hidden';
document.body.appendChild(canvas);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// ========== 2D‑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(m) { if (this.mag() > m) this.normalize().mult(m); return this; }
copy() { return new Vec(this.x, this.y); }
}
// ========== Helicopter “agent” ==========
class Helicopter {
constructor() {
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._pickTarget();
this.rotor = 0;
}
_pickTarget() {
return new Vec(
Math.random() * canvas.width,
Math.random() * canvas.height
);
}
_steer() {
const desired = this.target.copy().sub(this.pos);
if (desired.mag() < 20) this.target = this._pickTarget();
desired.normalize().mult(this.maxSpeed);
const steer = desired.sub(this.vel).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);
this.rotor += 0.3 + this.vel.mag() * 0.1;
}
draw(ctx) {
ctx.save();
ctx.translate(this.pos.x, this.pos.y);
const a = Math.atan2(this.vel.y, this.vel.x);
ctx.rotate(a);
// Body
ctx.fillStyle = '#333';
ctx.strokeStyle = '#000';
ctx.lineWidth = 1;
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
ctx.fillStyle = '#4af';
ctx.beginPath();
ctx.arc(-15, 0, 8, Math.PI/2, 3*Math.PI/2);
ctx.fill(); ctx.stroke();
// 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();
// Mast (from top of body at y = -10 to y = -20)
ctx.strokeStyle = '#222';
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(0, -10);
ctx.lineTo(0, -20);
ctx.stroke();
// Rotating blades centered at top of mast (y = -20)
ctx.strokeStyle = '#555';
ctx.lineWidth = 3;
ctx.save();
ctx.translate(0, -20); // move to top of mast
ctx.rotate(this.rotor);
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 animation loop ==========
const heli = new Helicopter();
(function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
heli.update();
heli.draw(ctx);
requestAnimationFrame(animate);
})();
})();
✅ What’s fixed:
- The rotor blades now appear directly on top of the helicopter body.
- The mast and blades are visually connected and centered.
✅ How to use:
- Open any webpage in Firefox.
- Press F12 to open DevTools.
- Go to the Console tab.
- Paste the entire code block above and press Enter.
Enjoy your properly spinning helicopter! 🚁