UNPKG

chessground

Version:
94 lines 3.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.clear = exports.cancel = exports.end = exports.move = exports.processDraw = exports.start = void 0; const board_1 = require("./board"); const util_1 = require("./util"); const brushes = ['green', 'red', 'blue', 'yellow']; function start(state, e) { if (e.touches && e.touches.length > 1) return; e.stopPropagation(); e.preventDefault(); e.ctrlKey ? board_1.unselect(state) : board_1.cancelMove(state); const pos = util_1.eventPosition(e), orig = board_1.getKeyAtDomPos(pos, board_1.whitePov(state), state.dom.bounds()); if (!orig) return; state.drawable.current = { orig, pos, brush: eventBrush(e), snapToValidMove: state.drawable.defaultSnapToValidMove, }; processDraw(state); } exports.start = start; function processDraw(state) { requestAnimationFrame(() => { const cur = state.drawable.current; if (cur) { const keyAtDomPos = board_1.getKeyAtDomPos(cur.pos, board_1.whitePov(state), state.dom.bounds()); if (!keyAtDomPos) { cur.snapToValidMove = false; } const mouseSq = cur.snapToValidMove ? board_1.getSnappedKeyAtDomPos(cur.orig, cur.pos, board_1.whitePov(state), state.dom.bounds()) : keyAtDomPos; if (mouseSq !== cur.mouseSq) { cur.mouseSq = mouseSq; cur.dest = mouseSq !== cur.orig ? mouseSq : undefined; state.dom.redrawNow(); } processDraw(state); } }); } exports.processDraw = processDraw; function move(state, e) { if (state.drawable.current) state.drawable.current.pos = util_1.eventPosition(e); } exports.move = move; function end(state) { const cur = state.drawable.current; if (cur) { if (cur.mouseSq) addShape(state.drawable, cur); cancel(state); } } exports.end = end; function cancel(state) { if (state.drawable.current) { state.drawable.current = undefined; state.dom.redraw(); } } exports.cancel = cancel; function clear(state) { if (state.drawable.shapes.length) { state.drawable.shapes = []; state.dom.redraw(); onChange(state.drawable); } } exports.clear = clear; function eventBrush(e) { var _a; const modA = (e.shiftKey || e.ctrlKey) && util_1.isRightButton(e); const modB = e.altKey || e.metaKey || ((_a = e.getModifierState) === null || _a === void 0 ? void 0 : _a.call(e, 'AltGraph')); return brushes[(modA ? 1 : 0) + (modB ? 2 : 0)]; } function addShape(drawable, cur) { const sameShape = (s) => s.orig === cur.orig && s.dest === cur.dest; const similar = drawable.shapes.find(sameShape); if (similar) drawable.shapes = drawable.shapes.filter(s => !sameShape(s)); if (!similar || similar.brush !== cur.brush) drawable.shapes.push(cur); onChange(drawable); } function onChange(drawable) { if (drawable.onChange) drawable.onChange(drawable.shapes); } //# sourceMappingURL=draw.js.map