Skip to content

Commit

Permalink
combine all png into 1 image per theme
Browse files Browse the repository at this point in the history
  • Loading branch information
octopoulos committed May 1, 2020
1 parent f716773 commit 3b1298d
Show file tree
Hide file tree
Showing 98 changed files with 74 additions and 22 deletions.
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ TODO
DONE CREATED DESCRIPTION
- : 2020-05-01 : save table data in memory for live and archive + instant switch between them
~ : 2020-04-17 : load archive games without loading a new page
2020-05-02 : 2020-05-01 : combine all png into 1 image, for each theme => faster loading
2020-05-01 : 2020-05-01 : delay google ads + analytics
2020-05-01 : 2020-05-01 : show the seasons in archive
2020-05-01 : 2020-05-01 : control highlight color + size, and display it on the board
Expand Down Expand Up @@ -109,7 +110,6 @@ low:
- : 2020-04-23 : redesign event stats: use boxes => more efficient available screen space usage
- : 2029-04-29 : add SVG connectors to the bracket
- : 2020-04-13 : add 3d rendering option to the chessboard
- : 2020-05-01 : combine all png into 1 image, for each theme => faster loading
lowest:
~ : 2020-04-13 : add 3d board
~ : 2020-04-13 : add 3d pieces
Expand Down
16 changes: 11 additions & 5 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ let BOARD_THEMES = {
NAMESPACE_SVG = 'http://www.w3.org/2000/svg',
num_ply,
PIECE_KEYS = Split('alpha chess24 dilena leipzig metro symbol uscf wikipedia'),
PIECE_SIZES = {
_: 80,
metro: 160,
},
PIECE_TYPES = {
_: 'png',
// wikipedia: 'svg', // will enable in the future
},
pgn_moves = [],
players = [{}, {}], // current 2 players
prev_pgn,
Expand All @@ -76,9 +84,6 @@ let BOARD_THEMES = {
1: 'win',
'=': 'draw',
},
SVG_THEMES = {
wikipedia: 1,
},
table_data = {},
TABLES = {
crash: 'gameno=G#|White|Black|Reason|decision=Final decision|action=Action taken|Result|Log',
Expand Down Expand Up @@ -242,10 +247,11 @@ function create_boards() {
function update_board_theme() {
let board_theme = BOARD_THEMES[Y.board_theme],
theme = Y.piece_theme,
theme_ext = SVG_THEMES[theme]? 'svg': 'png';
theme_ext = PIECE_TYPES[theme] || PIECE_TYPES._,
theme_size = PIECE_SIZES[theme] || PIECE_SIZES._;

Keys(xboards).forEach(key => {
xboards[key].set_theme(board_theme, theme, theme_ext, Y.highlight_color, Y.highlight_size);
xboards[key].set_theme(board_theme, theme, theme_ext, theme_size, Y.highlight_color, Y.highlight_size);
});
}

Expand Down
1 change: 1 addition & 0 deletions js/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let globalRoom = 0,
* + handle all messages
*/
function init_sockets() {
return;
socket = io.connect(HOST);
unlistenLogMain();

Expand Down
30 changes: 18 additions & 12 deletions js/xboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ let COLUMN_LETTERS = 'abcdefghijklmnopqrst'.split(''),
cube: 'Change view',
},
LETTER_COLUMNS = Assign({}, ...COLUMN_LETTERS.map((letter, id) => ({[letter]: id}))),
SPRITE_OFFSETS = Assign({}, ...'bknpqrBKNPQR'.split('').map((key, id) => ({[key]: id}))),
// https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
// KQkq is also supported instead of AHah
START_FEN = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w AHah - 0 1';
Expand Down Expand Up @@ -84,8 +85,9 @@ class XBoard {
this.size = options.size || 16;
this.smooth = options.smooth;
this.target = options.target || 'html';
this.theme = 'wikipedia';
this.theme_ext = 'svg';
this.theme = 'chess24';
this.theme_ext = 'png';
this.theme_size = 80;
}

/**
Expand Down Expand Up @@ -389,7 +391,12 @@ class XBoard {
if (dirty & 2) {
if (DEV.board & 1)
LS(`render_html: num_piece=${this.pieces.length}`);
let nodes = [],
let image = `theme/${this.theme}.${this.theme_ext}`,
nodes = [],
piece_size = this.theme_size,
diff = (piece_size - size) / 2,
style = `background-image:url(${image});height:${piece_size}px;width:${piece_size}px`,
transform = `transform:scale(${size / piece_size}) translate(-${diff}px,-${diff}px)`,
xpieces = _('div.xpieces', this.node);

Class(xpieces, 'smooth', this.smooth);
Expand All @@ -399,15 +406,12 @@ class XBoard {
let char = piece.char,
dead = (piece.flag & 1),
coord = piece.coord,
lower = Lower(char),
node = piece.node,
name = (char == lower)? `b${lower}`: `w${lower}`,
image = `theme/${this.theme}/${name}.${this.theme_ext}`;
offset = -SPRITE_OFFSETS[char] * piece_size;

if (node)
node.firstElementChild.src = image;
else {
node = CreateNode('div', `<img src="${image}">`, {class: 'xpiece'});
if (!node) {
let html = `<div style="${style};background-position-x:${offset}px"></div>`;
node = CreateNode('div', html, {class: 'xpiece'});
nodes.push(node);
piece.node = node;
}
Expand All @@ -421,7 +425,7 @@ class XBoard {
x = 7 - x;
y = 7 - y;
}
Style(node, `transform:translate(${x * size}px,${y * size}px)`);
Style(node, `${transform} translate(${x * piece_size}px,${y * piece_size}px)`);
}
}

Expand Down Expand Up @@ -537,15 +541,17 @@ class XBoard {
* @param {string[]} colors [light, dark]
* @param {Object} theme
* @param {string} theme_ext extension for the theme images
* @param {number} theme_size square dimension in px
* @param {string} high_color
* @param {number} high_size
*/
set_theme(colors, theme, theme_ext, high_color, high_size) {
set_theme(colors, theme, theme_ext, theme_size, high_color, high_size) {
if (DEV.board & 1)
LS('set_theme');
this.colors = colors;
this.theme = theme;
this.theme_ext = theme_ext;
this.theme_size = theme_size;
this.high_color = high_color;
this.high_size = high_size;

Expand Down
4 changes: 1 addition & 3 deletions script/convert_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
1) - theme.js = 387KB
- saved png = 184KB ... half the size
- quant png = 114KB
- combined = 83KB
2) only the desired theme will be loaded, if it's chess24, then it's only 21KB to load, instead of 387KB
"""

Expand Down Expand Up @@ -46,9 +47,6 @@ def go(self):
print(f'{num_theme}) {theme}')
continue

if theme == 'wikipedia':
continue

if 'base64' not in line:
continue

Expand Down
43 changes: 42 additions & 1 deletion script/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from time import time
from typing import Any

from PIL import Image, ImageFile

from common import makedirs_safe, read_text_safe, write_text_safe
from css_minify import css_minify

Expand Down Expand Up @@ -69,6 +71,40 @@ def __init__(self, **kwargs):

self.logger = getLogger(self.__class__.__name__)

def combine_pieces(self, folder: str):
"""Combine chess pieces png files into 1 file
"""
if 'metro' in folder:
height = 160
width = 160
else:
height = 80
width = 80
combined = Image.new('RGBA', (width * 12, height), (0, 255, 0, 0))
output = f'{folder}.png'

i = 0
pieces = 'bknpqr'
for color in 'bw':
for piece in pieces:
name = f'{color}{piece}'
image = Image.open(os.path.join(folder, f'{name}.png'))
offset = (i * width, 0)
combined.paste(image, offset)
i += 1

combined.save(output, format='png')
print('a', end='')

def combine_themes(self, folder: str):
"""Combine all pieces of each theme
"""
sources = os.listdir(folder)
for source in sources:
filename = os.path.join(folder, source)
if os.path.isdir(filename):
self.combine_pieces(filename)

def compress_3d(self, data: str) -> str:
"""Compress THREE javascript
"""
Expand Down Expand Up @@ -263,6 +299,11 @@ def synchronise(self) -> bool:
if __name__ == '__main__':
start = time()
sync = Sync()
sync.synchronise()

if 0:
sync.combine_themes(os.path.join(BASE, 'theme'))
else:
sync.synchronise()

end = time()
print(f'\nELAPSED: {end-start:.3f} seconds')
Binary file added theme/alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed theme/alpha/bb.png
Binary file not shown.
Binary file removed theme/alpha/bk.png
Binary file not shown.
Binary file removed theme/alpha/bn.png
Binary file not shown.
Binary file removed theme/alpha/bp.png
Binary file not shown.
Binary file removed theme/alpha/bq.png
Binary file not shown.
Binary file removed theme/alpha/br.png
Binary file not shown.
Binary file removed theme/alpha/wb.png
Binary file not shown.
Binary file removed theme/alpha/wk.png
Binary file not shown.
Binary file removed theme/alpha/wn.png
Binary file not shown.
Binary file removed theme/alpha/wp.png
Binary file not shown.
Binary file removed theme/alpha/wq.png
Binary file not shown.
Binary file removed theme/alpha/wr.png
Binary file not shown.
Binary file added theme/chess24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed theme/chess24/bb.png
Binary file not shown.
Binary file removed theme/chess24/bk.png
Binary file not shown.
Binary file removed theme/chess24/bn.png
Binary file not shown.
Binary file removed theme/chess24/bp.png
Binary file not shown.
Binary file removed theme/chess24/bq.png
Binary file not shown.
Binary file removed theme/chess24/br.png
Binary file not shown.
Binary file removed theme/chess24/wb.png
Binary file not shown.
Binary file removed theme/chess24/wk.png
Binary file not shown.
Binary file removed theme/chess24/wn.png
Binary file not shown.
Binary file removed theme/chess24/wp.png
Binary file not shown.
Binary file removed theme/chess24/wq.png
Binary file not shown.
Binary file removed theme/chess24/wr.png
Diff not rendered.
Binary file added theme/dilena.png
Binary file removed theme/dilena/bb.png
Diff not rendered.
Binary file removed theme/dilena/bk.png
Diff not rendered.
Binary file removed theme/dilena/bn.png
Diff not rendered.
Binary file removed theme/dilena/bp.png
Diff not rendered.
Binary file removed theme/dilena/bq.png
Diff not rendered.
Binary file removed theme/dilena/br.png
Diff not rendered.
Binary file removed theme/dilena/wb.png
Diff not rendered.
Binary file removed theme/dilena/wk.png
Diff not rendered.
Binary file removed theme/dilena/wn.png
Diff not rendered.
Binary file removed theme/dilena/wp.png
Diff not rendered.
Binary file removed theme/dilena/wq.png
Diff not rendered.
Binary file removed theme/dilena/wr.png
Diff not rendered.
Binary file added theme/leipzig.png
Binary file removed theme/leipzig/bb.png
Diff not rendered.
Binary file removed theme/leipzig/bk.png
Diff not rendered.
Binary file removed theme/leipzig/bn.png
Diff not rendered.
Binary file removed theme/leipzig/bp.png
Diff not rendered.
Binary file removed theme/leipzig/bq.png
Diff not rendered.
Binary file removed theme/leipzig/br.png
Diff not rendered.
Binary file removed theme/leipzig/wb.png
Diff not rendered.
Binary file removed theme/leipzig/wk.png
Diff not rendered.
Binary file removed theme/leipzig/wn.png
Diff not rendered.
Binary file removed theme/leipzig/wp.png
Diff not rendered.
Binary file removed theme/leipzig/wq.png
Diff not rendered.
Binary file removed theme/leipzig/wr.png
Diff not rendered.
Binary file added theme/metro.png
Binary file removed theme/metro/bb.png
Diff not rendered.
Binary file removed theme/metro/bk.png
Diff not rendered.
Binary file removed theme/metro/bn.png
Diff not rendered.
Binary file removed theme/metro/bp.png
Diff not rendered.
Binary file removed theme/metro/bq.png
Diff not rendered.
Binary file removed theme/metro/br.png
Diff not rendered.
Binary file removed theme/metro/wb.png
Diff not rendered.
Binary file removed theme/metro/wk.png
Diff not rendered.
Binary file removed theme/metro/wn.png
Diff not rendered.
Binary file removed theme/metro/wp.png
Diff not rendered.
Binary file removed theme/metro/wq.png
Diff not rendered.
Binary file removed theme/metro/wr.png
Diff not rendered.
Binary file added theme/symbol.png
Binary file removed theme/symbol/bb.png
Diff not rendered.
Binary file removed theme/symbol/bk.png
Diff not rendered.
Binary file removed theme/symbol/bn.png
Diff not rendered.
Binary file removed theme/symbol/bp.png
Diff not rendered.
Binary file removed theme/symbol/bq.png
Diff not rendered.
Binary file removed theme/symbol/br.png
Diff not rendered.
Binary file removed theme/symbol/wb.png
Diff not rendered.
Binary file removed theme/symbol/wk.png
Diff not rendered.
Binary file removed theme/symbol/wn.png
Diff not rendered.
Binary file removed theme/symbol/wp.png
Diff not rendered.
Binary file removed theme/symbol/wq.png
Diff not rendered.
Binary file removed theme/symbol/wr.png
Diff not rendered.
Binary file added theme/uscf.png
Binary file removed theme/uscf/bb.png
Diff not rendered.
Binary file removed theme/uscf/bk.png
Diff not rendered.
Binary file removed theme/uscf/bn.png
Diff not rendered.
Binary file removed theme/uscf/bp.png
Diff not rendered.
Binary file removed theme/uscf/bq.png
Diff not rendered.
Binary file removed theme/uscf/br.png
Diff not rendered.
Binary file removed theme/uscf/wb.png
Diff not rendered.
Binary file removed theme/uscf/wk.png
Diff not rendered.
Binary file removed theme/uscf/wn.png
Diff not rendered.
Binary file removed theme/uscf/wp.png
Diff not rendered.
Binary file removed theme/uscf/wq.png
Diff not rendered.
Binary file removed theme/uscf/wr.png
Diff not rendered.
Binary file added theme/wikipedia.png

0 comments on commit 3b1298d

Please sign in to comment.