Skip to content

Commit 4415b6f

Browse files
committed
Refactored and added svg.js
1 parent e24091f commit 4415b6f

File tree

5 files changed

+88
-22
lines changed

5 files changed

+88
-22
lines changed

.editorconfig

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# This file is for unifying the coding style for different editors and IDEs
22
# editorconfig.org
33

4-
# PHP PSR-2 Coding Standards
5-
# http://www.php-fig.org/psr/psr-2/
6-
74
root = true
85

96
[*]

.eslintrc.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ rules:
3030
- x
3131
- y
3232
- e
33-
id-match:
34-
- error
35-
- "^(([A-Za-z0-9]+){2,})|([A-Z][A-Z_0-9]+)$"
36-
-
37-
properties: false
38-
onlyDeclarations: true
33+
# id-match:
34+
# - error
35+
# - "^(([A-Za-z0-9]+){2,})|([A-Z][A-Z_0-9]+)$"
36+
# -
37+
# properties: false
38+
# onlyDeclarations: true
3939
indent:
4040
- error
4141
- 2

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Denis Efremov
3+
Copyright (c) 2021 Denis Efremov
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

src/index.js

+26-12
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ const {
1919
BOARD_PADDING,
2020
} = require('./config')
2121

22+
const {
23+
makeDot,
24+
makeCross,
25+
makePiece,
26+
makeScale,
27+
makeSquare,
28+
} = require('./svg')
29+
2230
const app = express()
23-
const stubSvg = fs.readFileSync('stub.svg', { encoding: 'utf-8' })
31+
const stubSvg = fs.readFileSync('stubs/stub.svg', { encoding: 'utf-8' })
2432

2533
const renderSVG = (board, {
2634
marks = [],
@@ -45,16 +53,18 @@ const renderSVG = (board, {
4553
const color = (fileNumber + rank) % 2 ? wCellColor : bCellColor
4654
const squareId = `${file}${rank}`
4755

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 }))
4957

5058
if (piece) {
51-
svgElements.push(`<use xlink:href="#${piece.side.name}-${piece.type}" transform="translate(${x}, ${y})"/>`)
59+
svgElements.push(makePiece({ x, y, piece }))
5260
}
5361

5462
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+
)
5868
}
5969
}
6070

@@ -65,14 +75,18 @@ const renderSVG = (board, {
6575
const file = horizontal[whiteBottom ? 8 - i - 1 : i]
6676
const rank = vertical[whiteBottom ? 8 - i - 1 : i]
6777

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+
}))
7386
}
7487

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(''))
7690
}
7791

7892
app.get('/:fen.jpeg', (req, res) => {

src/svg.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const makeSquare = ({ x, y, squareSize, squareId, color }) => `<rect
2+
x="${x}"
3+
y="${y}"
4+
width="${squareSize}"
5+
height="${squareSize}"
6+
class="square ${squareId}"
7+
stroke="none"
8+
fill="#${color}"
9+
/>`
10+
11+
const makePiece = ({ x, y, piece }) => `<use
12+
xlink:href="#${piece.side.name}-${piece.type}"
13+
transform="translate(${x}, ${y})"
14+
/>`
15+
16+
const makeCross = ({ x, y, crossColor }) => `<g transform="translate(${x}, ${y})">
17+
<path
18+
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"
19+
fill="#${crossColor}"
20+
stroke="#000"
21+
stroke-width="1.688"
22+
/>
23+
</g>`
24+
25+
const makeDot = ({ x, y, squareSize, marksSize, marksColor }) => `<circle
26+
cx="${x + squareSize / 2}"
27+
cy="${y + squareSize / 2}"
28+
r="${marksSize}"
29+
fill="#${marksColor}"
30+
/>`
31+
32+
const makeScale = ({ i, boardPadding, squareSize, textColor, file, rank }) => `<text
33+
transform="translate(${boardPadding + squareSize / 2 + i * squareSize - 3}, 10) scale(.65)"
34+
fill="#${textColor}"
35+
>${file.toUpperCase()}</text>
36+
<text
37+
transform="translate(${squareSize + i * squareSize - 10}, ${squareSize * 8 + boardPadding * 2 - 3}) scale(.65)"
38+
fill="#${textColor}"
39+
>${file.toUpperCase()}</text>
40+
<text
41+
transform="translate(4, ${squareSize + i * squareSize - 3}) scale(.7)"
42+
fill="#${textColor}"
43+
>${rank}</text>
44+
<text
45+
transform="translate(${squareSize * 8 + boardPadding * 2 - 10}, ${squareSize + i * squareSize - 3}) scale(.7)"
46+
fill="#${textColor}"
47+
>${rank}</text>`
48+
49+
module.exports = {
50+
makeDot,
51+
makeCross,
52+
makePiece,
53+
makeScale,
54+
makeSquare,
55+
}

0 commit comments

Comments
 (0)