Skip to content

Getting started ​

Install ​

Goban has no dependencies and needs no build step. Pick whichever way of loading it suits your page.

From npm ​

sh
npm install @kiranpg/goban
js
import { createBoard, newGame } from '@kiranpg/goban';

The package ships plain ES modules, so it works with any bundler and in Node 20 or later. It includes TypeScript types for the whole API.

From a CDN ​

For a page without a build step, import it from jsDelivr, which serves the package's files as they are:

html
<script type="module">
  import { createBoard } from 'https://cdn.jsdelivr.net/npm/@kiranpg/goban@0.1/src/index.mjs';
</script>

Pin a version, as above, so an update can't change your page unexpectedly. Prefer a CDN that serves files unchanged. CDNs that rebuild packages into their own bundles, such as esm.sh, can break the stone sound, because the board loads its sound file from next to its own code.

From source ​

Copy the src/ folder from the repository next to your page and import src/index.mjs.

Draw a board ​

Give createBoard an element to draw into. The board fills the element's width, and its height follows from that.

html
<div id="board" style="max-width: 640px"></div>

<script type="module">
  import { createBoard } from '@kiranpg/goban';

  const board = await createBoard(document.querySelector('#board'));
</script>

That's a working 19×19 game. People can click to play, and the board refuses illegal moves with a short reason underneath.

Start a different game ​

newGame makes a game value, and show puts it on the board:

js
import { newGame } from '@kiranpg/goban';

board.show(newGame({ size: 9, rules: 'chinese' }));

Sizes are 9, 13, or 19. Rules are 'japanese' (the default) or 'chinese'. Komi defaults to 6.5 under Japanese rules and 7.5 under Chinese rules; pass komi to change it.

Follow the game ​

onChange calls you after every change, whether a person made it or your code did:

js
board.onChange((game, { source }) => {
  console.log(source, game.moveNumber, `${game.toPlay} to play`);
});

Requirements ​

  • Any current browser. The classic style uses Canvas 2D.
  • The realistic style needs WebGL2. Where it's missing, the board falls back to the classic style.
  • For the game core on its own, Node 20 or later, or any JavaScript runtime with ES modules.

Next ​

Released under the MIT License.