generated from ecumene/gc-bootstrap-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (164 loc) · 5.36 KB
/
index.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
// fish selector
const selectFish = document.getElementById('fishes');
const makeFish = (fish) => ({ ...fish, flip: false });
const fishList = [
{value: "fish1", name: "Golden Gourami", img: 'img/gold-gourami.png', speed: 0.7},
{value: "fish2", name: "Pearl Gourami", img: 'img/pearl-gourami.png', speed: 0.8},
{value: "fish3", name: "Neon Tetra", img: 'img/neon-tetra.png', speed: 1.5},
{value: "fish4", name: "Siamese Algea Eater", img: 'img/siamese-algea-eater.png', speed: 1.0}
].map(makeFish);
const appendFish = (option) =>
selectFish.innerHTML += option;
const fillFishSelectWithOptions = ({value, name}) =>
`<option value="${value}">${name}</option>`;
fishList.map(fillFishSelectWithOptions).forEach(appendFish);
let currentFish = "";
let activeFish = [];
selectFish.addEventListener('change', ({ target }) => {
currentFish = target.value
});
// fish switch
const switchFish = (fish) => {
const { img, speed } = fishList.find(({value}) => value === fish);
const fishImg = new Image(150, 75)
fishImg.src = img;
activeFish.push({
x: 150,
y: 150,
tx: 0,
ty: 0,
image: fishImg,
speed
})
}
const addFish = document.getElementById('tank-button');
addFish.addEventListener('click', () => switchFish(currentFish));
const getRandomArbitrary = (min, max) =>
Math.random() * (max - min) + min;
// canvas setup
let canvas = document.getElementById('fishcontainer');
const ctx = canvas.getContext('2d');
let fishSize = 1;
const initialize = () => {
window.addEventListener('resize', resizeCanvas, false);
resizeCanvas();
}
let lightOff = false;
const lightSwitch = document.getElementById('lightswitch');
lightSwitch.addEventListener('click', () => {
if (!lightOff) lightOff = true;
else lightOff = false;
})
// drawing and direction
const drawAllFish = () => {
if (lightOff) {
ctx.filter = 'grayscale(0.85)';
canvas.style.filter = 'grayscale(85%)';
} else {
ctx.filter = 'none';
canvas.style.filter = 'none';
}
ctx.filter = 'drop-shadow(1px 1px 3px #404040)'
activeFish.forEach(({ image, x, y, flip }) => {
if (flip) {
ctx.drawImage(image, x - image.width /4, y - image.height /4 , image.width * fishSize, image.height * fishSize);
} else {
ctx.save();
ctx.translate(x, y);
ctx.scale(-1, 1);
ctx.drawImage(image, - image.width /4, - image.height /4, image.width * fishSize, image.height * fishSize);
ctx.restore();
}
})
}
// mouse interaction
const updateAllFish = (time) => {
activeFish = activeFish.map(({ x, y, tx, ty, speed, ...rest }) => {
let newTx = tx;
let newTy = ty;
let dx = (tx - x) * time * speed;
let dy = (ty - y) * time * speed;
if (mouse.mousedown) {
newTx = mouse.x;
newTy = mouse.y;
} else if ((tx - x < 5) && (ty - y < 5)) {
newTx = getRandomArbitrary(0.2, 0.8) * canvas.width;
newTy = getRandomArbitrary(0.2, 0.8) * canvas.height;
}
return { x: x + dx, y: y + dy, tx: newTx, ty: newTy, speed, ...rest, flip: dx > 0 };
});
}
const mouse = {
mousedown: false,
x: canvas.width/2,
y: canvas.height/2,
}
canvas.addEventListener('mousedown', function (event) {
mouse.mousedown = true;
})
canvas.addEventListener('mouseup', function (event) {
mouse.mousedown = false;
})
canvas.addEventListener('mousemove', function (event) {
let canvasPosition = canvas.getBoundingClientRect();
mouse.x = event.x - canvasPosition.left;
mouse.y = event.y - canvasPosition.top;
})
// animate
let lastTime = Date.now();
const animate = () => {
let currentTime = Date.now();
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawAllFish();
updateAllFish((currentTime - lastTime) / 2000);
requestAnimationFrame(animate);
lastTime = Date.now();
}
animate();
const resizeCanvas = () => {
canvas = document.getElementById("fishcontainer");
canvas.getBoundingClientRect();
const width = window.innerWidth * 0.8
const height = width * 0.625
canvas.width = width
canvas.height = height
fishSize = width / 1000
const xClip = width * 0.022
const yClip = height * 0.1456
const widthClip = width - (xClip * 2)
const heightClip = height - (yClip + (height * 0.048))
ctx.rect(xClip, yClip, widthClip, heightClip)
ctx.clip()
}
initialize();
const reset = document.getElementById('clear-fish');
reset.addEventListener('click', () => {
activeFish = [];
});
const alertClear = () => {
let x = document.getElementById('alert-1');
let y = document.getElementById('alert-2')
x.style.display = 'none';
y.style.display = 'flex';
}
const resetClear = () => {
y = document.getElementById('alert-2')
y.style.display = 'none';
}
const alertSwitch = document.getElementById('tank-button');
alertSwitch.addEventListener('click', () => {
alertClear();
});
const resetSwitch = document.getElementById('clear-fish');
resetSwitch.addEventListener('click', () => {
resetClear();
})
const handleAddFishFromCard = (fish) => {
alertClear();
window.scrollTo(0,175);
switchFish(fish)
}
fishList.map(({value}, i) => {
const fish = document.getElementById(`card-button${i+1}`);
fish.addEventListener('click', () => handleAddFishFromCard(value))
})