-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
199 lines (185 loc) · 4.04 KB
/
script.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
// data
const timeline = [
{
year: 2023,
month: 9,
month_name: "2023",
title: "Taarangana 2023"
},
{
year: 2022,
month: 6,
month_name: "2022",
title: "Taarangana 2022"
},
{
year: 2021,
month: 10,
month_name: "2021",
title: "Taarangana 2021"
},
{
year: 2020,
month: 10,
month_name: "2020",
title: "Taarangana 2020"
},
{
year: 2019,
month: 4,
month_name: "2019",
title: "Taarangana 2019"
},
{
year: 2018,
month: 11,
month_name: "2018",
title: "Taarangana 2018"
},
{
year: 2017,
month: 10,
month_name: "2017",
title: "Taarangana 2017"
},
{
year: 2016,
month: 8,
month_name: "2016",
title: "Taarangana 2016"
},
{
year: 2015,
month: 6,
month_name: "2015",
title: "Taarangana 2015"
},
{
year: 2014,
month: 7,
month_name: "2014",
title: "Taarangana 2014"
},
{
year: 2013,
month: 5,
month_name: "2013",
title: "Taarangana 2013"
},
{
year: 2012,
month: 11,
month_name: "2012",
title: "Taarangana 2012"
}
];
//
const mario = document.getElementById("mario");
const ground = document.getElementById("ground");
const grass = document.getElementById("grass");
const eventsContainer = document.getElementById("events");
let currentIndex = -1;
let currentPipe;
let int1;
// click handler
const pipeHandler = () => {
//clearInterval(int1);
//document.getElementById("info").style.display = "none";
// clear old
!currentPipe || currentPipe.classList.remove("active");
// get index
const index = parseInt(event.currentTarget.dataset.index);
// walk
const xpos = -100 - index * 150 - 25;
const curXpos = -100 - currentIndex * 150 - 25;
const distance = curXpos - xpos;
const duration = Math.abs(distance) * 3;
// console.log(distance);
eventsContainer.style.transitionDuration = `${duration}ms`;
eventsContainer.style.transform = `translateX(${xpos}px)`;
ground.style.transitionDuration = `${duration}ms`;
ground.style.backgroundPosition = `${xpos}px 32px`;
grass.style.transitionDuration = `${duration}ms`;
grass.style.backgroundPosition = `${xpos}px 0`;
//
playSfx("jump");
// walk style
const dir = distance < 0 ? "left" : "right";
mario.classList.remove(
"idle",
"walk-left",
"walk-right",
"search-left",
"search-right"
);
mario.classList.add(`walk-${dir}`);
int1 = setTimeout(
(dir, target) => {
mario.classList.remove(`walk-${dir}`);
mario.classList.add(`search-${dir}`);
target.classList.add("active");
playSfx("pipe");
},
duration,
dir,
event.currentTarget
);
// store position
currentIndex = index;
currentPipe = event.currentTarget;
};
// setup timeline
timeline.forEach((event, index) => {
const e = document.createElement("div");
e.classList.add("event");
e.dataset.index = index;
e.dataset.title = event.title;
e.dataset.month = event.month_name;
eventsContainer.appendChild(e);
e.addEventListener("click", pipeHandler.bind(this));
});
/**
* Audio handling
*/
const canAudio = "AudioContext" in window || "webkitAudioContext" in window;
const buffers = {};
let context = void 0;
if (canAudio) {
var AudioContext = window.AudioContext || window.webkitAudioContext;
context = new AudioContext(); // Make it crossbrowser
var gainNode = context.createGain();
gainNode.gain.value = 1; // set volume to 100%
}
const playSfx = function play(id) {
if (!canAudio || !buffers.hasOwnProperty(id)) return;
const buffer = buffers[id];
const source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
source.start();
};
const loadBuffers = (urls, ids) => {
if (typeof urls == "string") urls = [urls];
if (typeof ids == "string") ids = [ids];
urls.forEach((url, index) => {
window
.fetch(url)
.then((response) => response.arrayBuffer())
.then((arrayBuffer) =>
context.decodeAudioData(
arrayBuffer,
(audioBuffer) => {
buffers[ids[index]] = audioBuffer;
},
(error) => console.log(error)
)
);
});
};
loadBuffers(
[
"https://assets.codepen.io/439000/jump.mp3",
"https://assets.codepen.io/439000/smb_pipe.mp3"
],
["jump", "pipe"]
);