TExcelTAD
Home › 2D Games with JavaScript & Canvas › Platformer 3: Coyote Time & Jump Buffering

Platformer 3: Coyote Time & Jump Buffering

Play the step 2 platformer for a minute and you'll eventually press jump and nothing happens. The game was right by its own rules: you had already left the ledge by one frame, or you pressed jump a few frames before landing. But the player is sure they pressed jump in time, and they're angry. Two small timers fix this, and almost every well-loved platformer uses them.

Trick The situation The fix
Coyote time You press jump just after running off a ledge Allow a jump for a short time (about 0.1 s) after leaving the ground
Jump buffering You press jump just before landing Remember the press for a short time (about 0.1 s) and jump the moment you land

The name comes from cartoon coyotes, who run off cliffs and only fall when they notice.

Two timers instead of two booleans

Before, the rule was "jump if jumpPressed() and onGround", two facts about this exact frame. Now each fact gets a little memory:

js
// coyote: how long we may still jump since last touching the ground
if (player.onGround) coyote = COYOTE_TIME; else coyote -= dt;

// buffer: how long ago jump was pressed, while it still counts
if (jumpPressed()) buffer = BUFFER_TIME; else buffer -= dt;

if (buffer > 0 && coyote > 0) {
  player.vy = -JUMP_SPEED;
  buffer = 0;        // consume the press: one press, one jump
  coyote = 0;        // and no second jump in mid-air
}

Both resets matter. Without coyote = 0, the player could jump twice within the coyote window. Without buffer = 0, one press could trigger a second jump on the next landing.

The timeline, frame by frame

Here are both situations simulated at 60 updates per second, with the assists off and on. The function returns the frame on which the jump happens.

js · try it yourself
const DT = 1 / 60;

function simulate({ coyoteTime, bufferTime }, onGroundAt, pressFrame) {
  let coyote = 0, buffer = 0;
  for (let f = 0; f < 60; f++) {
    const onGround = onGroundAt(f);
    if (onGround) coyote = coyoteTime; else coyote -= DT;
    if (f === pressFrame) buffer = bufferTime; else buffer -= DT;
    // with no assists the timers are 0, which means "only this frame counts"
    const canJump = coyoteTime ? coyote > 0 : onGround;
    const wantsJump = bufferTime ? buffer > 0 : f === pressFrame;
    if (canJump && wantsJump) return `jumps on frame ${f}`;
  }
  return 'no jump';
}

const OFF = { coyoteTime: 0, bufferTime: 0 };
const ON = { coyoteTime: 0.1, bufferTime: 0.12 };

// Runs off a ledge on frame 10, presses jump 3 frames later.
const ledge = (f) => f < 10;
console.log('late press  (off):', simulate(OFF, ledge, 13));
console.log('late press  (on): ', simulate(ON, ledge, 13));
console.log('way too late (on):', simulate(ON, ledge, 20));

// Falling, lands on frame 24, pressed jump 4 frames early.
const landing = (f) => f >= 24;
console.log('early press (off):', simulate(OFF, landing, 20));
console.log('early press (on): ', simulate(ON, landing, 20));
late press  (off): no jump
late press  (on):  jumps on frame 13
way too late (on): no jump
early press (off): no jump
early press (on):  jumps on frame 24

Pressing 3 frames late (50 ms) or 4 frames early (67 ms) is a jump with the assists and a miss without them. Pressing 10 frames late still fails, so the assists forgive human timing without turning into a free double jump.

Tip: Keep both windows short: 0.08–0.15 s. Longer coyote time starts to look like the character runs on air. And reset coyote to 0 whenever the player jumps, or the window allows a second jump.

Step 3: a forgiving jump

The same level as step 2, now with both assists. Press C to switch them off and on and feel the difference: run off ledges and press jump a moment too late, or mash jump just before landing. A label pops up whenever an assist saved a jump that the strict rules would have refused.

html · try it yourself
<style>
  html, body { margin: 0; height: 100%; background: #0e1018; overflow: hidden; }
  canvas { display: block; margin: 0 auto; width: min(100vw, 177.78vh); height: auto; outline: none; touch-action: none; }
</style>
<canvas width="480" height="270"></canvas>
<script>
// ---- play kit: click to focus, keep game keys inside the frame, fixed-step loop ----
const cv = document.querySelector('canvas'), ctx = cv.getContext('2d');
const W = cv.width, H = cv.height;
const keys = new Set(), pressed = new Set();   // keys held now / pressed since the last update
let active = false, idleDrawn = false;
cv.tabIndex = 0;
cv.addEventListener('pointerdown', () => cv.focus());
cv.addEventListener('focus', () => { active = true; });
cv.addEventListener('blur', () => { active = false; idleDrawn = false; keys.clear(); pressed.clear(); });
addEventListener('keydown', (e) => {
  if (e.code.startsWith('Arrow') || e.code === 'Space') e.preventDefault(); // don't scroll the lesson
  if (!e.repeat) pressed.add(e.code);
  keys.add(e.code);
});
addEventListener('keyup', (e) => keys.delete(e.code));
function banner(title, sub = '') {
  ctx.save();
  ctx.fillStyle = 'rgba(0,0,0,0.6)'; ctx.fillRect(0, 0, W, H);
  ctx.fillStyle = '#fff'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
  ctx.font = 'bold 22px system-ui, sans-serif'; ctx.fillText(title, W / 2, H / 2 - 10);
  ctx.font = '14px system-ui, sans-serif'; ctx.fillText(sub, W / 2, H / 2 + 18);
  ctx.restore();
}
function run(update, draw, help = '') {
  const STEP = 1 / 60;
  let acc = 0, last = performance.now();
  requestAnimationFrame(function frame(now) {
    const dt = Math.min((now - last) / 1000, 0.1);
    last = now;
    if (active) {
      acc += dt;
      while (acc >= STEP) { update(STEP); pressed.clear(); acc -= STEP; }
      draw();
    } else if (!idleDrawn) {
      draw(); banner('Click to play', help); idleDrawn = true;
    }
    requestAnimationFrame(frame);
  });
}
// ---- end of play kit ----

const T = 16;
const MAP = [
  '##############################',
  '#............................#',
  '#............................#',
  '#............................#',
  '#.......####.................#',
  '#............................#',
  '#............................#',
  '#..............#####.........#',
  '#............................#',
  '#............................#',
  '#....#####...........####....#',
  '#............................#',
  '#............................#',
  '#..........####..........##..#',
  '#..........................###',
  '#.....##.............#.....###',
  '##############################',
];
const GRAVITY = 1500, JUMP_SPEED = 440, CUT = 0.45, MAX_FALL = 600;
const RUN_SPEED = 150, ACCEL = 1400, FRICTION = 1800, EPS = 0.001;
const COYOTE_TIME = 0.1, BUFFER_TIME = 0.12;
const player = { x: 40, y: 200, w: 12, h: 16, vx: 0, vy: 0, onGround: false, facing: 1 };
let jumpWasHeld = false, assists = true, coyote = 0, buffer = 0;
let note = '', noteTime = 0;

const approach = (v, t, s) => (v < t ? Math.min(v + s, t) : Math.max(v - s, t));
const jumpHeld = () => keys.has('Space') || keys.has('KeyW') || keys.has('ArrowUp');
const jumpPressed = () => pressed.has('Space') || pressed.has('KeyW') || pressed.has('ArrowUp');

function solidAt(tx, ty) {
  if (ty < 0 || ty >= MAP.length || tx < 0 || tx >= MAP[0].length) return true;
  return MAP[ty][tx] === '#';
}
function moveX(b, dx) {
  b.x += dx;
  const top = Math.floor(b.y / T), bottom = Math.floor((b.y + b.h - EPS) / T);
  if (dx > 0) {
    const tx = Math.floor((b.x + b.w - EPS) / T);
    for (let ty = top; ty <= bottom; ty++) if (solidAt(tx, ty)) { b.x = tx * T - b.w; return 'right'; }
  } else if (dx < 0) {
    const tx = Math.floor(b.x / T);
    for (let ty = top; ty <= bottom; ty++) if (solidAt(tx, ty)) { b.x = (tx + 1) * T; return 'left'; }
  }
  return null;
}
function moveY(b, dy) {
  b.y += dy;
  const left = Math.floor(b.x / T), right = Math.floor((b.x + b.w - EPS) / T);
  if (dy > 0) {
    const ty = Math.floor((b.y + b.h - EPS) / T);
    for (let tx = left; tx <= right; tx++) if (solidAt(tx, ty)) { b.y = ty * T - b.h; return 'floor'; }
  } else if (dy < 0) {
    const ty = Math.floor(b.y / T);
    for (let tx = left; tx <= right; tx++) if (solidAt(tx, ty)) { b.y = (ty + 1) * T; return 'ceiling'; }
  }
  return null;
}

function update(dt) {
  if (pressed.has('KeyC')) assists = !assists;
  noteTime -= dt;

  const dir = (keys.has('ArrowRight') || keys.has('KeyD') ? 1 : 0) - (keys.has('ArrowLeft') || keys.has('KeyA') ? 1 : 0);
  if (dir !== 0) player.facing = dir;
  const rate = dir !== 0 ? (player.onGround ? ACCEL : ACCEL * 0.6) : (player.onGround ? FRICTION : 300);
  player.vx = approach(player.vx, dir * RUN_SPEED, rate * dt);

  // the two timers
  if (player.onGround) coyote = assists ? COYOTE_TIME : 0; else coyote -= dt;
  const pressedNow = jumpPressed();
  if (pressedNow) buffer = assists ? BUFFER_TIME : 0; else buffer -= dt;
  const canJump = player.onGround || coyote > 0;
  const wantsJump = pressedNow || buffer > 0;
  if (canJump && wantsJump) {
    if (!player.onGround) { note = 'coyote jump!'; noteTime = 0.8; }
    else if (!pressedNow) { note = 'buffered jump!'; noteTime = 0.8; }
    player.vy = -JUMP_SPEED;
    buffer = 0; coyote = 0;
  }

  const held = jumpHeld();
  if (jumpWasHeld && !held && player.vy < 0) player.vy *= CUT;
  jumpWasHeld = held;
  player.vy = Math.min(player.vy + GRAVITY * dt, MAX_FALL);

  if (moveX(player, player.vx * dt)) player.vx = 0;
  const hit = moveY(player, player.vy * dt);
  player.onGround = hit === 'floor';
  if (hit) player.vy = 0;
}

function drawTiles() {
  for (let ty = 0; ty < MAP.length; ty++) {
    for (let tx = 0; tx < MAP[ty].length; tx++) {
      if (MAP[ty][tx] !== '#') continue;
      const x = tx * T, y = ty * T;
      ctx.fillStyle = '#5b3a29'; ctx.fillRect(x, y, T, T);
      ctx.fillStyle = '#6d4a36'; ctx.fillRect(x + 2, y + 2, T - 4, T - 4);
      if (ty > 0 && MAP[ty - 1][tx] !== '#') { ctx.fillStyle = '#4caf50'; ctx.fillRect(x, y, T, 3); }
    }
  }
}

function drawPlayer() {
  const x = Math.round(player.x), y = Math.round(player.y);
  ctx.fillStyle = '#ffb74d'; ctx.fillRect(x, y, player.w, player.h);
  ctx.fillStyle = '#5d4037'; ctx.fillRect(x, y, player.w, 4);
  ctx.fillStyle = '#fff';
  const ex = player.facing > 0 ? x + 6 : x + 1;
  ctx.fillRect(ex, y + 6, 2, 3); ctx.fillRect(ex + 4, y + 6, 2, 3);
}

function draw() {
  ctx.fillStyle = '#16213e'; ctx.fillRect(0, 0, W, H);
  drawTiles();
  drawPlayer();
  ctx.font = '11px ui-monospace, monospace'; ctx.textAlign = 'left';
  ctx.fillStyle = assists ? '#81c784' : '#e57373';
  ctx.fillText(`assists ${assists ? 'ON ' : 'OFF'} (C)`, 20, 30);
  // timer bars: how much of each window is left
  ctx.fillStyle = '#333'; ctx.fillRect(140, 23, 60, 4); ctx.fillRect(140, 30, 60, 4);
  ctx.fillStyle = '#4fc3f7'; ctx.fillRect(140, 23, Math.round(60 * Math.max(0, coyote) / COYOTE_TIME), 4);
  ctx.fillStyle = '#ffd54f'; ctx.fillRect(140, 30, Math.round(60 * Math.max(0, buffer) / BUFFER_TIME), 4);
  ctx.fillStyle = '#9aa4c0'; ctx.fillText('coyote', 206, 28); ctx.fillText('buffer', 206, 35);
  if (noteTime > 0) {
    ctx.fillStyle = '#fff'; ctx.textAlign = 'center'; ctx.font = 'bold 12px system-ui, sans-serif';
    ctx.fillText(note, Math.round(player.x + player.w / 2), Math.round(player.y) - 8);
  }
}

run(update, draw, 'Arrows / A D to run, Space / W / Up to jump, C toggles assists');
</script>

The only change from step 2 is the block marked "the two timers". With assists off, both windows are zero, so the rules fall back to "on the ground this frame, pressed this frame". The two bars in the corner show the windows in action: the blue coyote bar drains after you leave a ledge, the yellow buffer bar drains after an early press.

More "invisible help" in platformers

Players describe a game as "tight" or "responsive" partly because of tricks like these:

Trick What it does
Variable jump height Release early for a short hop (step 1)
Apex hang Lower gravity near the top of the jump, while vy is close to 0, for more air control
Fast fall Stronger gravity while falling, so jumps don't feel floaty
Corner correction If the head clips a ceiling corner by 1–3 pixels, nudge the player sideways instead of stopping the jump
Ledge forgiveness If the feet miss a ledge by a couple of pixels while rising, pop the player up onto it

Note: None of these are "cheating". The player's intent was clear, and the game honours it. Good game feel means matching what the player meant, not what the frame counter saw.

Test yourself

What is coyote time?

  1. A delay before the game starts
  2. A short window after leaving the ground during which a jump is still allowed
  3. Extra time added when the player dies
  4. A slow-motion effect

The player presses jump 4 frames before landing. What makes the jump happen on landing?

  1. Coyote time
  2. Jump buffering: the press is remembered for a short time
  3. Variable jump height
  4. A higher frame rate

Why set coyote = 0 and buffer = 0 right after jumping?

  1. To save memory
  2. So one press gives exactly one jump and no second jump is possible in the air
  3. To make the player fall faster
  4. It is required by requestAnimationFrame