jsGFwk is a lightweight HTML5/Canvas framework that gives you just the right amount of structure for building 2D games without hijacking your main loop. Every capability—rendering, collisions, input, sprites, audio, scenes, storage—is implemented as an optional plug-in, so you decide exactly how much of the framework to use.
This repository hosts the third generation of the framework. It keeps backward compatibility with older projects where possible, but it modernizes the source by using ES modules and a webpack-based bundle (dist/jsgfwk-bundle.js) that you can drop into any project, including Electron or plain browser deployments.
Tip: The quickest way to understand the API is to open
examples/index.html, which wires up every plug-in and demonstrates sprites, audio, storage, containers, and scenes in a single page.
-
Reference the bundle and canvas in your page
<script src="./scripts/jsgfwk-bundle.js"></script> <canvas id="canvas" width="640" height="480"></canvas>
-
Instantiate the engine and include the plug-ins you need
const game = new jsGFwk.Engine(); const animator = new jsGFwk.Animator2D(); const resourceManager = new jsGFwk.ResourcesManager(); const sprites = new jsGFwk.Sprites(); const keyboardIO = new jsGFwk.KeyboardIO(); game.include(animator); game.include(resourceManager); game.include(sprites); game.include(keyboardIO);
-
Create your game objects, load assets, then start the loop
resourceManager.addGraphic({ name: "atlas", source: "./graphics/jumpingBase.png" }); resourceManager.onResourcesLoadedCompleted = () => { sprites.createCollection("fallingGuy", resourceManager.GRAPHICS.atlas.image, [{ left: 0, top: 0, width: 64, height: 32 }]); game.createObject(new jsGFwk.VisualGameObject("player", 50, 50, 64, 32)); }; game.start();
Enginekeeps the registry of game objects, injects each included plug-in (game.include(...)), and owns lifecycle hooks (onStart,onStop,onObjectCreated).Animator2Dis the double-buffered renderer. It clears the buffer, runs every object’supdate(delta)anddraw(context)methods, and then blits the buffer to the visible canvas every frame viarequestAnimationFrame.
GameObject/VisualGameObjectare the base classes. Visual objects track position, size, visibility, and collision helpers injected by theCollisionsplug-in.ScenesManagergroups collections of objects into named scenes. Activating a scene creates all its objects at once; deactivating destroys them, making state transitions easy to manage.Containerslet you clone lightweight blueprint objects at runtime (great for particles or bullets) and destroy them individually.
ResourcesManagerqueues graphics (addGraphic) and audio (addSound) files, tracks load progress, and exposes the loaded assets throughGRAPHICSandSOUNDS. RegisteronResourcesLoadedCompletedto know when you can safely build sprites or play audio.Spritesslices sprite sheets into reusable collections and supports inverted frames and optional filters.ImageManipulationandImageFiltersprovide helpers for merging regions of images, applying convolution filters (blur, sharpen, emboss, etc.), or generating grayscale/inverted variants on the fly.
KeyboardIO,MouseIO, andTouchIOattach DOM listeners when the engine starts and remove them on stop, exposing simple registration APIs (registerKeypress,registerClick,registerTouch, etc.).WebStoragewraps the browser’slocalStorageto save structured data (setData/getJson), handy for remembering high scores or configuration.
Jukeboxwraps HTML5Audio, allowing you to create multiple playback channels from a single source, control volume per channel, and trigger overlapping effects.Clockis a lightweight timer that ticks inupdate(delta)and fires an action when its trigger time elapses—ideal for scripted events.
Fonts.buildFont(name, path)injects@font-facerules at runtime so you can render custom TTF/OTF fonts on your canvas.Camerais a placeholder plug-in ready for view transforms or scrolling logic if your project needs it.
const game = new jsGFwk.Engine();
const animator = new jsGFwk.Animator2D();
const collisions = new jsGFwk.Collisions();
const scenesManager = new jsGFwk.ScenesManager();
game.include(animator);
game.include(collisions);
game.include(scenesManager);
const player = new jsGFwk.VisualGameObject("Player", 100, 100, 32, 32, 1, true);
player.update = (delta) => { /* movement logic */ };
player.draw = (ctx) => {
ctx.fillStyle = "#00FFAA";
ctx.fillRect(player.x, player.y, player.width, player.height);
};
scenesManager.create("main", [player]);
scenesManager.SCENES.main.activate();
game.start();- Use
game.createObjectfor ad-hoc objects or rely onScenesManagerfor deterministic activation/deactivation. - Call
sprites.SPRITES_BAG.yourSprite.moveNextSprite()inupdateto animate frame collections. - Register mouse/keyboard callbacks through their respective IO plug-ins to keep your objects focused on gameplay logic.
examples/index.htmlshowcases the full engine: animated sprites, clones, clocks, fonts, sounds, scenes, and storage. Use it as a starting point for your own prototypes.games/andv1_games/contain additional experiments and legacy demos that you can port or learn from.v1_deprecated/preserves older plug-ins in case you need to reference the previous architecture.
If you build something cool with jsGFwk, drop a note or contribute improvements—new plug-ins are easy to add, and the community examples help everyone ship faster.
