UNPKG

kontra

Version:

Kontra HTML5 game development library

276 lines (240 loc) 12.8 kB
<!DOCTYPE html> <html lang="en"> <head> <title>Kontra.js – Getting Started</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <base href="https://straker.github.io/kontra/"> <script> // adjust path based on location (github pages required kontra url) if (window.location.host.indexOf('localhost') !== -1) { let base = document.querySelector('base'); base.setAttribute('href', '/'); } </script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/themes/prism.min.css"> <link rel="stylesheet" href="assets/styles.css"> <script src="assets/js/kontra.js"></script> </head> <body> <div class="content"> <header class="main-nav"> <div id="kontra-heading" class="nav-heading">Kontra</div> <a href="#main">Skip navigation</a> <nav> <button class="menu-button" aria-expanded="false" aria-controls="menu"><span aria-hidden="true"></span> Menu</button> <!-- add role=list back to list so screen readers still read it as list when css list-style: none is set @see https://web-a11y.slack.com/archives/C042TSFGN/p1501699529181172 --> <ul id="menu" role="list"> <li><a href="." >Introduction</a></li> <li><a href="getting-started" aria-current="page">Getting Started</a></li> <li><a href="download" >Download</a></li> <li><a href="tutorials" >Tutorials</a></li> <li><a href="made-with-kontra" >Made With Kontra</a></li> <li> <span id="api" class="nav-api-heading">API</span> <ul aria-labelledby="api"> <li><a href="api/animation" >Animation</a></li> <li><a href="api/assets" >Assets</a></li> <li><a href="api/core" >Core</a></li> <li><a href="api/events" >Events</a></li> <li><a href="api/gameLoop" >GameLoop</a></li> <li><a href="api/keyboard" >Keyboard</a></li> <li><a href="api/plugin" >Plugin</a></li> <li><a href="api/pointer" >Pointer</a></li> <li><a href="api/pool" >Pool</a></li> <li><a href="api/quadtree" >Quadtree</a></li> <li><a href="api/sprite" >Sprite</a></li> <li><a href="api/spriteSheet" >SpriteSheet</a></li> <li><a href="api/store" >Store</a></li> <li><a href="api/tileEngine" >TileEngine</a></li> <li><a href="api/vector" >Vector</a></li> </ul> </li> </ul> </nav> <div class="scroll-indicator" aria-hidden="true"><span>^</span></div> </header> <main id="main"> <div> <a href="https://github.com/straker/kontra" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a> <h1>Getting Started </h1> <p>Kontra is written for modern ES6 capable browsers. If you need to run it in an ES5 browser, you will have to <a href="https://babeljs.io/">transpile</a> and polyfill needed ES6 features (Assets requires <code>WeakMap</code> and <code>Promise</code>).</p> <h2 id="download">Download</h2> <ul> <li>Global object <a href="https://raw.githubusercontent.com/straker/kontra/master/kontra.min.js">production</a> and <a href="https://raw.githubusercontent.com/straker/kontra/master/kontra.js">development</a> versions</li> <li>ES module <a href="https://raw.githubusercontent.com/straker/kontra/master/kontra.min.mjs">production</a> and <a href="https://raw.githubusercontent.com/straker/kontra/master/kontra.mjs">development</a> versions</li> <li><code>$ npm install kontra</code></li> </ul> <h2 id="load">Load</h2> <p>There are a few different ways to load the library depending on how you are going to use it.</p> <h3 id="global-object">Global Object</h3> <p>Load the library by adding it as a script tag. This will add a global <code>kontra</code> object to the page with all Kontra functions and properties.</p> <pre><code class="language-html">&lt;script src=&quot;path/to/kontra.js&quot;&gt;&lt;/script&gt;</code></pre> <h3 id="es-module-import">ES Module Import</h3> <p>Kontra also supports ES modules and exports all API functions, allowing you to import it into your code as well. Import the file <code>kontra.mjs</code> and either use the entire <code>kontra</code> object or just import the functions you need.</p> <pre><code class="language-js">import kontra from &#39;path/to/kontra.mjs&#39;;</code></pre> <h3 id="module-bundler">Module Bundler</h3> <p>If you&#39;re using a module bundler that supports npm dependency resolution (such as Webpack or Parcel), you can import the library directly from the module name. This is the recommended way to create <a href="download">custom builds</a> of the library.</p> <pre><code class="language-js">import kontra from &#39;kontra&#39;;</code></pre> <h3 id="web-maker">Web Maker</h3> <p>Want to get started without all the hassle? <a href="https://webmakerapp.com/">Web Maker</a> has you covered! When you start a new project, select the Kontra Game Engine from the list of predefined templates and you&#39;re good to go. Learn more by reading the <a href="https://medium.com/web-maker/js13kgames-jam-with-web-maker-a3389cf2cbb">Web Maker and JS13k tutorial</a>.</p> <h2 id="initialize">Initialize</h2> <p>Initialize the game by calling <a href="api/core.html#init">init()</a> to create the drawing context. By default, it will use the first canvas element on the page, but you can also pass it the ID of the canvas or a Canvas element.</p> <p>The <code>init()</code> function returns the canvas and context used for the game.</p> <div class="tablist"> <ul role="tablist"> <li role="presentation" data-tab="global"> <button role="tab" id="getting-started-global-tab">Global Object</button> </li> <li role="presentation" data-tab="es"> <button role="tab" id="getting-started-es-tab">ES Module Import</button> </li> <li role="presentation" data-tab="bundle"> <button role="tab" id="getting-started-bundle-tab">Module Bundler</button> </li> <li role="presentation"></li> </ul> <section role="tabpanel" aria-labelledby=getting-started-global-tab data-tabpanel="global"><pre><code class="language-js">let { init } = kontra; let { canvas, context } = init();</code></pre></section> <section role="tabpanel" aria-labelledby=getting-started-es-tab data-tabpanel="es"><pre><code class="language-js">import { init } from 'path/to/kontra.mjs'; let { canvas, context } = init();</code></pre></section> <section role="tabpanel" aria-labelledby=getting-started-bundle-tab data-tabpanel="bundle"><pre><code class="language-js">import { init } from &#39;kontra&#39;; let { canvas, context } = init();</code></pre></section> </div> <h2 id="create">Create</h2> <p>After the game is initialize, you can create a simple <a href="api/sprite.html">Sprite</a> and <a href="api/gameLoop.html">Game Loop</a> in just a few lines of code</p> <canvas id="game-canvas-8"></canvas> <div class="tablist"> <ul role="tablist"> <li role="presentation" data-tab="global"> <button role="tab" id="game-canvas-8-global-tab">Global Object</button> </li> <li role="presentation" data-tab="es"> <button role="tab" id="game-canvas-8-es-tab">ES Module Import</button> </li> <li role="presentation" data-tab="bundle"> <button role="tab" id="game-canvas-8-bundle-tab">Module Bundler</button> </li> <li role="presentation"></li> </ul> <section role="tabpanel" aria-labelledby=game-canvas-8-global-tab data-tabpanel="global"> <pre><code class="lang-js">let { init, Sprite, GameLoop } &#x3D; kontra let { canvas } &#x3D; init(); let sprite &#x3D; Sprite({ x: 100, // starting x,y position of the sprite y: 80, color: &#x27;red&#x27;, // fill color of the sprite rectangle width: 20, // width and height of the sprite rectangle height: 40, dx: 2 // move the sprite 2px to the right every frame }); let loop &#x3D; GameLoop({ // create the main game loop update: function() { // update the game state sprite.update(); // wrap the sprites position when it reaches // the edge of the screen if (sprite.x &gt; canvas.width) { sprite.x &#x3D; -sprite.width; } }, render: function() { // render the game state sprite.render(); } }); loop.start(); // start the game</code></pre> </section> <section role="tabpanel" aria-labelledby=game-canvas-8-es-tab data-tabpanel="es"> <pre><code class="lang-js">import { init, Sprite, GameLoop } from &#x27;path/to/kontra.mjs&#x27; let { canvas } &#x3D; init(); let sprite &#x3D; Sprite({ x: 100, // starting x,y position of the sprite y: 80, color: &#x27;red&#x27;, // fill color of the sprite rectangle width: 20, // width and height of the sprite rectangle height: 40, dx: 2 // move the sprite 2px to the right every frame }); let loop &#x3D; GameLoop({ // create the main game loop update: function() { // update the game state sprite.update(); // wrap the sprites position when it reaches // the edge of the screen if (sprite.x &gt; canvas.width) { sprite.x &#x3D; -sprite.width; } }, render: function() { // render the game state sprite.render(); } }); loop.start(); // start the game</code></pre> </section> <section role="tabpanel" aria-labelledby=game-canvas-8-bundle-tab data-tabpanel="bundle"> <pre><code class="lang-js">import { init, Sprite, GameLoop } from &#x27;kontra&#x27;; let { canvas } &#x3D; init(); let sprite &#x3D; Sprite({ x: 100, // starting x,y position of the sprite y: 80, color: &#x27;red&#x27;, // fill color of the sprite rectangle width: 20, // width and height of the sprite rectangle height: 40, dx: 2 // move the sprite 2px to the right every frame }); let loop &#x3D; GameLoop({ // create the main game loop update: function() { // update the game state sprite.update(); // wrap the sprites position when it reaches // the edge of the screen if (sprite.x &gt; canvas.width) { sprite.x &#x3D; -sprite.width; } }, render: function() { // render the game state sprite.render(); } }); loop.start(); // start the game</code></pre> </section> </div> <script>(function() { kontra.init("game-canvas-8"); var canvas = document.querySelector("#game-canvas-8"); canvas.width = 600; canvas.height = 200; var context = canvas.getContext("2d"); // exclude-code:start let { init, Sprite, GameLoop } = kontra; // exclude-code:end let sprite = Sprite({ x: 100, // starting x,y position of the sprite y: 80, color: 'red', // fill color of the sprite rectangle width: 20, // width and height of the sprite rectangle height: 40, dx: 2 // move the sprite 2px to the right every frame }); let loop = GameLoop({ // create the main game loop update: function() { // update the game state sprite.update(); // wrap the sprites position when it reaches // the edge of the screen if (sprite.x > canvas.width) { sprite.x = -sprite.width; } }, render: function() { // render the game state sprite.render(); } }); loop.start(); // start the game })();</script> </div> </main> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/prism.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/components/prism-javascript.min.js"></script> <script src="assets/js/navbar.js"></script> <script src="assets/js/exampleTabList.js"></script> </body> </html>