-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest animation.html
209 lines (153 loc) · 5.03 KB
/
test animation.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
<style>
body {
width: 100vw;
height: 100vh;
background-color: #333;
overflow: hidden;
margin: 0;
padding: 0;
}
</style>
<div class="container">
<canvas id="canvas"></canvas>
</div>
<script src="scripts/projectInfo.js"></script>
<script src="Math.js"></script>
<script>
class CanvasImage {
constructor(image, x, y, width, height){
this.image = image;
this.x = x;
this.y = y;
this.width = 0;
this.height = 0;
this.targetX = x;
this.targetY = y;
this.targetWidth = width;
this.targetHeight = height;
this.cutX = 0;
this.cutY = 0;
this.cutWidth = image.width;
this.cutHeight = image.height;
}
aspect(){
return this.image.width / this.image.height;
}
tick(){
this.x = MathUtils.Lerp(this.x, this.targetX, 5 * dt);
this.y = MathUtils.Lerp(this.y, this.targetY, 5 * dt);
this.width = MathUtils.Lerp(this.width, this.targetWidth, 5 * dt);
this.height = MathUtils.Lerp(this.height, this.targetHeight, 5 * dt);
}
draw(){
ctx.drawImage(this.image, this.cutX, this.cutY, this.cutWidth, this.cutHeight, this.x, this.y, this.width, this.height);
}
move(x, y){
this.targetX = x;
this.targetY = y;
}
resize(width, height){
this.targetWidth = width;
this.targetHeight = height;
}
clicked(x, y){
return x > this.x &&
x < this.x + this.width &&
y > this.y &&
y < this.y + this.height;
}
}
</script>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var scrollAmount = 0;
var currentScroll = 0;
const fps = 100;
const dt = 1/fps;
const gap = 40;
const initialX = 400;
var images = [];
var clickX = 0;
var clickY = 0;
var selectedIndex = -1;
function loadImages(){
var counter = 0;
for(var i = 0; i < info.length; i++){
(function(i){
var img = new Image();
img.src = info[i].image;
img.onload = function(){
var image = new CanvasImage(img, 0, 0, 600, 400);
images[i] = image;
counter++;
if (counter == info.length){
renderAnimations();
}
}
})(i);
}
}
loadImages();
function renderAnimations(){
ctx.fillStyle = "#333";
ctx.fillRect(0, 0, canvas.width, canvas.height);
var x = initialX;
currentScroll = MathUtils.Lerp(currentScroll, scrollAmount, 5 * dt);
x += currentScroll;
for(var i = 0; i < images.length; i++){
var image = images[i];
image.tick();
var height = 400;
var width = image.aspect() * height;
var y = canvas.height/2 - height/2;
var percentage = MathUtils.InverseLerp(0, canvas.width, x + width / 2);
percentage = MathUtils.Clamp(percentage, 0, 1);
if (selectedIndex == -1){
var cut_width = image.width * 0.7;
var cut_x = MathUtils.Lerp(0, image.width - cut_width, percentage);
image.resize(width, height);
image.cutX = cut_x;
image.cutWidth = cut_width;
image.x = x;
image.y = y;
image.draw();
ctx.font = "20px Arial";
ctx.fillStyle = "white";
ctx.textAlign = "center";
var debugString = `${info[i].name}`;
ctx.fillText(debugString, x + width/2, y + height + 50);
} else {
if (i == selectedIndex){
image.resize(canvas.width, canvas.height);
image.move(0, 0);
image.draw();
} else {
image.cutX = 0;
image.cutWidth = image.width;
image.move(x, canvas.height - height);
image.draw();
}
}
if (selectedIndex != i && image.clicked(clickX, clickY)){
selectedIndex = i;
console.log(i);
clickX = 0;
clickY = 0;
}
x += width;
x += gap;
}
setTimeout(renderAnimations, 1000/60);
}
canvas.addEventListener("mousedown", function(e){
clickX = e.clientX;
clickY = e.clientY;
});
canvas.addEventListener("wheel", function(e){
scrollAmount -= e.deltaY * 5;
scrollAmount = Math.min(scrollAmount, 0);
});
</script>