Skip to content
nguyenphuminhPublic

Repository files navigation

Kippy is a 2D JS game engine written from scratch with zero dependencies, aiming to be simple, lightweight, and fun to use. It currently utilizes the Canvas 2D context for rendering and provides a small set of APIs for game dev. Kippy is in early development, so expect some components and features to change.

Also check Kippy out on itch.io!

Features

  • Game: A simple game structure of game loop, scenes, and entities.
  • Graphics: Sprites, visual effects, and sprite sheet animation.
  • Physics: An in-house physics engine handling bodies, movements, and collisions.
  • Input: Handle keyboard, mouse, touch, controller, pen, etc.
  • Audio: Audio buses, fades, pitch control, and per-instance or global volume.
  • Camera: Pan, rotation, zoom, and screen-to-world conversion.
  • Vector2: A thorough set of APIs for 2D vector calculations.
  • Zero dependencies: Written fully from scratch, no external dependencies.
  • Free and open source: Licensed under Apache 2.0.

Usage

Install through npm:

npm install kippy

then import, for example:

import { Game, Scene, Entity } from "kippy";

or if you are on raw HTML5, you can pull from a cdn:

import { Game, Scene, Entity } from "https://unpkg.com/kippy";

Examples

You can check out examples in the ./examples directory:

To try them out, clone this repo, then install deps:

npm install

and build:

npm run build

and start Vite:

npx vite

Then go to http://localhost:5173/game-name/index.html, for example, http://localhost:5173/neon-flap/index.html.

Tutorial

Here is a vaguely-written tutorial for now:

Initialize game

First, prepare a canvas tag in your html file:

<canvas></canvas>

Then mount your game there:

import { Game } from "kippy";

const canvas = document.querySelector("canvas");
const game = new Game({
    canvas
});

// Start the game loop
game.start();

// You can also swap canvas if you want
// game.setCanvas(someOtherCanvas);

Create a scene

Here is how you can create a scene and load that scene into your game object:

import { Scene } from "kippy";

class Main extends Scene {
    // Runs when this scene gets loaded into a game
    init() {}
    // Runs 60 times per second at a fixed timestep, and dt is always 1/60
    update(dt) {}
    // Runs when another scene replaces this scene in a game
    exit() {}
}

// Create scene and load into game
const main = new Main();
game.setScene(main);

Create an entity

A scene can have multiple entities like players, mobs, obstacles, etc in it. This is how you can create an entity:

import { Entity } from "kippy";

const entity = new Entity({
    // These are all optional, and can also be set later

    // Graphics
    animator, // Entity's animator to be rendered, type Animator
    sprite, // Entity's sprite to be rendered, type Sprite
    // Position
    position, // Entity's position (centered), type Vector2
    rotation, // Entity's rotation in radians, type number
    // Physics
    body, // Entity's physical body, type EntityBody
    collider, // Entity's collider, type Collider
    // Effects
    glow
});

// Add it to a scene
scene.addEntity(entity);
// Remove it from a scene
scene.removeEntity(entity);

// These props contain movement info and you can mutate them to edit its position
entity.position; // Initialized from the "position" param above, Vector2(0, 0) if not specified
// You can mutate these directly:
entity.position.x;
entity.position.y;
entity.rotation; // Initialized from the "rotation" param above, 0 if not specified

Create a sprite

A sprite represents what an entity looks like (the "graphics part"), and you can create a sprite like this:

import { Sprite } from "kippy";

const sprite = new Sprite({
    texture, // Sprite's texture - HTMLImageElement, HTMLCanvasElement, OffscreenCanvas, ImageBitmap
    width, // Sprite's width, type number
    height // Sprite's height, type number
});

// Set sprite for an entity
entity.sprite = sprite;

Animation

First you create a sprite sheet that contains your frames:

import { SpriteSheet } from "kippy";

const spriteSheet = new SpriteSheet({
    texture, // Similar to sprite texture
    frameWidth, // Width of each frame, type number
    frameHeight, // Height of each frame, type number
    width, // Width when render, default is frameWidth
    height // Height when render, default is frameHeight
});

Then you create an animation object which defines the order of frame to play, fps, and whether to loop or not:

import { Animation } from "kippy";

const animation = new Animation({
    spriteSheet, // A SpriteSheet instance
    frames, // Order of frames, type number[]
    fps, // Frames per second, type number
    loop // Loop animation endlessly, type boolean
});

Then you create an animator, attach it to an entity, and then play an animation:

import { Animator } from "kippy";

const animator = new Animator({
    animations, // A Record<string, Animation> map that contains all animations
    default // The animation that will be played first, type string
});

// Attach it to an entity
entity.animator = animator

// Play an animation
entity.animator.play("animationName");
// If you want to force restart the animation, you can pass true as second param:
entity.animator.play("animationName", true);

// You can also call a handler when animation ends
entity.animator.onEnd = function(name) {
    // Do something
}

Add controls

Game controls like mouse presses, key presses, touch, and cursor tracking (in the game canvas, not the web window) can be done by using the input handler from your game instance:

const input = game.input;

Then in a scene's update method, you can use these utilities to check for key presses:

// Keyboard, using physical key names: "KeyA", "Digit1", "Space", "ArrowUp", "ShiftLeft",...
// These follow key position, so WASD stays in the same place on any keyboard layout
input.isKeyDown("KeyW");
input.isKeyPressed("Space");
input.isKeyReleased("ArrowUp");
// Mouse/touch: 0 = left button, touch or pen (default), 1 = middle, 2 = right
input.isPointerDown();
input.isPointerPressed(2);
// Mouse/touch position
input.pointer; // Pointer's position vector
input.pointer.x; // Current X position of mouse/touch
input.pointer.y; // Current Y position of mouse/touch

Kippy supports input handling for controllers as well:

// Buttons
input.isButtonDown(buttonId, padIndex);
input.isButtonPressed(buttonId, padIndex);
input.isButtonReleased(buttonId, padIndex);

// Where buttonId could be one of these values (remember to import BUTTON first):
BUTTON.A; // 0
BUTTON.B; // 1
BUTTON.X; // 2
BUTTON.Y; // 3
BUTTON.LB; // 4
BUTTON.RB; // 5
BUTTON.LT; // 6
BUTTON.RT; // 7
BUTTON.SELECT; // 8
BUTTON.START; // 9
BUTTON.LS; // 10
BUTTON.RS; // 11
BUTTON.UP; // 12
BUTTON.DOWN; // 13
BUTTON.LEFT; // 14
BUTTON.RIGHT; // 15
BUTTON.HOME; // 16

// Furthermore, padIndex is optional, and it defaults to input.activePad, which is the
// controller the player used most recently (0 if no controller has been used yet)

// Joystick, returns a Vector2 with x and y from -1 to 1 (y is positive when pushed down)
input.getStick(stick, padIndex, deadzone);
// where stick can be "left" or "right", and deadzone is 0.15 by default.

Vectors

To work with positions and movements in Kippy, it is best to know about Vector2 first. Positions, velocities, forces, etc are all represented as vectors in Kippy. And here are how you can create a 2D vector and some vector math utilities that come along with it:

import { Vector2 } from "kippy";

const vect = new Vector2(/* x coordinate, number */, /*y coordinate, number */);

// Props
vect.x; // X coordinate
vect.y; // Y coordinate

// Utilities
vect.toString(); // Returns "Vector2(x, y)"
vect.add(otherVect); // Add another vector and return the result vector
vect.sub(otherVect); // Subtract another vector and return the result vector
vect.mul(otherVect); // Multiply with another vector and return the result vector
vect.div(otherVect); // Divide by another vector and return the result vector
vect.neg(); // Negate and return the result vector
vect.scale(scale); // Multiply with scale and return the result vector
vect.magnitude(); // Return the magnitude/length of vector
vect.magnitudeSquared(); // Return the squared magnitude/length of vector
vect.normalize(); // Return the normalized vector by magnitude
vect.dot(otherVect); // Return dot product with another vector
vect.cross(otherVect); // Return cross product with another vector
vect.project(otherVect); // Return projection on another vector
vect.min(otherVect); // Return a new vector with min coordinates
vect.max(otherVect); // Return a new vector with max coordinates
vect.floor(); // Floor rounding
vect.ceil(); // Ceil rounding
vect.round(); // Normal rounding
vect.distance(otherVect); // Return distance to another vector
vect.distanceSquared(otherVect); // Return squared distance to another vector
vect.copy(); // Return a copy (same coordinates, different reference)
vect.lerp(otherVect, scale); // Apply linear interpolation and return
vect.clamp(maxLength); // Clamp vector to have length below maxLength
vect.rotate(angle); // Return rotated vector by provided angle
vect.orthogonal(); // Return orthogonal vector of this vector
vect.angle(); // Return angle of vector.
vect.angleTo(otherVec); // Return angle between this and another vector
vect.reflect(otherVect); // Return reflection/bounce back vector
vect.equals(otherVect); // Check if two vectors are equal

// Useful constants
Vector2.ZERO; // Vector2(0, 0)
Vector2.ONE; // Vector2(1, 1);
Vector2.UP; // Vector2(0, -1);
Vector2.DOWN; // Vector2(0, 1);
Vector2.LEFT; // Vector2(-1, 0);
Vector2.RIGHT; // Vector2(1, 0);

Physics

For movements, currently you can create a RigidBody:

import { RigidBody } from "kippy";

// Create a rigid body
const rigidBody = new RigidBody({
    velocity, // Entity's velocity vector, type Vector2
    rotationVelocity, // Entity's angular/rotation velocity, type number
    mass, // Entity's mass, type number
    inertia, // Entity's inertia, type number
    force, // Entity's force vector, type Vector2
    torque, // Entity's torque/rotational force, type number
    restitution // Entity's restitution for collision bounce back, type number
});

// Attach body to an entity
entity.body = rigidBody;

// And you can mutate these props to update movement every frame
entity.body.velocity; // Set with the matching parameter above, default is Vector2(0, 0)
entity.body.rotationVelocity; // Set with the matching parameter above, default is 0
entity.body.mass; // Set with the matching parameter above, default is 1
entity.body.inertia; // Set with the matching parameter above, default is 1
// Note that forces are reset after every frame
entity.body.force; // Set with the matching parameter above, default is Vector2(0, 0)
entity.body.torque; // Set with the matching parameter above, default is 0
entity.body.restitution; // Set with the matching parameter above, default is 0

For collisions, you can create a CircleCollider:

import { CircleCollider } from "kippy";

const collider = new CircleCollider({
    radius, // Circle collider's radius, type number
    offset, // Offset from entity's position, type Vector2
    isTrigger, // If true, trigger callbacks are called and collision physics like bouncing 
               // will not apply. Otherwise, collision callbacks are called and physics apply
    layer, // A bit mask to determine what collision layer this collider is at
    mask, // A bit mask to check what colliders to collide
});

// Attach collider to an entity
entity.collider = collider;

// You can mutate these props to configure the collider
collider.radius; // Set with the matching parameter above, required
collider.offset; // Set with the matching parameter above, default is Vector2(0, 0)
collider.isTrigger; // Set with the matching parameter above, default is false
collider.layer; // Set with the matching parameter above, default is (1 << 0)
collider.mask; // Set with the matching parameter above, default is 0xFFFFFFFF

or a BoxCollider:

import { BoxCollider } from "kippy";

const collider = new BoxCollider({
    width, // Box collider's width, type number
    height, // Box collider's height, type number
    offset, // Offset from entity's position, type Vector2
    isTrigger, // If true, trigger callbacks are called and collision physics like bouncing 
               // will not apply. Otherwise, collision callbacks are called and physics apply
    layer, // A bit mask to determine what collision layer this collider is at
    mask, // A bit mask to check what colliders to collide
});

// You can mutate these props to configure the box collider
collider.width; // Set with the matching parameter above, required
collider.height; // Set with the matching parameter above, required
collider.offset; // Set with the matching parameter above, default is Vector2(0, 0)
collider.isTrigger; // Set with the matching parameter above, default is false
collider.layer; // Set with the matching parameter above, default is (1 << 0)
collider.mask; // Set with the matching parameter above, default is 0xFFFFFFFF

and you can handle when two objects collide:

entity.onCollisionEnter = (other, info) => {};
entity.onCollisionStay = (other, info) => {};
entity.onCollisionExit = (other, info) => {};
entity.onTriggerEnter = (other) => {};
entity.onTriggerStay = (other) => {};
entity.onTriggerExit = (other) => {};

info has the structure of:

{
    normal, // Vector2
    penetration, // number
    contact, // Vector2
}

Camera

The camera decides what part of your game world gets rendered. Note that unlike most camera implementations of which positions are centered, Kippy's camera position is at the top-left of the camera. For example, camera at (0,0) and entity at (0,0) in Godot would show the entity at the center, while the same setup in Kippy would show the entity at the top-left. This is to be more aligned with how web and canvas positioning works.

The camera is available for use through every scene object:

// Get camera
const camera = scene.camera;

// The camera has these props to manage it:
camera.position; // The position vector of the camera, default is Vector2(0, 0)
camera.rotation; // Rotation of the camera, default is 0
camera.zoom // Camera zoom level, default is 1

// Convert a screen-based vector (mouse position for example) to world-based vector
camera.screenToWorld(input.pointer); // Return new vector

Audio

You can create a sound and play it using the Sound class:

// Create a Sound object
const sound = new Sound({
    src, // String URL to sound file or a preloaded AudioBuffer object
    volume, // Volume to play in, default is 1
    loop, // Choose whether to loop, default is false
    bus, // Audio bus name, default is "sfx"
    maxInstances // Maximum number of playable instances of this sound, default is 8
})

// Load sound
await sound.load();
// You can also load multiple sounds at once
await Sound.loadAll([sound1, sound2, sound3]);

// Play sound, returning a SoundInstance object, or null if audio is not loaded yet
const soundInstance = sound.play({
    volume, // Volume multiplier against the base volume, default is 1
    pitch, // Sound pitch, default is 1
    loop, // Choose whether to loop for this play, default is sound.loop
    offset, // Timestamp offset in the audio file, default is 0
    fadeIn, // Ramps volume up over this many seconds, default is 0
});

// Stop this sound instance
soundInstance.stop();
// Pause this sound instance
soundInstance.pause();
// Resume
soundInstance.resume();
// Fade to silence over a duration then stop
soundInstance.fadeOut(duration);
// Fade to specified volume over a duration
soundInstance.fadeTo(volume, duration);
// Configure sound options
soundInstance.volume = someVolume;
soundInstance.pitch = somePitch;
// Some useful getters
soundInstance.state; // current state, can be "playing", "paused", or "stopped"
soundInstance.position; // current position in audio source
soundInstance.duration; // duration
// On-end event handler, called when finished playing:
soundInstance.onEnd = () => {
    // Do something
}

// Stop all instances of this sound
sound.stopAll();

You can also control sounds globally through game.audio:

// Configure global sound options
game.audio.volume = someVolume;
game.audio.muted = trueOrFalse;
// Pause all sound instances
game.audio.pause();
// Resume
game.audio.resume();
// Stop all sound instances
game.audio.stopAll();

// You can also configure sound options for a set of sound instances by doing it through a bus they share:
game.audio.bus("busName").volume = someVolume;
game.audio.bus("busName").muted = trueOrFalse;

Effects

Kippy comes with some graphical effects to make things look nicer :)

Entity glow

entity.glow = {
    color, // Color code as string
    bloom, // Glow bloom/range of spread
    intensity, // Glow intensity/boldness
}

// You can also assign it during entity initialization in case you forgot
const newEntity = new Entity({
    glow: { /* Your config */ }
});

Sleep system

When a body's velocity is too low for too long, the body will enter sleep state, which means its position will not be affected by the physics engine until a force is applied, a collision happens, or the velocity is above threshold again, this is to prevent jittering and optimize performance.

You can configure it inside RigidBody:

const rigidBody = new RigidBody({
    sleepThreshold, // The low threshold velocity to enter sleep state, type number
    sleepTimeThreshold, // The duration of sustained low velocity to enter sleep state, type number
    isSleeping, // Flag to set sleep state, type boolean
    sleepTimer // Current sleep timer, you probably don't need this
});

// You can mutate these to change sleep configuration:
rigidBody.sleepThreshold; // Set with the param above, default is 0.1
rigidBody.sleepTimeThreshold; // Set with the param above, default is 0.5
rigidBody.isSleeping; // Set with the param above, default is false
rigidBody.sleepTimer; // Set with the param above, default is 0

Copyright and License

Copyright © 2026 Nguyen Phu Minh.

This project is licensed under the Apache 2.0 license.