-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
206 lines (143 loc) · 6.8 KB
/
canvas.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
const app = new PIXI.Application({ width: window.innerWidth - 20, height: window.innerHeight - 20 });
document.body.appendChild(app.view);
// to enable pixi-devtools
globalThis.__PIXI_APP__ = app;
// resizing the window
window.addEventListener('resize', function() {
app.stage.width = window.innerWidth - 20;
app.stage.height = window.innerHeight - 20;
})
// declaring some variables
const rows = 3, columns = 3;
const spriteMatrix = Array(rows).fill().map(() => Array(columns)); // creating empty (rows*columns) matrix
var sum = 0;
const sumArray = [];
var boxSize = 110;
// setting up background image
const bg = PIXI.Sprite.from('examples/assets/bg1.jpg');
bg.anchor.set(0.5);
bg.x = app.screen.width / 2;
bg.y = app.screen.height / 2;
app.stage.addChild(bg);
// initialising the matrix with sprits.
function giftMatrix() {
for (let i = 0; i < rows; i++) {
for (let j = 0; j < columns; j++) {
spriteMatrix[i][j] = PIXI.Sprite.from('examples/assets/closed-box.png');
}
}
}
// positioning gift boxes
function boxPositioning() {
for (let i = 0; i < rows; i++) {
for (let j = 0; j < columns; j++) {
spriteMatrix[i][j].width = boxSize;
spriteMatrix[i][j].height = boxSize;
spriteMatrix[i][j].anchor.set(0);
spriteMatrix[i][j].position.set(j * boxSize + app.screen.width/columns, i * boxSize + app.screen.height/(rows*rows/2));
app.stage.addChild(spriteMatrix[i][j]);
spriteMatrix[i][j].interactive = true;
spriteMatrix[i][j].cursor = 'pointer';
spriteMatrix[i][j].on('pointertap', () => {
var number = Math.floor(Math.random() * 100);
var boxNumber = displayNumbers(i, j, number);
// adding no. to display sum
sum += number;
sumArray.push(sum);
var finalXPositionOfNumber = window.statusMessage.x - window.statusMessage.width; // x position for sum (message).
var finalYPositionOfNumber = window.statusMessage.y - window.statusMessage.height; // y position for sum (message).
console.log("^^^^^^^^^^^^^^^^^^^^", finalXPositionOfNumber, finalYPositionOfNumber);
// initially set alpha to 0
boxNumber.alpha = 0;
const ticker = PIXI.Ticker.shared;
// Add a variable to count up the seconds our application has been running
let elapsedTime = 0.0;
var initialXPositionOfNumber = boxNumber.x;
var initialYPositionOfNumber = boxNumber.y;
var differenceOfXaxis = (finalXPositionOfNumber - initialXPositionOfNumber);
var differenceOfYaxis = (finalYPositionOfNumber - initialYPositionOfNumber);
// m = (y2 - y1) / (x2 - x1)
let slope = differenceOfYaxis / differenceOfXaxis;
// declare a ticker listener
const onTick = () => {
// updates the alpha value by little
boxNumber.alpha += 0.02;
spriteMatrix[i][j].alpha -= 0.02;
let maximumTimeToReachSum = 2000; // total time in mili-seconds
// remove this listener once alpha reaches 1 or 0
if (boxNumber.alpha > 1 && spriteMatrix[i][j].alpha < 0) {
// Add the time to our total elapsed time
elapsedTime += ticker.elapsedMS; // time in mili-seconds
// draging no. to sum's position
if (elapsedTime <= maximumTimeToReachSum) {
console.log("&&&&&&&&&&&&&&&&&&&&&&", elapsedTime);
let speed = differenceOfXaxis/maximumTimeToReachSum;
// distance = (speed * time)
let x = (speed * elapsedTime);
console.log("(((((((((((((((((", initialXPositionOfNumber, initialYPositionOfNumber);
// y = m * x
let y = (slope * x);
boxNumber.y = y + initialYPositionOfNumber;
boxNumber.x = x + initialXPositionOfNumber;
// decreasing size of text litle by litle
boxNumber.width -= 0.4;
boxNumber.height -= 0.4;
console.log(boxNumber.x, boxNumber.y, finalXPositionOfNumber, finalYPositionOfNumber);
}
else {
console.log('*****************');
console.log(boxNumber.x, boxNumber.y, finalXPositionOfNumber, finalYPositionOfNumber);
console.log('*****************');
ticker.remove(onTick);
boxNumber.alpha = 0;
app.stage.removeChild(window.statusMessage); // to remove previous total value
displayMessage(sum, window.statusMessage.x, window.statusMessage.y, 40, 1);
}
// setting up time leap for winning message
setTimeout(winingMessage, maximumTimeToReachSum);
}
}
// register the ticker listener
ticker.add(onTick)
});
}
}
}
// function for winning message display
function winingMessage() {
if (sumArray.length == (rows*columns)) {
let final_Message = 'You Won ' + sumArray[rows*columns-1] + ' Points !!';
displayMessage(final_Message, app.screen.width / 2, app.screen.height / 2, 100, 0.5);
}
}
// display no. from boxes
function displayNumbers(a, b, num) {
const numberText = new PIXI.Text(num, {
fontFamily: 'Cambria',
fontSize: 100,
fill: 'red',
});
numberText.y = spriteMatrix[a][b].y;
numberText.x = spriteMatrix[a][b].x;
numberText.anchor.set(0);
app.stage.addChild(numberText);
return numberText;
}
// display message on stage
function displayMessage(message, xPos, yPos, font, Anchor) {
window.statusMessage = new PIXI.Text(message, {
fontFamily: 'Algerian',
fontSize: font,
fontWeight: 'bold',
fill: 'yellow',
stroke: '#004620',
strokeThickness: 12,
});
window.statusMessage.y = yPos;
window.statusMessage.x = xPos;
window.statusMessage.anchor.set(Anchor);
app.stage.addChild(window.statusMessage);
}
displayMessage(sum, app.screen.width, app.screen.height, 40, 1);
giftMatrix();
boxPositioning();