forked from tweenjs/tween.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07a_dynamic_to_two_array_values.html
122 lines (101 loc) · 2.64 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
118
119
120
121
122
<!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 src="../dist/tween.umd.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script type="module">
import {drawRabbit, drawFox} from './js/drawings.js'
startScene('scene1', false)
startScene('scene2', true)
function startScene(id, dynamic) {
var context
var width
var height
var rabbit1
var rabbit2
var fox
init()
animate()
function init() {
width = 480
height = 320
var scene = document.getElementById(id)
var canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
scene.appendChild(canvas)
context = canvas.getContext('2d')
rabbit1 = {
x: width * 0.8,
y: 50,
}
new TWEEN.Tween(rabbit1)
.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()
rabbit2 = {
x: width - 50,
y: height - 50,
}
new TWEEN.Tween(rabbit2)
.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()
var rabbits = {
x: [rabbit1.x, rabbit2.x],
y: [rabbit1.y, rabbit2.y],
}
fox = {
x: 50,
y: 50,
}
new TWEEN.Tween(fox).to(rabbits, 3000).dynamic(dynamic).interpolation(TWEEN.Interpolation.CatmullRom).start()
}
function animate(time) {
TWEEN.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>