@noe_rls/cm-chessboard
Version:
A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.
70 lines (66 loc) • 2.35 kB
HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<title>cm-chessboard</title>
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0"/>
<link rel="stylesheet" href="styles/examples.css"/>
<link rel="stylesheet" href="../assets/styles/cm-chessboard.css"/>
</head>
<body>
<h1><a href="../">cm-chessboard</a></h1>
<h2>Example: Input enabled without validation</h2>
<p>Input enabled on both sides, without move validation.</p>
<div class="board" id="board"></div>
<pre>
const board = new Chessboard(document.getElementById("board"), {
position: "start",
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"}
})
board.enableMoveInput(inputHandler)
function inputHandler(event) {
switch (event.type) {
case INPUT_EVENT_TYPE.moveStart:
log(`moveStart: ${event.square}`)
return true
case INPUT_EVENT_TYPE.moveDone:
log(`moveDone: ${event.squareFrom}-${event.squareTo}`)
return true
case INPUT_EVENT_TYPE.moveCanceled:
log(`moveCanceled`)
}
}
</pre>
<button style="margin-bottom: 10px"
onclick="window.board.setOrientation(window.board.getOrientation() === 'w' ? 'b' : 'w')">Switch Orientation
</button>
<div id="output" style="width: 100%; overflow: hidden"></div>
<script type="module">
import {INPUT_EVENT_TYPE, Chessboard} from "../src/cm-chessboard/Chessboard.js"
window.board = new Chessboard(document.getElementById("board"), {
position: "start",
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"}
})
window.board.enableMoveInput(inputHandler)
function inputHandler(event) {
console.log(event)
switch (event.type) {
case INPUT_EVENT_TYPE.moveStart:
log(`moveStart: ${event.square}`)
return true
case INPUT_EVENT_TYPE.moveDone:
log(`moveDone: ${event.squareFrom}-${event.squareTo}`)
return true
case INPUT_EVENT_TYPE.moveCanceled:
log(`moveCanceled`)
}
}
const output = document.getElementById("output")
function log(text) {
const log = document.createElement("div")
log.innerText = text
output.appendChild(log)
}
</script>
</body>
</html>