<div id="board1" style="width: 600px; height: 450px"></div>
var board1 = new ChessBoard3('board1', 'start');
These two lines create a board with default settings, for illustrating the position supplied to the second argument to the constructor- either as a FEN string or 'start' for the starting position.
(By default, pieces are not draggable.)
<div id="board2" style="width: 600px; height: 450px"></div> <input type="button" id="startBtn" value="Start" /> <input type="button" id="clearBtn" value="Clear" /> <input type="button" id="flipBtn" value="Flip" /> <div id="FEN"></div>
var board2 = new ChessBoard3('board2', { draggable: true, dropOffBoard: 'trash', sparePieces: true onChange: function(oldPos, newPos) { $("#FEN").text(ChessBoard3.objToFen(newPos)); } }); $('#startBtn').on('click', board2.start); $('#clearBtn').on('click', board2.clear); $('#flipBtn').on('click', board2.flip);The second argument can also be a configuration object. Here we let the user set up the board, and see the current position as a FEN string.
8/8/8/8/8/8/8/8
Here you can drag pieces onto the board and see the corresponding FEN.
<div id="outer"> <div id="inner"></div> </div> <input type="button" id="2D" value="2D"/> <input type="button" id="3D" value="3D"/>
var sampleConfig = position: 'start', draggable : true, dropOffBoard: 'snapback' }; var board; function setUpBoard(dimensions) { var currentPosition = 'start'; if (board !== undefined) { currentPosition = board.position(); board.destroy(); } if (dimensions >= 3) { $('#inner').css('width', '600px'); $('#inner').css('height', '450px'); $('#outer').css('padding', ''); board = new ChessBoard3('inner', sampleConfig); } else { $('#inner').css('width', '450px'); $('#outer').css('height', '450px'); $('#outer').css('padding', '0px 75px 0px 75px'); board = new ChessBoard('inner', sampleConfig); } board.position(currentPosition, false); } $('#2D').on('click', function() {setUpBoard(2);}); $('#3D').on('click', function() {setUpBoard(3);}); setUpBoard(2); // start with a 2D board
You can easily switch between 2D and 3D boards, since chessboard.js and chessboard3.js expose the same API. You can use chessboard.js as a fallback library, especially if WebGL is not supported in the browser. One major difference is in the aspect ratio. While chessboard.js sets the widget height to be equal to the width in order to make a square widget, chessboard3.js sets its height to 75% of the width for a 4:3 aspect ratio. This example code fiddles a bit with CSS widths and padding so that the 2D board doesn't gobble up extra page height when it appears.