-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
05_video_and_time.html
76 lines (66 loc) · 1.8 KB
/
05_video_and_time.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
<!doctype html>
<html lang="en">
<head>
<title>Tween.js / video and time</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="info">
<h1><a href="http://github.com/tweenjs/tween.js">tween.js</a></h1>
<h2>05 _ video and time</h2>
<p>Playing a video, synchronizing a tween to it. <button id="playButton">Play</button></p>
</div>
<div
id="target"
style="
position: absolute;
left: 0px;
top: 350px;
transform: translateX(50px);
font-size: 100px;
letter-spacing: -7px;
"
>
<video id="video" src="video/sintel.webm" width="320" height="138"></video>
</div>
<script type="module">
import * as TWEEN from '../dist/tween.esm.js'
let video
let tween = null
playButton.addEventListener('click', function () {
init()
animate()
})
function init() {
const output = document.createElement('div')
const target = document.getElementById('target')
target.appendChild(output)
video = document.getElementById('video')
video.addEventListener(
'play',
function () {
tween = new TWEEN.Tween({x: 50, y: 0})
.to({x: 400}, this.duration)
.easing(TWEEN.Easing.Linear.None)
.onUpdate(function (object) {
const roundX = Math.round(object.x)
const transform = 'translateX(' + roundX + 'px)'
output.innerHTML = 'x == ' + roundX
target.style.transform = transform
})
.start(video.currentTime)
},
false,
)
video.play()
}
function animate() {
if (tween && video.readyState === video.HAVE_ENOUGH_DATA) {
tween.update(video.currentTime)
}
requestAnimationFrame(animate)
}
</script>
</body>
</html>