UNPKG

chessground12

Version:
62 lines 2.33 kB
import { start } from './api'; import { configure } from './config'; import { defaults } from './state'; import { renderWrap } from './wrap'; import { render, renderResized, updateBounds } from './render'; import { renderPockets, renderPocketsInitial } from './pocket'; import { bindBoard, bindDocument } from './events'; import { renderSvg } from './svg'; import { memo } from './util'; export function Chessground(element, config, _height, pocketTop, pocketBottom) { const maybeState = defaults(); configure(maybeState, config || {}); function redrawAll() { const prevUnbind = 'dom' in maybeState ? maybeState.dom.unbind : undefined; // compute bounds from existing board element if possible // this allows non-square boards from CSS to be handled (for 3D) const elements = renderWrap(element, maybeState), bounds = memo(() => elements.board.getBoundingClientRect()), redrawNow = (skipSvg) => { render(state); renderPockets(state); if (!skipSvg && elements.svg) renderSvg(state, elements.svg, elements.customSvg); }, onResize = () => { updateBounds(state); renderResized(state); }; if (elements.pocketTop) pocketTop = elements.pocketTop; if (elements.pocketBottom) pocketBottom = elements.pocketBottom; renderPocketsInitial(maybeState, elements, pocketTop, pocketBottom); const state = maybeState; state.dom = { elements, bounds, redraw: debounceRedraw(redrawNow), redrawNow, unbind: prevUnbind, }; state.drawable.prevSvgHash = ''; updateBounds(state); redrawNow(false); bindBoard(state, onResize); if (!prevUnbind) state.dom.unbind = bindDocument(state, onResize); state.events.insert && state.events.insert(elements); return state; } return start(redrawAll(), redrawAll); } function debounceRedraw(redrawNow) { let redrawing = false; return () => { if (redrawing) return; redrawing = true; requestAnimationFrame(() => { redrawNow(); redrawing = false; }); }; } //# sourceMappingURL=chessground.js.map