-
Notifications
You must be signed in to change notification settings - Fork 0
/
basketball_animate.js
415 lines (396 loc) · 15.3 KB
/
basketball_animate.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
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
"use strict";
const basketball_animate = (settings, continuation) => {
const url_argument = location.hash.slice(1);
if (/[^\w,.]/.test(url_argument)) {
throw "animation ids may only contain letters, numbers, and underscores";
}
const loop = url_argument.split(",").length >= 3;
const svg_main = document.querySelector("#main_svg");
svg_main.data = settings.svg_source;
svg_main.addEventListener("load", () => {
TweenLite.defaultOverwrite = "none";
// positioning
// Es gibt kein allgemeines Konzept von absoluter Position fuer svg-Elemente.
// Die Funktionen svg_animate.tween_along_path, svg_animate.timeline_align_position
// erlaubt absolute Positionierung,
// wenn man ihr als letzte Argumente eine Definitionen von absoluter Position uebergibt.
const player_absolute_position = {
defining_element: player => player.querySelector("circle"),
coordinate: defining_element => ({
x: defining_element.cx.baseVal.value,
y: defining_element.cy.baseVal.value
})
};
const ball_absolute_position = player_absolute_position;
const ball_dribbled_absolute_position = {
defining_element: ball_absolute_position.defining_element,
coordinate: defining_element => ({
x: ball_absolute_position.coordinate(defining_element).x - 300,
y: ball_absolute_position.coordinate(defining_element).y + 300
})
};
const player_move_defaults = {ease: Power1.easeInOut};
const player_move_tween = (player, move, duration, options = {}) => {
const options_combined = Object.assign({}, player_move_defaults);
Object.assign(options_combined, options);
return svg_animate.along_path(
"to",
svg(player)[0],
duration,
svg(move)[0],
options_combined,
player_absolute_position,
);
};
const player_move = (player, move, start_time, end_time, options) => {
timeline.add(player_move_tween(player, move, end_time - start_time, options), start_time);
};
const center_absolute_position = {
defining_element: center => center,
coordinate: defining_element => {
const start = defining_element.getPointAtLength(0);
const end = defining_element.getPointAtLength(defining_element.getTotalLength());
return {x: (start.x+end.x) / 2, y: (start.y+end.y) / 2};
}
};
const pass_defaults = {ease: Power1.easeInOut};
const pass = (player, receiver, start_time, end_time, options = {}) => {
// ensure that players that have not moved before receiving a pass
// are at the right position when receiving the pass
timeline.seek(timeline.totalDuration(), true);
timeline.seek(0, true);
const receiver_svg = svg(receiver)[0];
ball_throw(svg(player)[0], receiver_svg, start_time, end_time, translation_interim_result_ball => {
timeline.seek(end_time, false);
const options_combined = svg_animate.translation(
translation_interim_result_ball,
svg_animate.translation_interim_result_target(receiver_svg, player_absolute_position),
);
Object.assign(options_combined, pass_defaults);
Object.assign(options_combined, options);
return options_combined;
});
};
const basket_absolute_position = {
defining_element: basket => basket,
coordinate: defining_element => ({
x: defining_element.cx.baseVal.value,
y: defining_element.cy.baseVal.value
})
};
const shoot_defaults = {ease: Power1.easeInOut};
const shoot = (player, start_time, end_time, options = {}) => {
ball_throw(svg(player)[0], null, start_time, end_time, () => {
const options_combined = svg_animate.translation(
svg_animate.translation_interim_result_object(ball, ball_absolute_position),
svg_animate.translation_interim_result_target(basket, basket_absolute_position),
);
Object.assign(options_combined, shoot_defaults);
Object.assign(options_combined, options);
return options_combined;
});
};
const ball_throw =
(player, receiver, start_time, end_time, options_generate) => {
timeline.seek(start_time, false);
const translation_interim_result_ball =
svg_animate.translation_interim_result_object(ball, ball_dribbled_absolute_position);
const start_coordinate = svg_animate.translation(
translation_interim_result_ball,
svg_animate.translation_interim_result_target(player_possession, player_absolute_position)
);
const options = options_generate(
translation_interim_result_ball,
start_coordinate
);
let reversed_start = false;
timeline.addCallback(() => { // onStart cannot be used because is not executed when seeking backwards
if (!reversed_start)
player_possession = null;
else
player_possession = player;
reversed_start = !reversed_start;
}, start_time);
timeline.fromTo( // fromTo is a workaround for a suspected GSAP bug
ball,
end_time - start_time,
start_coordinate,
options,
start_time
);
let reversed_complete = false;
timeline.addCallback(() => { // onComplete cannot be used because is not executed when seeking backwards
if (!reversed_complete)
player_possession = receiver;
else
player_possession = null;
reversed_complete = !reversed_complete;
}, end_time);
timeline.seek(0, false);
};
// hide, show
// to-do. options argument
const hide =
(object, start_time, end_time) =>
{
if (end_time === undefined) {
end_time = start_time;
}
timeline.to(svg(object), end_time - start_time, {opacity: 0, ease: Power0.easeNone}, start_time);
};
const show =
(object, start_time, end_time) =>
{
if (end_time === undefined) {
end_time = start_time;
}
timeline.to(svg(object), end_time - start_time, {opacity: 1, ease: Power0.easeNone}, start_time);
};
const svg = svg_selector => svg_animate.svg_element([svg_main], svg_selector);
// convert music time unit to seconds
const time_duration =
(bar, beat) =>
(bar * settings.beats_per_bar + beat) * 60 / settings.beats_per_minute;
const time =
(bar, beat) =>
time_duration(bar, beat) + 60 * (settings.beats_per_bar-1) / settings.beats_per_minute;
const bars_beats =
time => {
const beats_total =
Math.floor(time * settings.beats_per_minute / 60 - 2 * settings.beats_per_bar);
return (
beats_total >= 0
?
(
(Math.trunc(beats_total / settings.beats_per_bar)+1).toString() +
":" +
(beats_total % settings.beats_per_bar + 1).toString()
)
: beats_total
);
};
// can only be used with paths of which the stroke-dasharray is specified in pixels
const path_shorten =
(path, distance_start, distance_end) =>
{svg_animate.path_shorten(svg(path), distance_start, distance_end);};
const startup_animation_defaults = {};
const startup_animation_timing_defaults = {delay: 1, stagger: .0625, duration: .5};
const startup_animation = (player, options = {}) => {
const translation_interim_result_center =
svg_animate.translation_interim_result_target(svg("#center")[0], center_absolute_position);
const translation_interim_result_ball =
svg_animate.translation_interim_result_object(ball, ball_dribbled_absolute_position);
const startup_animation_timing_defaults_combined =
Object.assign({}, startup_animation_timing_defaults);
Object.assign(startup_animation_timing_defaults_combined, options);
// to-do. use GSAP's stagger
svg(player).forEach(player_current => {
const onUpdate = {};
if (player_current === player_possession) {
const translation_interim_result_player_target =
svg_animate.translation_interim_result_target(player_current, player_absolute_position);
onUpdate.onUpdate = () => {
TweenMax.set(ball, svg_animate.translation(
translation_interim_result_ball,
translation_interim_result_player_target
));
};
}
const options_combined = svg_animate.translation(
svg_animate.translation_interim_result_object(player_current, player_absolute_position),
translation_interim_result_center
);
Object.assign(options_combined, startup_animation_defaults);
Object.assign(options_combined, options);
options_combined.delay = startup_animation_timing_defaults_combined.delay;
svg_animate.merge_callback_options(options_combined, onUpdate);
TweenMax.from(
player_current,
startup_animation_timing_defaults_combined.duration,
options_combined
);
startup_animation_timing_defaults_combined.delay += startup_animation_timing_defaults_combined.stagger;
});
TweenMax.set(
ball,
svg_animate.translation(
translation_interim_result_ball,
svg_animate.translation_interim_result_target(player_possession, player_absolute_position)
)
);
};
// initialize dom references
const music_dom = document.querySelector("#music");
const ball = svg("#ball")[0];
const basket = svg("#basket")[0];
let player_possession = svg(settings.player_possession)[0];
music_dom.src = settings.music_source;
// initialize GSAP timeline objects
const timeline = new TimelineMax({
paused: true,
onUpdate: () => {
// Interim results are calculated up front for performance.
// The ball's interim result has to be recalculated
// throughout the animation if the ball changes internally too.
const translation_interim_result_ball =
svg_animate.translation_interim_result_object(ball, ball_dribbled_absolute_position);
if (player_possession !== null) {
TweenMax.set(ball, svg_animate.translation(
translation_interim_result_ball,
svg_animate.translation_interim_result_target(player_possession, player_absolute_position)
));
}
position_slider.slider("value", timeline.totalProgress() * 100);
position_display_slider.text(bars_beats(timeline.totalTime()));
},
onComplete: () => {
if (loop) {
return;
}
timeline_supplementary.pause();
music_dom.pause();
play_button.button("option", "label", play_button_label.restart);
// This enables the browser to start playing the music with less delay
// because the cost of seeking is already payed.
music_dom.currentTime = 0 + settings.music_offset;
}
});
const timeline_supplementary = new TimelineMax({paused: true});
timeline_supplementary.to(
svg("#hand_clock"),
settings.clock_period * 60 / settings.beats_per_minute,
{
rotation: "360_cw",
transformOrigin: "50% 0%",
ease: Power0.easeNone,
repeat: -1
},
0
);
// user interface
const play_button_label = {
play: "Play",
pause: "Pause",
restart: "Restart"
};
const play_button_click =
() =>
{
if (timeline.totalProgress() !== 1 || loop) {
const paused = !timeline.paused();
music_dom[paused ? "pause" : "play"]();
timeline.paused(paused);
timeline_supplementary.paused(paused);
play_button.button(
"option",
"label",
paused ? play_button_label.play : play_button_label.pause
);
if (paused)
position_display_precise.text(timeline.totalTime() + "s");
}
else {
music_dom.play();
timeline.restart(true, false);
timeline_supplementary.restart(true, false);
play_button.button("option", "label", play_button_label.pause);
}
};
const play_button = $("#play").button({label: play_button_label.play, disabled: true});
play_button.on("click", play_button_click);
$(music_dom).one("canplaythrough", () => {
timeline.to([], 0, {}, music_dom.duration);
// `canplaythrough` is triggered again by seek command right below.
$(music_dom).one("canplaythrough", () => {play_button.button("enable");});
// In addition to starting at the correct offset,
// this enables the browser to start playing the music with less delay
// because the cost of seeking is already payed.
music_dom.currentTime = 0 + settings.music_offset;
});
music_dom.load();
const position_display_precise = $("#position_display_precise");
const position_display_slider = $("#position_display_slider");
const position_slider = $("#position_slider").slider({
min: 0,
max: 100,
step: .1,
stop: () => {
music_dom.currentTime = timeline.totalTime() + settings.music_offset;
},
slide: (event, ui) => {
music_dom.pause();
timeline.pause();
timeline.totalProgress(ui.value/100, false);
timeline_supplementary.pause();
timeline_supplementary.totalTime(timeline.totalTime(), false);
position_display_slider.text(bars_beats(timeline.totalTime()));
position_display_precise.text(timeline.totalTime() + "s");
play_button.button("option", "label", play_button_label.play);
}
});
const speed_change =
(speed_new) =>
{
music_dom.playbackRate = speed_new / 100;
timeline.timeScale(speed_new / 100);
timeline_supplementary.timeScale(speed_new / 100);
};
const speed_display = $("#speed_display");
const speed_slider = $("#speed_slider").slider({
min: 0,
max: 300,
step: 1,
value: 100,
change: (event, ui) => {speed_change(ui.value)},
slide: (event, ui) => {
speed_display.text(ui.value + "%");
}
});
// speed from url
const speed_text = url_argument.split(",")[1] ?? null;
if (speed_text !== null) {
const speed = Number.parseFloat(speed_text) ?? null;
if (speed === null) {
throw "speed parameter must be a number";
}
speed_slider.slider("value", speed);
speed_display.text(speed + "%");
}
// loop
if (loop) {
music_dom.addEventListener(
"ended",
(event) =>
{
timeline_supplementary.pause();
music_dom.pause();
// This enables the browser to start playing the music with less delay
// because the cost of seeking is already payed.
$(music_dom).one("canplaythrough", () => {
music_dom.play();
timeline.restart(true, false);
timeline_supplementary.restart(true, false);
});
music_dom.currentTime = 0 + settings.music_offset;
}
);
}
// export
continuation({
svg,
svg_main,
player_move_tween,
player_move,
pass,
shoot,
hide,
show,
timeline,
time_duration,
time,
bars_beats,
path_shorten,
startup_animation
});
});
};