Skip to content

Commit 1c73af6

Browse files
committed
Sizes fixes
1 parent 28ccb85 commit 1c73af6

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "chessboard-svg-image",
3-
"version": "0.0.2",
4-
"description": "Microservice render jpeg image of chessboard by FEN.",
3+
"version": "0.0.3",
4+
"description": "Chess board JPEG image generator microservice",
55
"main": "src/index.js",
66
"scripts": {
77
"lint": "eslint --ext .js src",

src/index.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const stubSvg = fs.readFileSync('stubs/stub.svg', { encoding: 'utf-8' })
3434

3535
const renderSVG = (board, {
3636
marks = [],
37+
scale,
3738
arrows = [],
3839
bgColor,
3940
marksSize,
@@ -59,14 +60,14 @@ const renderSVG = (board, {
5960
svgElements.push(makeSquare({ x, y, squareSize, squareId, color }))
6061

6162
if (piece) {
62-
svgElements.push(makePiece({ x, y, piece }))
63+
svgElements.push(makePiece({ x, y, piece, scale }))
6364
}
6465

6566
if (marks.includes(squareId)) {
6667
svgElements.push(
6768
piece
68-
? makeCross({ x, y, crossColor })
69-
: makeDot({ x, y, squareSize, marksSize, marksColor }),
69+
? makeCross({ x, y, crossColor, scale })
70+
: makeDot({ x, y, squareSize, marksSize, marksColor, scale }),
7071
)
7172
}
7273
}
@@ -101,6 +102,7 @@ const renderSVG = (board, {
101102
}
102103

103104
return stubSvg
105+
.split('{{fullWidth}}').join(squareSize * 8 + boardPadding * 2)
104106
.split('{{bg}}').join(bgColor)
105107
.split('{{board}}').join(svgElements.join(''))
106108
}
@@ -116,22 +118,22 @@ app.get('/:fen.jpeg', (req, res) => {
116118
text_color: textColor = TEXT_COLOR,
117119
cross_color: crossColor = CROSS_COLOR,
118120
marks_color: marksColor = MARKS_COLOR,
119-
square_size: squareSize = SQUARE_SIZE,
120121
b_cell_color: bCellColor = B_CELL_COLOR,
121122
w_cell_color: wCellColor = W_CELL_COLOR,
122-
board_padding: boardPadding = BOARD_PADDING,
123123
} = req.query
124124
const { fen = 'rnbkqbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBKQBNR' } = req.params
125125

126+
const boardPadding = boardSize / 26
127+
const squareSize = boardPadding * 3
128+
const scale = boardSize / 390
126129
const marks = marksList.split(',')
127130
const whiteBottom = !!Number(rotate)
128131

129132
res.contentType('image/jpeg')
130133

131-
console.log(fen)
132-
133134
const svg = renderSVG(Board.load(fen), {
134135
marks,
136+
scale,
135137
arrows,
136138
bgColor,
137139
marksSize,
@@ -152,6 +154,7 @@ app.get('/:fen.jpeg', (req, res) => {
152154

153155
loadSVGFromString(svg, (objects, info) => {
154156
const ctx = canvas.getContext('2d')
157+
console.log(info)
155158
const scaleX = info.width ? (boardSize / info.width) : 1
156159
const scaleY = info.height ? (boardSize / info.height) : 1
157160

src/svg.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ const makeSquare = ({ x, y, squareSize, squareId, color }) => `<rect
88
fill="#${color}"
99
/>`
1010

11-
const makePiece = ({ x, y, piece }) => `<use
11+
const makePiece = ({ x, y, piece, scale }) => `<use
1212
xlink:href="#${piece.side.name}-${piece.type}"
13-
transform="translate(${x}, ${y})"
13+
transform="translate(${x}, ${y}) scale(${scale})"
1414
/>`
1515

16-
const makeCross = ({ x, y, crossColor }) => `<g transform="translate(${x}, ${y})">
16+
const makeCross = ({ x, y, crossColor, scale }) => `<g
17+
transform="translate(${x}, ${y}) scale(${scale})">
1718
<path
1819
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"
1920
fill="#${crossColor}"
@@ -22,27 +23,27 @@ const makeCross = ({ x, y, crossColor }) => `<g transform="translate(${x}, ${y})
2223
/>
2324
</g>`
2425

25-
const makeDot = ({ x, y, squareSize, marksSize, marksColor }) => `<circle
26+
const makeDot = ({ x, y, squareSize, marksSize, marksColor, scale }) => `<circle
2627
cx="${x + squareSize / 2}"
2728
cy="${y + squareSize / 2}"
28-
r="${marksSize}"
29+
r="${marksSize * scale}"
2930
fill="#${marksColor}"
3031
/>`
3132

3233
const makeScale = ({ i, boardPadding, squareSize, textColor, file, rank }) => `<text
33-
transform="translate(${boardPadding + squareSize / 2 + i * squareSize - 3}, 10) scale(.65)"
34+
transform="translate(${boardPadding + squareSize / 2 + i * squareSize - 3}, 10) scale(.75)"
3435
fill="#${textColor}"
3536
>${file.toUpperCase()}</text>
3637
<text
37-
transform="translate(${squareSize + i * squareSize - 10}, ${squareSize * 8 + boardPadding * 2 - 3}) scale(.65)"
38+
transform="translate(${squareSize + i * squareSize - 10}, ${squareSize * 8 + boardPadding * 2 - 3}) scale(.75)"
3839
fill="#${textColor}"
3940
>${file.toUpperCase()}</text>
4041
<text
41-
transform="translate(4, ${squareSize + i * squareSize - 3}) scale(.7)"
42+
transform="translate(4, ${squareSize + i * squareSize - 3}) scale(.75)"
4243
fill="#${textColor}"
4344
>${rank}</text>
4445
<text
45-
transform="translate(${squareSize * 8 + boardPadding * 2 - 10}, ${squareSize + i * squareSize - 3}) scale(.7)"
46+
transform="translate(${squareSize * 8 + boardPadding * 2 - 10}, ${squareSize + i * squareSize - 3}) scale(.75)"
4647
fill="#${textColor}"
4748
>${rank}</text>`
4849

stubs/stub.svg

+14-2
Loading

0 commit comments

Comments
 (0)