Categorygithub.com/hultan/chessimage
repositorypackage
1.0.1
Repository: https://github.com/hultan/chessimage.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

chessimage

chessimage is a golang library for rendering a chess board PNG in specific state.

GoDoc

go run examples/starting_board.go | open -f -a /Applications/Preview.app/

Basic Usage

Include in your go path.

go get github.com/cjsaylor/chessimage

Initialize the renderer with a FEN notation.

board, _ := chessimage.NewRendererFromFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")

Render the chess board to a png image.Image interface. This will use the internal (embedded) images, that are licensed under the CC license (see ./assets/license.md)

f, _ := os.Create("board.png")
defer f.Close()
image, _ := board.Render(chessimage.Options{})
png.Encode(f, image)

If you want to use your own chess piece images, provide a path to an asset folder:

f, _ := os.Create("board.png")
defer f.Close()
image, _ := board.Render(chessimage.Options{AssetPath: "./assets/"})
png.Encode(f, image)

Highlighting LastMove

You can highlight tiles of where a move started and ended.

board.SetLastMove(chessimage.LastMove{
	From: chessimage.E4,
	To: chessimage.E2,
})

Example

To highlight a castling move, use the MoveType field of the LastMove struct. Note that the From and To fields are not being used when highlighting castling moves.

board.SetLastMove(chessimage.LastMove{
	MoveType: chessimage.MoveTypeCastlingWK,
})

Example

Mark Checked

You can highlight a tile as "checked".

board.SetCheckTile(chessimage.G1)

Example

Options

You can define rendering options at render time:

options := chessimage.Options{
	AssetPath: "./assets/"
}
renderer.Render(options)

AssetPath

Specify the path of the image assets for the individual pieces. Feel free to use the assets embedded in this repo, but be aware they are under CC license.

Inverted (false)

Invert the board so that it displays correctly for the black player. By default the white player view is rendered. This is a boolean option.

Resizer (draw.CatmullRom)

Change the algorhythm for asset resizing. Depending on your performance requirements, you may need to use a faster (but more lossy) resizing method (like draw.NearestNeighbor).

BoardSize (512)

Square board size in pixels

PieceRatio (0.8)

Size of the pieces relative as a percentage to the game board tile size. If the game board size is 800, each board tile would be 100 pixels wide, and the pieces would render at 80 pixels with the default ratio.

Todo

  • Add support for PGN notation for rendering a board (similar to the FEN notation setup now)
  • Add configuration support for changing board and tile highlight colors