-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
901 lines (744 loc) · 23.7 KB
/
index.html
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
<!DOCTYPE html>
<html>
<head>
<title>Pente</title>
<link rel="shortcut icon" href="img/black_piece_trapped.png" type="image/png">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/wgo.js"></script>
<style>
body {
margin-top:30px;
background-color: #ABB2B9;
}
footer {
position:fixed;
bottom:0;
text-align:center;
width:100%;
}
canvas {
border: 1px solid black;
}
#lobbyArea {
display: none;
}
#gameArea {
display: none;
}
#chatArea {
display: none;
}
#gameContainer {
/*not sure why there is padding
automatically added but this fixes it*/
padding-right: 0;
padding-left: 0;
}
#gameMessage {
margin-top:0px;
margin-bottom:15px;
}
#opponentLeftMessage {
margin: 0px;
margin-bottom:15px;
}
#board {
display:none;
}
#gameOverPanel {
display: none;
position:absolute;
z-index:1000;
margin-bottom:0px;
}
#availableRematch {
padding: 20px;
}
#opponentLeft {
padding:20px;
}
#chat {
overflow-y: scroll;
width: 100%;
border: 1px solid black;
}
#white-trapped {
float:left;
}
#black-trapped {
float:right;
}
#playerPiece {
float:right;
}
#opponentPiece {
float:right;
}
#playerUsername {
display:inline-block;
margin-bottom: 0px;
}
#opponentUsername {
display:inline-block;
margin-bottom: 0px;
}
#trapped-pieces {
width:100%;
border-radius:50px;
border:1px solid black;
background-color:grey;
display:none;
margin-top:15px;
}
#boardPlaceholder {
border: 1px solid black;
}
#rematch {
width:100%;
}
#newmatch {
width:100%;
}
#confetti {
z-index:800;
position:absolute;
border:none;
display:none;
}
.selected {
font-weight: bold;
}
.chat-bubble-sent {
margin: 3px 3px 3px 3px;
border-radius: 10px;
}
.chat-bubble-received {
margin: 3px 3px 3px 3px;
border-radius: 10px;
background-color: #A9CCE3;
}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
.no-bottom {
margin-bottom:0px;
}
</style>
</head>
<body>
<div class="container">
<!-- game board -->
<div id="gameContainer" class="col-md-7">
<div>
<img id="boardPlaceholder" />
<div id="board">
<canvas id="confetti"></canvas>
</div>
<!-- game messages -->
<div id="gameOverPanel">
<div class="panel panel-default no-bottom">
<div class="panel-body" id="availableRematch">
<h1 id="gameMessage"></h1>
<form id="rematchForm">
<button type="submit" id="rematch" class="btn btn-primary">Rematch</button>
</form>
</div>
<div class="panel-body" id="requestingRematch">
<p class="text-center">Requesting rematch</p>
<center><img src="img/ajax-loader.gif" /></center>
</div>
<div class="panel-body" id="opponentLeft">
<h1 id="opponentLeftMessage"></h1>
<form id="findNewMatchForm">
<button type="submit" id="newmatch" class="btn btn-primary">Find a new match</button>
</form>
</div>
<div class="panel-body" id="findingNewMatch">
<p class="text-center">Finding new match</p>
<center><img src="img/ajax-loader.gif" /></center>
</div>
</div>
</div>
</div>
<div id="trapped-pieces" class="clearfix">
<div id="white-trapped" class="panel-body" align="left">
<img id="piecew1" src="img/white_piece_untrapped.png" class="trapped-piece" />
<img id="piecew2" src="img/white_piece_untrapped.png" class="trapped-piece" />
<img id="piecew3" src="img/white_piece_untrapped.png" class="trapped-piece" />
<img id="piecew4" src="img/white_piece_untrapped.png" class="trapped-piece" />
<img id="piecew5" src="img/white_piece_untrapped.png" class="trapped-piece" />
</div>
<div id="black-trapped" class="panel-body" align="right">
<img id="pieceb5" src="img/black_piece_untrapped.png" class="trapped-piece" />
<img id="pieceb4" src="img/black_piece_untrapped.png" class="trapped-piece" />
<img id="pieceb3" src="img/black_piece_untrapped.png" class="trapped-piece" />
<img id="pieceb2" src="img/black_piece_untrapped.png" class="trapped-piece" />
<img id="pieceb1" src="img/black_piece_untrapped.png" class="trapped-piece" />
</div>
</div>
</div>
<!-- sidebar -->
<div id="sidebar" class="col-md-3">
<div id="loginArea" class="panel panel-default">
<div class="panel-body">
<form id="loginForm">
<div class="input-group">
<input class="form-control" id="username" placeholder="Enter player name" />
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">Play</button>
</span>
</div>
</form>
</div>
</div>
<!-- lobby panel, has a loading gif -->
<div id="lobbyArea">
<div class="panel panel-default">
<div class="panel-body">
<p class="text-center">Waiting for opponent to join</p>
<center><img src="img/ajax-loader.gif" /></center>
</div>
</div>
</div>
<!-- shows player names and chat window -->
<div id="gameArea">
<!-- displays player names -->
<div id="playerNames" class="panel panel-default">
<div class="panel-body">
<ul class="list-group no-bottom">
<li id="player" class="list-group-item">
<p id="playerUsername"></p>
<img id="playerPiece" />
</li>
<li id="opponent" class="list-group-item">
<p id="opponentUsername"></p>
<img id="opponentPiece" />
</li>
</ul>
</div>
</div>
<!-- chat window -->
<div id="chatWindow" class="panel panel-default">
<div class="panel-heading">
<label class="no-bottom">Chat</label>
</div>
<div class="panel-body">
<div id="chat"></div>
<form id="messageForm">
<div class="form-group no-bottom">
<br />
<div class="input-group">
<input class="form-control" id="message" placeholder="Enter message" />
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">Enter</button>
</span>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
$(function() {
var socket = io.connect();
var $findNewMatchForm = $('#findNewMatchForm');
var $messageForm = $('#messageForm');
var $rematchForm = $('#rematchForm');
var $lobbyArea = $('#lobbyArea');
var $loginArea = $('#loginArea');
var $loginForm = $('#loginForm');
var $gameArea = $('#gameArea');
var $username = $('#username');
var username; // stores the current player's username
var opponent; // stores the username of the opponent
var pieceColor; // stores if current user is white or black
var gameBoard; // rendered canvas that the user sees
var position; // stores game state of each cell
var currentTurn;// true if it is the current player's turn, false otherwise
var gameOver; // true if the game is over
var size; // number of cells on the board
var boardRendered = false; // if the game board has already been rendered
/* Variables for confetti */
var particles;
var mp;
var ctx;
var angle;
var W;
var H;
var confetti = false;
window.onload = function() {
setupBoardPlaceholder();
setupTrappedPieces();
}
$loginForm.submit(function(e) {
e.preventDefault();
loginUser();
});
$messageForm.submit(function(e) {
e.preventDefault();
sendMessage();
});
$rematchForm.submit(function(e) {
e.preventDefault();
requestRematch();
});
$findNewMatchForm.submit(function (e) {
e.preventDefault();
findNewMatch();
});
// Called when a message is received from the opponent
socket.on('new message', function(data) {
addMessageToChatWindow(data, false);
});
// Got paired with another player in the lobby
socket.on('found match', function(data) {
// Sets opponent username
opponent = data.msg;
startGame(data.start);
});
// Called when the opponent leaves the game
socket.on('opponent left', function(data) {
// TODO notify the player
opponentLeft();
});
// Called when a piece has been placed down (by this user or the opponent)
// and the server has validated this move
socket.on('piece placed', function(data) {
placePiece(data.x, data.y, data.color);
});
// Called when someone wins the game
socket.on('game over', function(data) {
endGame(data.won);
});
// Called when pieces have been trapped
socket.on('trapped pieces', function(data) {
removeTrappedPair(data.x1, data.y1, data.x2, data.y2, data.color, data.count);
});
// Called when both players agree to a rematch
socket.on('starting rematch', function(data) {
startRematch(data.start);
});
// When an opponent has left, and the user clicks to
// rejoin the matchmaking process
function findNewMatch() {
// Rejoins the lobby
socket.emit('join lobby', username);
// Shows a loading button
$('#opponentLeft').css('display', 'none');
$('#findingNewMatch').css('display', 'block');
centerGameOverPanel();
}
function opponentLeft() {
clearPlayerHighlighting();
showOpponentLeftPanel();
gameOver = true;
}
function showOpponentLeftPanel() {
$('#gameOverPanel').css('display', 'block');
$('#availableRematch').css('display', 'none');
$('#requestingRematch').css('display', 'none');
$('#findingNewMatch').css('display', 'none');
$('#opponentLeft').css('display', 'block');
$('#opponentLeftMessage').empty();
$('#opponentLeftMessage').append(opponent + ' has left the game.');
centerGameOverPanel();
}
function clearChatHistory() {
$('#chat').empty();
}
// Starts a rematch, clears the board
function startRematch(firstMove) {
// clear rematch panel
clearRematchPanel();
// clear the board
clearGameBoard();
// clear the trapped pieces
resetTrappedPieces();
// adjust what color each person is
formatPlayerTurn(firstMove);
// stops the confetti on the screen
clearConfetti();
gameOver = false;
}
function clearConfetti() {
confetti = false;
$('#confetti').css('display', 'none');
}
function clearRematchPanel() {
$('#gameOverPanel').css('display', 'none');
}
// Clears the game state and the rendered board
function clearGameBoard() {
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
// Remove the piece graphically
gameBoard.removeObjectsAt(i, j);
// Remove the pieces from the local copy
position.set(i, j, 0);
}
}
}
// Resets all the trapped pieces to untrapped
function resetTrappedPieces() {
var white_id_base = "#piecew";
var black_id_base = "#pieceb";
for (let i = 1; i <= 5; i++) {
$(white_id_base + i).attr('src', 'img/white_piece_untrapped.png');
$(black_id_base + i).attr('src', 'img/black_piece_untrapped.png');
}
}
// Called when the user presses the rematch button after a game ends
function requestRematch() {
// Changes the rematch panel from showing the button
// to showing a 'requesting rematch' with a loading gif
$('#availableRematch').css('display', 'none');
$('#requestingRematch').css('display', 'block');
// Centers the panel
centerGameOverPanel();
// Notifies the server of the rematch request
socket.emit('request rematch');
}
// Adds the user to the lobby with the specified username
function loginUser() {
// push username to server
username = $username.val();
if (username === '') username = 'Anonymous';
socket.emit('join lobby', username);
$username.val('');
// move from login screen to game screen
$loginArea.css('display', 'none');
$lobbyArea.css('display', 'block');
}
// Sends whatever was in the user's chat input
function sendMessage() {
// Grabs the message
var message = $('#message').val();
// Formats the message and adds it to the chat window
addMessageToChatWindow(message, true);
// Sends the message to the opponent
socket.emit('send message', message);
// Clears the message input
$('#message').val('');
}
// Adds a placeholder image for the board so the
// pente lines don't appear until the game starts
function setupBoardPlaceholder() {
var $placeholder = $('#boardPlaceholder');
var width = $placeholder.parent().width();
$placeholder.width(width);
$placeholder.height(width);
// $placeholder.attr('src', 'img/wood_1024.jpg');
$placeholder.attr('src', 'img/logo.jpg');
}
// Sizes the trapped pieces at the bottom of the game area
function setupTrappedPieces() {
$('.trapped-piece').height(0.05 * $('#boardPlaceholder').height());
}
// Formats the message depending on if it was sent or received
// and adds it to the chat window
function addMessageToChatWindow(message, sent) {
// Grabs the username and how to style the bubble
// depending on who sent it
var user = sent ? username : opponent;
var styleClass = sent ? 'chat-bubble-sent' : 'chat-bubble-received';
// Bolds the username but not the message
var formattedMessage = '<b>' + user + ':</b> ' + message;
// Adds the message into a well div
var chatBubble = '<div class="well well-sm ' + styleClass + '">' + formattedMessage + '</div>';
// Adds the well into the chat div and scrolls down
$('#chat').append(chatBubble);
updateScroll();
}
function placePiece(x, y, color) {
// Updates the rendered canvas with a piece
gameBoard.addObject({
x: x,
y: y,
c: color
});
// Updates the game state
position.set(x, y, color);
// Switches the player turn
switchPlayerTurn();
}
// Removes two pieces from the board and
// increments the trapped count
function removeTrappedPair(x1, y1, x2, y2, color, count) {
// Remove the rendered pieces
gameBoard.removeObjectsAt(x1, y1);
gameBoard.removeObjectsAt(x2, y2);
// Remove the pieces from the local copy
position.set(x1, y1, 0);
position.set(x2, y2, 0);
// shows the new trapped piece at the bottom
var imageString = color == WGo.B ? 'img/black_piece_trapped.png' : 'img/white_piece_trapped.png';
var colorString = color == WGo.B ? 'b' : 'w';
var countString = count;
var fullId = '#piece' + colorString + countString;
$(fullId).attr('src', imageString);
}
// Starts the game and formats elements based on
// whether or not the user starts
function startGame(firstMove) {
// Add player usernames to sidebar
$('#playerUsername').empty();
$('#playerUsername').append(username);
$('#opponentUsername').empty();
$('#opponentUsername').append(opponent);
// Switch to the game area page
$('#gameOverPanel').css('display', 'none');
$lobbyArea.css('display', 'none');
$gameArea.css('display', 'block');
// Formats elements based on whos turn it is
formatPlayerTurn(firstMove);
// game board has already been rendered,
// lets not make it again
if (boardRendered) {
// clear the board
clearGameBoard();
// clear the trapped pieces
resetTrappedPieces();
} else {
// Render game board
renderGameBoard();
}
clearChatHistory();
// Sets the height of the chat window relative to the board size
$('#chat').height($('#board').height() * 0.4);
// Starts the game
gameOver = false;
}
// Formats player elements based on if they start or not
function formatPlayerTurn(firstMove) {
// Current player makes first move
if (firstMove) {
pieceColor = WGo.W;
currentTurn = true;
$('#player').toggleClass('selected');
$('#player').toggleClass('list-group-item-success');
$('#playerPiece').attr('src', 'img/white_piece_trapped.png');
$('#opponentPiece').attr('src', 'img/black_piece_trapped.png');
} else {
pieceColor = WGo.B;
currentTurn = false;
$('#opponent').toggleClass('selected');
$('#opponent').toggleClass('list-group-item-success');
$('#opponentPiece').attr('src', 'img/white_piece_trapped.png');
$('#playerPiece').attr('src', 'img/black_piece_trapped.png');
}
// Sets the sizes of the player color indicators
$('#playerPiece').height($('#playerPiece').parent().height());
$('#opponentPiece').height($('#opponentPiece').parent().height());
}
// Ends the game and formats elements based on
// whether or not the user won
function endGame(winner) {
// Clears player turn highlighting
clearPlayerHighlighting();
// Appends the results of the game
showGameOverPanel(winner);
gameOver = true;
}
function showGameOverPanel(winner) {
$('#gameMessage').empty();
if (winner) {
$('#gameMessage').append('You win!!!');
startConfetti();
} else {
$('#gameMessage').append('You lose :\'(');
}
// Makes the rematch panel visible
$('#gameOverPanel').css('display', 'block');
$('#opponentLeft').css('display', 'none');
$('#availableRematch').css('display', 'block');
$('#requestingRematch').css('display', 'none');
$('#findingNewMatch').css('display', 'none');
// Centers the panel on top of the board
centerGameOverPanel();
}
function clearPlayerHighlighting() {
if (currentTurn) {
$('#player').toggleClass('list-group-item-success').toggleClass('selected');
} else {
$('#opponent').toggleClass('list-group-item-success').toggleClass('selected');
}
}
function centerGameOverPanel() {
// Not necessarily an elegant solution but it works
var $panel = $('#gameOverPanel');
var $parent = $panel.parent();
var top = $parent.height() / 2 - $panel.height() / 2;
var left = $parent.width() / 2 - $panel.width() / 2;
$panel.css({
top: top+'px',
left: left+'px'
});
}
function switchPlayerTurn() {
currentTurn = !currentTurn;
// Switches between selected player and green background
$('#player').toggleClass('list-group-item-success').toggleClass('selected');
$('#opponent').toggleClass('list-group-item-success').toggleClass('selected');
}
// Initializes the game board
function renderGameBoard() {
// Removes the board placeholder
$('#boardPlaceholder').css('display', 'none');
$('#board').css('display', 'block');
$('#trapped-pieces').css('display', 'block');
// Fills the parent bootstrap column
var width = $('#board').parent().width();
size = 19;
// Initializes board rendering element
gameBoard = new WGo.Board(document.getElementById('board'), {
width: width,
size: size,
});
// Initializes game state
position = new WGo.Position(size);
// Handles click methods
gameBoard.addEventListener("click", function(x, y) {
// Verifies that the click is valid
if (!gameOver && currentTurn && isEmpty(x,y)) {
socket.emit('place piece', {x: x, y: y, color: pieceColor});
}
});
boardRendered = true;
}
// Gets the contents in the (x,y) position on the board
// Returns either 0 (empty), WGo.W, or WGo.B
function getCell(x, y) {
return position.get(x,y);
}
// Returns true if the cell at (x,y) position is empty
// false otherwise
function isEmpty(x, y) {
return getCell(x,y) == 0;
}
// Scrolls the chat window down
function updateScroll() {
$('#chat').animate({ scrollTop: $('#chat').prop("scrollHeight")}, 750);
}
// Kicks off the confetti making
// All confetti code (most of startConfetti(), all of draw() and update()) was taken from
// https://stackoverflow.com/questions/16322869/trying-to-create-a-confetti-effect-in-html5-how-do-i-get-a-different-fill-color
function startConfetti() {
W = $('#board').width();
H = $('#board').height();
// Makes the canvas visible and covers the whole board
$('#confetti').css('display', 'block');
$('#confetti').height(H);
$('#confetti').width(W);
// Grabs the context
var canvas = document.getElementById('confetti');
ctx = canvas.getContext('2d');
confetti = true;
// Initialize variables for snowflake particles
mp = 300; // max particles
particles = [];
for (var i = 0; i < mp; i++) {
particles.push({
x: Math.random() * W, //x-coordinate
y: Math.random() * H, //y-coordinate
r: Math.random() * 15 + 1, //radius
d: Math.random() * mp, //density
color: "rgba(" + Math.floor((Math.random() * 255)) + ", " + Math.floor((Math.random() * 255)) + ", " + Math.floor((Math.random() * 255)) + ", 0.8)",
tilt: Math.floor(Math.random() * 5) - 5
});
}
angle = 0;
draw();
}
//Lets draw the flakes
function draw() {
ctx.clearRect(0, 0, W, H);
for (var i = 0; i < mp; i++) {
var p = particles[i];
ctx.beginPath();
ctx.lineWidth = p.r;
ctx.strokeStyle = p.color; // Green path
ctx.moveTo(p.x, p.y);
ctx.lineTo(p.x + p.tilt + p.r / 2, p.y + p.tilt);
ctx.stroke(); // Draw it
}
update();
}
function update() {
angle += 0.01;
for (var i = 0; i < mp; i++) {
var p = particles[i];
//Updating X and Y coordinates
//We will add 1 to the cos function to prevent negative values which will lead flakes to move upwards
//Every particle has its own density which can be used to make the downward movement different for each flake
//Lets make it more random by adding in the radius
p.y += (Math.cos(angle + p.d) + 1 + p.r / 2) * 0.65;
p.x += Math.sin(angle) * 2;
//Sending flakes back from the top when it exits
//Lets make it a bit more organic and let flakes enter from the left and right also.
if (p.x > W + 5 || p.x < -5 || p.y > H) {
if (i % 3 > 0) //66.67% of the flakes
{
particles[i] = {
x: Math.random() * W,
y: -10,
r: p.r,
d: p.d,
color: p.color,
tilt: p.tilt
};
} else {
//If the flake is exitting from the right
if (Math.sin(angle) > 0) {
//Enter from the left
particles[i] = {
x: -5,
y: Math.random() * H,
r: p.r,
d: p.d,
color: p.color,
tilt: p.tilt
};
} else {
//Enter from the right
particles[i] = {
x: W + 5,
y: Math.random() * H,
r: p.r,
d: p.d,
color: p.color,
tilt: p.tilt
};
}
}
}
}
// Continue calling so long as we want it
if (confetti) {
setTimeout(draw, 20);
}
}
});
</script>
</body>
<footer>
<p>Made by Daniel Gil. Find the source code <a href="https://github.com/dpgil/pente">here</a></p>
</footer>
</html>