Skip to content

Commit

Permalink
lib: ship wikimedia piece svgs as built-in data urls
Browse files Browse the repository at this point in the history
  • Loading branch information
zackschuster committed Oct 4, 2022
1 parent b030f0b commit b84d360
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 22 deletions.
13 changes: 10 additions & 3 deletions lib/chessboard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-env browser */

import * as pieces from './pieces.svg.js';

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -439,10 +441,10 @@ function expandConfig(config) {
// draggable must be true if sparePieces is enabled
if (config.sparePieces) config.draggable = true;

// default piece theme is wikipedia
// default piece theme is built-in svg
if (!Object.prototype.hasOwnProperty.call(config, 'pieceTheme') ||
(typeof config.pieceTheme !== 'string' && typeof config.pieceTheme !== 'function')) {
config.pieceTheme = 'img/chesspieces/wikipedia/{piece}.png';
config.pieceTheme = '{piece}';
}

// animation speeds
Expand Down Expand Up @@ -498,6 +500,10 @@ function checkContainerArg(containerElOrString) {
// ---------------------------------------------------------------------------

class Chessboard {
static pieces = pieces;
static fenToObj = fenToObj;
static objToFen = objToFen;

#config = {};
/**
* @type {HTMLElement}
Expand Down Expand Up @@ -898,11 +904,12 @@ class Chessboard {

#buildPiece(piece, hidden, id) {
const pieceEl = new Image(this.#squareSize, this.#squareSize);
const src = this.#buildPieceImgSrc(piece);

pieceEl.src = src === piece ? pieces[src] : src;
pieceEl.alt = '';
pieceEl.classList.add(ClassNameLookup.piece);
pieceEl.dataset.piece = piece;
pieceEl.src = this.#buildPieceImgSrc(piece);

if (typeof id === 'string' && id.length > 0) {
pieceEl.id = id;
Expand Down
36 changes: 36 additions & 0 deletions lib/pieces.svg.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions templates/homepage.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<div class="hero-wrapper">
<div class="body-width-115f6 hero-inner-556fe">
<img src="img/chesspieces/wikipedia/bK.svg" alt="Black King" />
<img id="hero-king" alt="Black King" />
<h1>chessboard.js</h1>
<h3>The easiest way to embed a chess board on your site.</h3>
<a href="releases/chessboardjs-1.0.0.zip">Download v1.0.0</a>
Expand Down Expand Up @@ -62,11 +62,8 @@
<script src="js/prettify.min.js"></script>
{{{chessboardJsScript}}}
<script>
;(function () {
function isTouchDevice () {
return ('ontouchstart' in document.documentElement)
}
const heroKing = document.getElementById('hero-king');
heroKing.src = Chessboard.pieces.bK;
prettyPrint()
// example 1
Expand All @@ -82,7 +79,6 @@
if (isTouchDevice()) {
document.querySelectorAll('.navbar-a57cc').forEach(x => x.classList.remove('hover-effect'))
}
})()
</script>
</body>
</html>
Binary file removed website/img/chesspieces/wikipedia/bB.png
Binary file not shown.
Binary file removed website/img/chesspieces/wikipedia/bK.png
Binary file not shown.
12 changes: 0 additions & 12 deletions website/img/chesspieces/wikipedia/bK.svg

This file was deleted.

Binary file removed website/img/chesspieces/wikipedia/bN.png
Binary file not shown.
Binary file removed website/img/chesspieces/wikipedia/bP.png
Binary file not shown.
Binary file removed website/img/chesspieces/wikipedia/bQ.png
Binary file not shown.
Binary file removed website/img/chesspieces/wikipedia/bR.png
Binary file not shown.
Binary file removed website/img/chesspieces/wikipedia/wB.png
Binary file not shown.
Binary file removed website/img/chesspieces/wikipedia/wK.png
Binary file not shown.
Binary file removed website/img/chesspieces/wikipedia/wN.png
Binary file not shown.
Binary file removed website/img/chesspieces/wikipedia/wP.png
Binary file not shown.
Binary file removed website/img/chesspieces/wikipedia/wQ.png
Binary file not shown.
Binary file removed website/img/chesspieces/wikipedia/wR.png
Binary file not shown.

0 comments on commit b84d360

Please sign in to comment.