-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
07a_dynamic_to_two_array_values.html
117 lines (96 loc) · 2.54 KB
/
07a_dynamic_to_two_array_values.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
<!doctype html>
<html lang="en">
<head>
<title>Tween.js / dynamic to interpolation array</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
body {
margin: 0px;
}
#scene1,
#scene2 {
display: inline-block;
font-size: 13px;
padding-right: 32px;
}
</style>
<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="info" style="position: relative">
<h1><a href="http://github.com/tweenjs/tween.js">tween.js</a></h1>
<h2>07a _ dynamic to interpolation array</h2>
<p>tweening towards two moving targets</p>
<div id="scene1">
<p><code>.dynamic(false)</code></p>
</div>
<div id="scene2">
<p><code>.dynamic(true)</code></p>
</div>
</div>
<script type="module">
import * as TWEEN from '../dist/tween.esm.js'
import {drawRabbit, drawFox} from './js/drawings.js'
startScene('scene1', false)
startScene('scene2', true)
function startScene(id, dynamic) {
const group = new TWEEN.Group()
const width = 480
const height = 320
const scene = document.getElementById(id)
const canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
scene.appendChild(canvas)
const context = canvas.getContext('2d')
const rabbit1 = {
x: width * 0.8,
y: 50,
}
new TWEEN.Tween(rabbit1, group)
.to({x: width / 2, y: height - 50}, 2000)
.easing(TWEEN.Easing.Cubic.InOut)
.onUpdate(function ({x, y}) {
rabbits.x[1] = x
rabbits.y[1] = y
})
.start()
const rabbit2 = {
x: width - 50,
y: height - 50,
}
new TWEEN.Tween(rabbit2, group)
.to({x: width / 3, y: 50}, 3000)
.easing(TWEEN.Easing.Cubic.InOut)
.onUpdate(function ({x, y}) {
rabbits.x[2] = x
rabbits.y[2] = y
})
.start()
const rabbits = {
x: [rabbit1.x, rabbit2.x],
y: [rabbit1.y, rabbit2.y],
}
const fox = {
x: 50,
y: 50,
}
new TWEEN.Tween(fox, group)
.to(rabbits, 3000)
.dynamic(dynamic)
.interpolation(TWEEN.Interpolation.CatmullRom)
.start()
animate()
function animate(time) {
group.update(time)
context.fillStyle = 'rgb(240,250,240)'
context.fillRect(0, 0, width, height)
drawRabbit(context, rabbit1.x, rabbit1.y, 'rgb(0,0,150)')
drawRabbit(context, rabbit2.x, rabbit2.y, 'rgb(0,80,80)')
drawFox(context, fox.x, fox.y, 'rgb(200,80,80)')
requestAnimationFrame(animate)
}
}
</script>
</body>
</html>