@@ -5,70 +5,88 @@ const express = require('express')
5
5
const { StaticCanvas, loadSVGFromString, util : { groupSVGElements } } = require ( 'fabric' ) . fabric
6
6
const { Board } = require ( 'chess/dist/board' )
7
7
8
+ const {
9
+ FILES ,
10
+ BG_COLOR ,
11
+ BOARD_SIZE ,
12
+ MARKS_SIZE ,
13
+ TEXT_COLOR ,
14
+ CROSS_COLOR ,
15
+ MARKS_COLOR ,
16
+ SQUARE_SIZE ,
17
+ B_CELL_COLOR ,
18
+ W_CELL_COLOR ,
19
+ BOARD_PADDING ,
20
+ } = require ( './config' )
21
+
22
+ const {
23
+ makeDot,
24
+ makeCross,
25
+ makePiece,
26
+ makeScale,
27
+ makeSquare,
28
+ } = require ( './svg' )
29
+
8
30
const app = express ( )
9
- const stubSvg = fs . readFileSync ( 'stub.svg' , { encoding : 'utf-8' } )
10
-
11
- const W_CELL_COLOR = 'f0d9b5'
12
- const B_CELL_COLOR = 'b58863'
13
- const BG_COLOR = '212121'
14
- const TEXT_COLOR = 'e5e5e5'
15
- const MARKS_COLOR = 'aaa23b'
16
- const MARKS_SIZE = 7
17
- const BOARD_SIZE = 512
18
- const SQUARE_SIZE = 45
19
- const BOARD_PADDING = 15
20
- const FILES = 'abcdefgh'
31
+ const stubSvg = fs . readFileSync ( 'stubs/stub.svg' , { encoding : 'utf-8' } )
21
32
22
33
const renderSVG = ( board , {
23
34
marks = [ ] ,
24
35
bgColor,
25
36
marksSize,
26
37
textColor,
38
+ crossColor,
27
39
marksColor,
28
40
bCellColor,
29
41
wCellColor,
30
42
squareSize,
31
43
whiteBottom,
44
+ boardPadding,
32
45
} ) => {
33
46
const svgElements = [ ]
34
47
35
48
for ( let i = 0 ; i < board . squares . length ; i += 1 ) {
36
49
const { file, rank, piece } = board . squares [ i ]
37
50
const fileNumber = FILES . indexOf ( file ) + 1
38
- const x = ( ( whiteBottom ? 9 - fileNumber : fileNumber ) - 1 ) * squareSize + BOARD_PADDING
39
- const y = ( ( whiteBottom ? rank : 9 - rank ) - 1 ) * squareSize + BOARD_PADDING
51
+ const x = ( ( whiteBottom ? 9 - fileNumber : fileNumber ) - 1 ) * squareSize + boardPadding
52
+ const y = ( ( whiteBottom ? rank : 9 - rank ) - 1 ) * squareSize + boardPadding
40
53
const color = ( fileNumber + rank ) % 2 ? wCellColor : bCellColor
41
54
const squareId = `${ file } ${ rank } `
42
55
43
- svgElements . push ( `<rect x=" ${ x } " y=" ${ y } " width=" ${ squareSize } " height=" ${ squareSize } " class="square ${ squareId } " stroke="none" fill="# ${ color } "/>` )
56
+ svgElements . push ( makeSquare ( { x , y , squareSize, squareId, color } ) )
44
57
45
58
if ( piece ) {
46
- svgElements . push ( `<use xlink:href="# ${ piece . side . name } - ${ piece . type } " transform="translate( ${ x } , ${ y } )"/>` )
59
+ svgElements . push ( makePiece ( { x , y , piece } ) )
47
60
}
61
+
48
62
if ( marks . includes ( squareId ) ) {
49
- if ( piece ) {
50
- svgElements . push ( `<use xlink:href="#xx" transform="translate( ${ x } , ${ y } )"/>` )
51
- } else {
52
- svgElements . push ( `<circle cx=" ${ x + squareSize / 2 } " cy=" ${ y + squareSize / 2 } " r=" ${ marksSize } " fill="# ${ marksColor } "/>` )
53
- }
63
+ svgElements . push (
64
+ piece
65
+ ? makeCross ( { x , y , crossColor } )
66
+ : makeDot ( { x , y , squareSize, marksSize, marksColor } ) ,
67
+ )
54
68
}
55
69
}
56
70
57
71
const horizontal = FILES . split ( '' )
58
72
const vertical = Array . from ( { length : 8 } , ( item , idx ) => 8 - idx )
59
73
60
74
for ( let i = 0 ; i < 8 ; i += 1 ) {
61
- const file = horizontal [ whiteBottom ? i : 8 - i - 1 ]
62
- const rank = vertical [ whiteBottom ? i : 8 - i - 1 ]
63
-
64
- svgElements . push ( `<text transform="translate(${ BOARD_PADDING + squareSize / 2 + i * squareSize - 3 } , 10) scale(.65)" fill="#${ textColor } ">${ file . toUpperCase ( ) } </text>` )
65
- svgElements . push ( `<text transform="translate(${ squareSize + i * squareSize - 10 } , ${ squareSize * 8 + BOARD_PADDING * 2 - 3 } ) scale(.65)" fill="#${ textColor } ">${ file . toUpperCase ( ) } </text>` )
66
-
67
- svgElements . push ( `<text transform="translate(4, ${ squareSize + i * squareSize - 3 } ) scale(.7)" fill="#${ textColor } ">${ rank } </text>` )
68
- svgElements . push ( `<text transform="translate(${ squareSize * 8 + BOARD_PADDING * 2 - 10 } , ${ squareSize + i * squareSize - 3 } ) scale(.7)" fill="#${ textColor } ">${ rank } </text>` )
75
+ const file = horizontal [ whiteBottom ? 8 - i - 1 : i ]
76
+ const rank = vertical [ whiteBottom ? 8 - i - 1 : i ]
77
+
78
+ svgElements . push ( makeScale ( {
79
+ i,
80
+ file,
81
+ rank,
82
+ boardPadding,
83
+ squareSize,
84
+ textColor,
85
+ } ) )
69
86
}
70
87
71
- return stubSvg . split ( '{{bg}}' ) . join ( bgColor ) . split ( '{{board}}' ) . join ( svgElements . join ( '' ) )
88
+ return stubSvg . split ( '{{bg}}' ) . join ( bgColor )
89
+ . split ( '{{board}}' ) . join ( svgElements . join ( '' ) )
72
90
}
73
91
74
92
app . get ( '/:fen.jpeg' , ( req , res ) => {
@@ -77,14 +95,15 @@ app.get('/:fen.jpeg', (req, res) => {
77
95
rotate = 0 ,
78
96
marks : marksList = '' ,
79
97
bg_color : bgColor = BG_COLOR ,
80
- marks_color : marksColor = MARKS_COLOR ,
81
- marks_size : marksSize = MARKS_SIZE ,
82
98
board_size : boardSize = BOARD_SIZE ,
99
+ marks_size : marksSize = MARKS_SIZE ,
83
100
text_color : textColor = TEXT_COLOR ,
101
+ cross_color : crossColor = CROSS_COLOR ,
102
+ marks_color : marksColor = MARKS_COLOR ,
84
103
square_size : squareSize = SQUARE_SIZE ,
85
- board_padding : boardPadding = BOARD_PADDING ,
86
- w_cell_color : wCellColor = W_CELL_COLOR ,
87
104
b_cell_color : bCellColor = B_CELL_COLOR ,
105
+ w_cell_color : wCellColor = W_CELL_COLOR ,
106
+ board_padding : boardPadding = BOARD_PADDING ,
88
107
} = req . query
89
108
const { fen = 'rnbkqbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBKQBNR' } = req . params
90
109
@@ -98,6 +117,7 @@ app.get('/:fen.jpeg', (req, res) => {
98
117
bgColor,
99
118
marksSize,
100
119
textColor,
120
+ crossColor,
101
121
marksColor,
102
122
squareSize,
103
123
bCellColor,
0 commit comments