1.6.0 • Published 19 days ago

chessboard-vue v1.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
19 days ago

Chessboard Vue

A chess board component for Vue JS (>= 3), written in Typescript.

Usage

  1. With your package manager, add the 'chessboard-vue' package (e.g bun add chessboard-vue)
  2. In you main.js add the following content :
import ChessboardVueComponent from "chessboard-vue";
import "chessboard-vue/dist/style.css";
  1. Don't forget to use the component (in the same file), e.g :
createApp(App)
  .use(ChessboardVueComponent) //add this line
  .use(pinia)
  .use(router);
  1. You can use the ChessboardVue component from any template in your project. E.g :
<ChessboardVue
  :size="500"
  ref="board"
  @checkmate="handleCheckmate"
  @move-done="addHistoryMove"
  :reversed="reversed"
/>

Documentation

Attributes

NamePurposeTypeDefault
sizeDefines the common width and height of the board, in pixels.number100
reversedSays if the black side is at bottom of the board.booleanfalse
backgroundDefines the overall background color of the board.string#124589
coordinatesVisibleSays if the coordinates (around the cells) are visible.booleantrue
coordinatesColorDefines the text color of the coordinates.stringyellow
whiteCellsColorDefines the color of the white cells.stringnavajowhite
blackCellsColorDefines the color of the black cells.stringperu
originCellColorDefines the color of the origin cell when performing a drag and drop for a move.stringcrimson
targetCellColorDefines the color of the target cell when performing a drag and drop for a move.stringForestGreen
dndCrossCellColorDefines the color of the indicator cross cells when performing a drag and drop for a move.stringDimGrey
moveHighlightColorDefines the color of the last move arrow.stringCadetBlue
lastMoveVisibleSays if the last move arrow must be visible.booleantrue
whitePlayerHumanSays if the white side accepts a move from the user, of if it should be handled manually with the playManualMove method.booleantrue
blackPlayerHumanSays if the black side accepts a move from the user, of if it should be handled manually with the playManualMove method.booleantrue

Methods

newGame

Starts a new game with the given position in Forsyth-Edwards Notation. If the startPositionFen string is not given, will use the default chess start position.

Field namePurposeTypeDefault
startPositionFenDefines the position, in Forsth-Edwards Notation, that must be use.stringrnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

This method does not return anything.

stop

Stops the current game, even if it has not been stopped logically yet.

This method does not take any parameter, and does not return anything.

isWhiteTurn

Says, if the turn is to white side, as a boolean.

This method does not take any parameter.

playManualMove

Plays a move manually, without user interaction. This method won't have any effect is game is not in progress, or if the side in turn is defined as a human player.

If the parameter inputMove is given a string, it must conform to the Long Algebraic Notation. Otherwise, it can be an object with the following keys : "from" as the start cell string (e.g: "a4"), "to" as the end cell string, and "promotion" still an optional letter which can be one of "q", "r", "b" or "n".

Field namePurposeTypeDefault
inputMoveDefines the move to be applied.string | { from : string ; to : string ; promotion ? : string }Not defined

This method returns if the call succeed, as a boolean.

gameIsInProgress

Says if the game is in progress, as a boolean.

This method does not take any parameter.

setStartPosition

Sets the start position of the game in the board, but only if the game is not in progress. It returns a boolean indicated the success of the call.

This method does not take any parameter.

It can be useful in order to define an history component and make it interact with the board. Don't forget to check the event @moveDone below.

setPositionAndLastMove

Sets the current position of the game in the board, but only if the game is not in progress. It returns a boolean indicated the success of the call.

The type Move is defined in the Chessboard.vue file (so can be imported too), and means the following :

export interface Move {
  start: Cell;
  end: Cell;
}

where Cell is also defined in the Chessboard.vue file and means the following :

export interface Cell {
  file: number,
  rank: number,
}

Forsyth-Edwards Notation is explained there.

Field namePurposeTypeDefault
positionFenDefines the position to be set in Forsyth-Edwards Notation.stringNot defined
moveDefines the last move arrow to be set values.MoveNot defined

It can be useful in order to define an history component and make it interact with the board. Don't forget to check the event @moveDone below.

getCurrentPosition

Returns the current position of the board, in the format Forsyth-Edwards Notation as a string.

This method does not take any parameter.

setLastBoardArrow

Sets the last board arrow, it can even be when the game is in progress.

This method does not return anything.

The type Move is defined in the Chessboard.vue file (so can be imported too), and means the following :

export interface Move {
  start: Cell;
  end: Cell;
}

where Cell is also defined in the Chessboard.vue file and means the following :

export interface Cell {
  file: number,
  rank: number,
}
Field namePurposeTypeDefault
moveThe coordinates of the last move arrow.MoveNot defined

Events

checkmate

Happens when a checkmate has been done on the board.

Given field namePurposeType
byWhiteSays it the checkmate has been administrated by white side.boolean

stalemate

Happens when a stalemate has been done on the board.

This event does not provide any parameter.

perpetualDraw

Happens when a three-fold repetition has been done on the board.

This event does not provide any parameter.

missingMaterialDraw

Happens when a draw by missing material has been done on the board.

This event does not provide any parameter.

fiftyMovesDraw

Happens when a draw by the 50 moves rule has been done on the board.

This event does not provide any parameter.

waitingManualMove

Indicates that the side which is now in turn cannot be updated with an interaction from the user, and that the board is waiting for a manual move to be done (see also the method playManualMove).

This event does not provide any parameter.

moveDone

Indicates that a move has been done on the board.

SAN is the Standard Algebraic Notation.

FAN is the Figurine Algebraic Notation.

Forsyth-Edwards Notation is explained there.

The type Move is defined in the Chessboard.vue file (so can be imported too), and means the following :

export interface Move {
  start: Cell;
  end: Cell;
}

where Cell is also defined in the Chessboard.vue file and means the following :

export interface Cell {
  file: number,
  rank: number,
}
Given field namePurposeType
moveNumberSays the move number of the given move.number
whiteTurnSays if the given move has been done by white side.boolean
moveSanThe SAN notation of the move.string
moveFanThe FAN notation of the move.string
resultingPositionThe resulting position in Forsyth-Edwards Notationstring
moveThe move definition.Move
promotionThe applied promotion (can be undefined).string?

For developers

Building

  1. Install dependencies : e.g with bun bun install
  2. Build : e.g with bun bun run vue-tsc && bun run vite build

Credits

Pieces vectors definitions from CBurnett and found on Wikimedia commons.

Svg cross has been downloaded from Svg repo

Using ChessJS library, which is bundled in the produced script.

1.6.0

19 days ago

1.5.2

25 days ago

1.5.0

30 days ago

1.4.0

1 month ago

1.2.0

1 month ago

1.0.2

1 month ago

1.1.0

1 month ago

1.0.0

1 month ago

1.3.0

1 month ago

0.1.0

1 month ago

0.0.1

1 month ago