-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsvg.chessboard.js
547 lines (436 loc) · 15.2 KB
/
svg.chessboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/**
* SvgChessBoard - v0.0.1 - 03/08/2016
* {homepage}
* Copyright (c) 2016 Philip Sylvain
* Licensed MIT
*/
function SvgChessBoard(id, options)
{
this.options = {
coords : false,
margin : 30,
themePath : '/themes',
squareStyle : 'aluminium',
pieceStyle : 'merida',
comment : {
defaultColor : '#00ff00',
altColor : '#ffff00',
shiftColor : '#FF0000'
}
};
if (options) {
for (var key in options) {
if (this.options[key]) {
this.options[key] = options[key];
}
}
}
this.coordinates = [
'a8','b8','c8','d8','e8','f8','g8','h8',
'a7','b7','c7','d7','e7','f7','g7','h7',
'a6','b6','c6','d6','e6','f6','g6','h6',
'a5','b5','c5','d5','e5','f5','g5','h5',
'a4','b4','c4','d4','e4','f4','g4','h4',
'a3','b3','c3','d3','e3','f3','g3','h3',
'a2','b2','c2','d2','e2','f2','g2','h2',
'a1','b1','c1','d1','e1','f1','g1','h1'
];
this.angle = 0;
this.boardSize = 1600;
this.squareSize = this.boardSize / 8;
this.pieceSize = 132;
var viewBoxSize = this.boardSize + (2 * this.options.margin);
this.draw = SVG(id);
this.draw.viewbox(0, 0, viewBoxSize, viewBoxSize);
var that = this;
var boardGroup = this.draw.group();
boardGroup.id('board-group');
// White square pattern
var wSquarePattern = this.draw.pattern(this.squareSize, this.squareSize, function(add) {
var image = add.image(that.options.themePath + '/board/'+ that.options.squareStyle +'.png', 200, 400);
image.attr({x: 0, y: 0});
});
wSquarePattern.attr({
'class' : 'white square-pattern',
x : this.options.margin,
y : this.options.margin
});
// Black square pattern
var bSquarePattern = this.draw.pattern(this.squareSize, this.squareSize, function(add) {
var image = add.image(that.options.themePath +'/board/'+ that.options.squareStyle +'.png', 200, 400);
image.attr({x: 0, y: -200});
});
bSquarePattern.attr({
'class' : 'black square-pattern',
x : this.options.margin,
y : this.options.margin
});
// Generate Squares
for (var i = 0, col = 0, x = this.options.margin, y = this.options.margin, fill = 0; i < this.coordinates.length; i++) {
var square = this.draw.rect(this.squareSize, this.squareSize);
square.attr({
'x' : x,
'y' : y,
'fill' : (fill) ? bSquarePattern : wSquarePattern,
'class' : 'drop square ' + (fill ? 'black' : 'white'),
'id' : this.coordinates[i],
'pointer-events' : 'all'
});
square.remember('square', this.coordinates[i]);
var color = this.draw.rect(this.squareSize, this.squareSize);
color.attr({
'x' : x,
'y' : y,
'fill' : 'transparent',
'class' : 'color',
'pointer-events' : 'none'
});
square.on('mousedown', function(e) {
if (e.ctrlKey) {
this.doc().remember('commentFrom', this.remember('square'));
}
});
square.on('mouseup', function(e) {
if (e.ctrlKey) {
this.doc().fire('comment', {
event: e,
from : this.doc().remember('commentFrom'),
to : this.remember('square')
});
}
});
var squareGroup = this.draw.group();
boardGroup.add(squareGroup);
squareGroup.attr({
'id' : 'g-' + this.coordinates[i],
});
squareGroup.add(square);
squareGroup.add(color);
col++;
x += this.squareSize;
fill = ~fill;
if (col > 7) {
col = 0;
x = this.options.margin;
y += this.squareSize;
fill = ~fill;
}
}
// Pieces pattern
var imageOptions = {
'href' : this.options.themePath +'/pieces/'+ this.options.pieceStyle +'.png',
x : 0,
y : 0,
width : this.pieceSize * 6,
height : this.pieceSize * 2
};
// Pieces disposition in PNG theme
var pieces = ['Rook', 'Knight', 'Bishop', 'Queen', 'King', 'Pawn'];
var side='w';
var addImage = function(add) {
var image = add.image(imageOptions.href, imageOptions.width, imageOptions.height);
image.attr({x: imageOptions.x, y: imageOptions.y});
};
for (i=0; i < pieces.length ; i++) {
var pattern = this.draw.pattern(this.pieceSize, this.pieceSize, addImage);
pattern.id(side+pieces[i]+'Pattern');
pattern.addClass('piece-pattern');
var symbol = this.draw.symbol();
symbol.id(side+pieces[i]);
pattern.addClass('piece-symbol');
symbol.viewbox(0, 0, this.pieceSize, this.pieceSize);
symbol.rect(this.pieceSize, this.pieceSize).fill(pattern);
if (side == 'w') {
i--;
side = 'b';
imageOptions.y -= this.pieceSize;
} else {
side = 'w';
imageOptions.y = 0;
imageOptions.x -= this.pieceSize;
}
}
// Generate coordinates
var coordsGroup = this.draw.group();
boardGroup.add(coordsGroup);
coordsGroup.id('coordinates');
var coords = ['A','B','C','D','E','F','G','H'];
var middleMargin = this.options.margin / 2;
for (i=0, c=8, x=this.options.margin+(this.squareSize/2), y=this.options.margin+(this.squareSize/2); i < 8; i++) {
var row = this.draw.element('text').words(coords[i]).attr({
'class' : 'coords',
'x' : x,
'y' : middleMargin,
'dominant-baseline' : 'middle'
});
var column = this.draw.element('text').words(c + '').attr({
'class' : 'coords',
'x' : middleMargin,
'y' : y,
'text-anchor' : 'middle'
});
var row2 = row.clone().y(this.boardSize + this.options.margin + middleMargin);
var column2 = column.clone().x(this.boardSize + this.options.margin + middleMargin);
coordsGroup.add(row);
coordsGroup.add(column);
coordsGroup.add(row2);
coordsGroup.add(column2);
x += this.squareSize;
y += this.squareSize;
c--;
}
this.draw.on('comment', function(e) {
var from = e.detail.from,
to = e.detail.to,
event = e.detail.event,
color = that.options.comment.defaultColor;
if (event.altKey)
{
color = that.options.comment.altColor;
}
else if (event.shiftKey)
{
color = that.options.comment.shiftColor;
}
if (from == to) {
that.toggleSquareColor(from, color);
}
else {
that.drawArrow(from, to, color);
}
});
}
SvgChessBoard.prototype = {
addPiece : function(type, square)
{
var boardGroup = SVG.get('board-group');
var squareBg = SVG.get(square);
var symbol = SVG.get(type);
var piece = this.draw.use(symbol).attr({
'x' : squareBg.x(),
'y' : squareBg.y(),
'width' : squareBg.width(),
'height' : squareBg.height(),
'class' : 'drop piece'
});
boardGroup.add(piece);
piece.remember('pieceType', type);
piece.remember('square', square);
piece.draggable();
piece.on('beforedrag', function(e) {
if (e.detail.event.ctrlKey) {
// Start a comment if Ctrl key is down
this.doc().remember('commentFrom', this.remember('square'));
}
// Turn off all pointer events to the dragged element, this does 2
// things:
// 1) allows us to drag text elements without selecting the text
// 2) allows us to find out where the dragged element is dropped
this.attr('pointer-events', 'none');
// Move this element to the "top" of the display
this.front();
});
piece.on('dragmove', function(e){
if (e.detail.event.ctrlKey) {
// Don't move if Ctrl key is down
e.preventDefault();
}
});
piece.on('dragend', function(e) {
// Restore pointer-events
this.attr('pointer-events', null);
// Since the element currently being dragged has its pointer-events turned off,
// we are afforded the opportunity to find out the element it's being dropped on
var targetElement = e.detail.event.target.instance;
var squareTo,
squareFrom = this.remember('square'),
canDrop = false;
if (targetElement.hasClass('drop')) {
canDrop = true;
squareTo = targetElement.remember('square');
}
// Don't move if Ctrl key is down
if (e.detail.event.ctrlKey) {
if (targetElement.hasClass('piece')) {
this.doc().fire('comment', {
event: e,
from : this.doc().remember('commentFrom'),
to : squareTo
});
}
return;
}
var eventDetail = {
event: e,
piece: this,
candrop: canDrop,
move: {
piece: this.remember('pieceType'),
from: squareFrom,
to: squareTo
},
};
var event = new CustomEvent('beforedrop', {
detail: eventDetail,
cancelable: true
});
this.doc().fire(event);
if (event.detail.candrop) {
this.x(targetElement.x());
this.y(targetElement.y());
if (targetElement.hasClass('piece')) {
targetElement.remove();
}
this.remember('square', squareTo);
this.doc().fire('afterdrop', eventDetail);
} else {
// The piece go back to its start points
this.x(e.detail.handler.startPoints.box.x);
this.y(e.detail.handler.startPoints.box.y);
}
});
},
removePiece : function(square)
{
var pieces = this.draw.select('.piece');
pieces.each(function(i) {
if (this.remember('square') == square) {
this.remove();
}
});
},
movePiece : function(from, to)
{
if (from == to) {
return;
}
var pieces = this.draw.select('.piece');
var pieceToMove = null;
var targetElement = null;
pieces.each(function(i) {
if (this.remember('square') == from) {
pieceToMove = this;
}
if (this.remember('square') == to) {
targetElement = this;
}
});
if (!targetElement) {
// Square
targetElement = SVG.get(to);
}
if (pieceToMove && targetElement) {
pieceToMove.remember('square', to);
pieceToMove.x(targetElement.x());
pieceToMove.y(targetElement.y());
if (targetElement.hasClass('piece')) {
targetElement.remove();
}
}
},
empty : function()
{
},
toggleSquareColor : function(square, color, opacity)
{
if (!opacity) opacity = 0.5;
var squareElt = SVG.get('g-' + square).select('.color').first();
if (squareElt.attr('fill') == 'transparent') {
squareElt.attr({
'fill': color,
'opacity': opacity
});
} else {
squareElt.attr({
'fill': 'transparent',
'opacity': null
});
}
},
drawArrow : function(fromSquare, toSquare, color, opacity)
{
var lineId = 'arrow-'+fromSquare+toSquare;
if (SVG.get(lineId)) {
SVG.get(lineId).remove();
return;
}
if (!opacity) opacity = 0.5;
var from = {
x : SVG.get(fromSquare).x() + (this.squareSize / 2),
y : SVG.get(fromSquare).y() + (this.squareSize / 2)
};
var to = {
x : SVG.get(toSquare).x() + (this.squareSize / 2),
y : SVG.get(toSquare).y() + (this.squareSize / 2)
};
var boardGroup = SVG.get('board-group');
var line = this.draw.line(from.x, from.y, to.x, to.y).stroke({ width: 15, color: color, opacity: opacity });
boardGroup.add(line);
line.id(lineId);
line.attr({
'class': 'arrow',
'pointer-events': 'none'
});
line.marker('end', 4, 4, function(add) {
add.polygon('0,0 0,4 4,2').fill(color).opacity(opacity + 0.2);
this.ref(0,2);
});
},
rotate : function()
{
this.angle = (this.angle === 0)? 180 : 0;
var that = this;
var boardGroup = SVG.get('board-group');
// Rotate main group
boardGroup.rotate(this.angle);
// Rotate coordinates
var coords = SVG.get('coordinates').select('text.coords');
coords.each(function(i) {
this.rotate(that.angle);
});
// Rotate piece symbols because use element remove transformations after deplacement
var symbols = this.draw.defs().select('.piece-symbol');
symbols.each(function(i) {
this.rotate(that.angle);
});
},
setSquareStyle : function(style)
{
this.options.squareStyle = style;
var that = this;
var setPattern = this.draw.defs().select('pattern.square-pattern');
var wSquarePattern = this.draw.defs().select('pattern.white.square-pattern').first();
var bSquarePattern = this.draw.defs().select('pattern.black.square-pattern').first();
setPattern.each(function(i) {
this.first().load(that.options.themePath + '/board/'+ that.options.squareStyle +'.png');
});
var wSquares = SVG.get('board-group').select('.square');
wSquares.each(function(i) {
if (this.hasClass('white'))
this.fill(wSquarePattern);
else
this.fill(bSquarePattern);
});
},
setSquaresColor : function(wColor, bColor)
{
var squares = SVG.get('board-group').select('.square');
squares.each(function(i) {
if (wColor && this.hasClass('white')) this.fill(wColor);
if (bColor && this.hasClass('black')) this.fill(bColor);
});
},
setPieceStyle : function(style)
{
this.options.pieceStyle = style;
var that = this;
var setPattern = this.draw.defs().select('pattern.piece-pattern');
setPattern.each(function(i) {
this.first().load(that.options.themePath +'/pieces/'+ that.options.pieceStyle +'.png');
});
},
on : function(eventName, eventListener)
{
this.draw.on(eventName, eventListener);
}
};