This project makes starting an interactive 2D Canvas application fast and easy, even for developers who have never used Canvas before.
Thorough documentation is an explicit goal of this project. It should be easy to learn, use, and get started.
First, fork and
git clone this repository, or install from NPM with
npm i canvasboilerplate.
To get started building, open up main.js. There are three functions there which are the backbone of your application:
- update(): Update the objects in your world -- move them, calculate collision, etc.
- draw(): Draw all the objects and layers onto the main canvas.
- setup(): Set up the objects that should populate your world. This runs after images have been pre-loaded but immediately before animation starts.
These functions are called automatically for you. Just fill in the logic! To see your project, open index.html in your browser. By default, it has a keyboard-controlled player who can wander around.
At this point you can dive in and start building right away, explore more features, or dig deeper into the documentation:
- What's Included & API Overview
- Full API Documentation
- Walk-Through Guide/Tutorial - Build a Mario-style platformer game in part 1 and a C&C-style RTS game in part 2
- Examples in the examples folder — Mario-style platformer (code); Command & Conquer-style RTS (code); Paint (code); and more!
- Boilerplate HTML and CSS: Based on the venerable HTML5 Boilerplate, you get cross-browser-compatibility and standards-compliance for free.
- Easy to learn: You do not have to learn any new APIs to start developing; just open up main.js and go. Extra features provided should be intuitive, and documentation is thorough both online and in the code itself.
- Rapid prototyping: Standard, extendable classes are provided with support for collision, physics, bulk initialization, and more.
- Boilerplate JS: Avoid low-level Canvas APIs and boilerplate setup. Focus on your business logic, and don't worry about setting up the canvas, animation loop, physics timing, image caching, etc. Sprite animation is also supported out of the box.
- Simple interaction: Interacting with the mouse and keyboard is easy, and a simple event model allows the entities on your canvas to be treated as first-class citizens. The canvas even automatically scrolls if your virtual world is too large to fit in view. Positioning HTML over the canvas (e.g. for menus) is straightforward as well.
You can also read a complete overview of everything this project provides.
Why use H5CGB instead of a game engine?
H5CGB is a good choice if you just want to get going quickly without learning a bunch of opinioniated, engine-specific APIs, or if you are mainly interested in avoiding setup but want control over the details. Sometimes game engines are just overkill, or too overwhelming to learn, or try to do too much for you.
Why use H5CGB instead of starting from scratch?
This project is MIT-licensed.
Compatible with all modern browsers, meaning not IE8. Also works in modern mobile browsers as long as you accommodate mobile input methods. The Paint demo is a good example of this.
Contributions are welcome!
I'd love to hear what you make!
Many other people wrote jQuery, classes.js, console-extras.js, stats.js, and parts of jQuery.hotkeys; they are credited in their respective files. Additionally, many people contributed to H5BP, from which most of the HTML and CSS, .htaccess, and robots.txt rules originated. Thanks also to JSDuck for providing the framework for the documentation site, and to Atari for the original image used to derive the centipede example sprite.