@@ -19,8 +19,16 @@ const {
19
19
BOARD_PADDING ,
20
20
} = require ( './config' )
21
21
22
+ const {
23
+ makeDot,
24
+ makeCross,
25
+ makePiece,
26
+ makeScale,
27
+ makeSquare,
28
+ } = require ( './svg' )
29
+
22
30
const app = express ( )
23
- const stubSvg = fs . readFileSync ( 'stub.svg' , { encoding : 'utf-8' } )
31
+ const stubSvg = fs . readFileSync ( 'stubs/ stub.svg' , { encoding : 'utf-8' } )
24
32
25
33
const renderSVG = ( board , {
26
34
marks = [ ] ,
@@ -45,16 +53,18 @@ const renderSVG = (board, {
45
53
const color = ( fileNumber + rank ) % 2 ? wCellColor : bCellColor
46
54
const squareId = `${ file } ${ rank } `
47
55
48
- 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 } ) )
49
57
50
58
if ( piece ) {
51
- svgElements . push ( `<use xlink:href="# ${ piece . side . name } - ${ piece . type } " transform="translate( ${ x } , ${ y } )"/>` )
59
+ svgElements . push ( makePiece ( { x , y , piece } ) )
52
60
}
53
61
54
62
if ( marks . includes ( squareId ) ) {
55
- svgElements . push ( piece
56
- ? `<g transform="translate(${ x } , ${ y } )"><path d="M35.865 9.135a1.89 1.89 0 0 1 0 2.673L25.173 22.5l10.692 10.692a1.89 1.89 0 0 1 0 2.673 1.89 1.89 0 0 1-2.673 0L22.5 25.173 11.808 35.865a1.89 1.89 0 0 1-2.673 0 1.89 1.89 0 0 1 0-2.673L19.827 22.5 9.135 11.808a1.89 1.89 0 0 1 0-2.673 1.89 1.89 0 0 1 2.673 0L22.5 19.827 33.192 9.135a1.89 1.89 0 0 1 2.673 0z" fill="#${ crossColor } " stroke="#000" stroke-width="1.688"/></g>`
57
- : `<circle cx="${ x + squareSize / 2 } " cy="${ y + squareSize / 2 } " r="${ marksSize } " fill="#${ marksColor } "/>` )
63
+ svgElements . push (
64
+ piece
65
+ ? makeCross ( { x, y, crossColor } )
66
+ : makeDot ( { x, y, squareSize, marksSize, marksColor } ) ,
67
+ )
58
68
}
59
69
}
60
70
@@ -65,14 +75,18 @@ const renderSVG = (board, {
65
75
const file = horizontal [ whiteBottom ? 8 - i - 1 : i ]
66
76
const rank = vertical [ whiteBottom ? 8 - i - 1 : i ]
67
77
68
- svgElements . push ( `<text transform="translate(${ boardPadding + squareSize / 2 + i * squareSize - 3 } , 10) scale(.65)" fill="#${ textColor } ">${ file . toUpperCase ( ) } </text>` )
69
- svgElements . push ( `<text transform="translate(${ squareSize + i * squareSize - 10 } , ${ squareSize * 8 + boardPadding * 2 - 3 } ) scale(.65)" fill="#${ textColor } ">${ file . toUpperCase ( ) } </text>` )
70
-
71
- svgElements . push ( `<text transform="translate(4, ${ squareSize + i * squareSize - 3 } ) scale(.7)" fill="#${ textColor } ">${ rank } </text>` )
72
- svgElements . push ( `<text transform="translate(${ squareSize * 8 + boardPadding * 2 - 10 } , ${ squareSize + i * squareSize - 3 } ) scale(.7)" fill="#${ textColor } ">${ rank } </text>` )
78
+ svgElements . push ( makeScale ( {
79
+ i,
80
+ file,
81
+ rank,
82
+ boardPadding,
83
+ squareSize,
84
+ textColor,
85
+ } ) )
73
86
}
74
87
75
- return stubSvg . split ( '{{bg}}' ) . join ( bgColor ) . split ( '{{board}}' ) . join ( svgElements . join ( '' ) )
88
+ return stubSvg . split ( '{{bg}}' ) . join ( bgColor )
89
+ . split ( '{{board}}' ) . join ( svgElements . join ( '' ) )
76
90
}
77
91
78
92
app . get ( '/:fen.jpeg' , ( req , res ) => {
0 commit comments