Skip to content

The board ​

createBoard(container, options) draws a game into an element and returns a Board once it's ready.

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

const board = await createBoard(document.querySelector('#board'), {
  game: newGame({ size: 13 }),
  style: 'realistic',
  background: 'light',
  label: 'Practice board',
});

Options ​

Every option is optional.

OptionDefaultWhat it does
gamenewGame()The game to show first. Its size and mode come with it.
style'classic''classic' or 'realistic'. See Styles.
view'top''top' or 'seated'. Only the realistic style uses it.
background'dark''dark', 'light', or 'transparent'.
coordinatestrueShows coordinate labels around the board.
navigationtrueShows previous and next buttons and the move number under the board.
label'Go board'The board's accessible name, read by screen readers.

Styles ​

  • Classic draws the board from photographed artwork with Canvas 2D. It's light and works everywhere.
  • Realistic path-traces the board in a softly lit room with WebGL2. It can be seen from above (view: 'top') or from a player's seat (view: 'seated'). Its board has the proportions of a real Japanese table board, so it's slightly taller than it is wide.

Where WebGL2 isn't available, a realistic board falls back to classic. configure tells you which style is actually in use.

Backgrounds ​

background sets what's drawn around the board.

  • 'dark' and 'light' frame the classic board, and put the realistic board on a walnut or linen table.
  • 'transparent' draws only the board, so it sits directly on your page.

Change settings later ​

configure changes any setting in place. The game, hover, and keyboard focus stay as they were.

js
const settings = await board.configure({ style: 'realistic', view: 'seated' });
console.log(settings.style); // 'classic' if WebGL2 wasn't available

configure also controls two settings that createBoard doesn't take. Both start off:

  • sound: plays a stone sound for every stone placed. The sound file loads the first time you turn it on.
  • stats: shows the realistic style's frame rate and sampling progress under the board.

Play mode and review mode ​

What the board shows around itself follows the mode of the game it shows:

  • Play mode is a live game: the board, with navigation underneath. Moves are played at the latest move only.
  • Review mode adds a sidebar with mark tools, the move tree, a comment box, and Open and Save SGF buttons. Playing at an earlier move starts a variation.

To switch, show a game in the other mode:

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

const result = setMode(board.state, 'review');
if (result.ok) board.show(result.game);

On narrow containers the review sidebar moves under the board. The layout follows the container's width, not the window's.

Keyboard ​

  • When the board has focus, the arrow keys move a cursor over the points, and Enter or Space plays at the cursor.
  • With navigation shown, the left and right arrow keys step back and forward through the moves, unless a text field or the board itself is using them.

Read the board's state ​

  • board.state is the game currently shown.
  • board.style, board.view, and board.coordinates are the current settings.

Remove a board ​

board.destroy() removes the board, stops its listeners, and frees its drawing resources. Call it when you take the board off the page, particularly in single-page apps, where browsers limit how many WebGL contexts can be open at once.

Released under the MIT License.