/
06_array_interpolation.html
155 lines (126 loc) · 3.99 KB
/
06_array_interpolation.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
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
<!doctype html>
<html lang="en">
<head>
<title>Tween.js / array interpolation</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
body {
margin: 0px;
}
#target {
font-size: 13px;
padding: 0px 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>06 _ array interpolation</h2>
<p>The different interpolations if arrays are used as values.</p>
</div>
<div id="target"></div>
<script src="../dist/tween.umd.js"></script>
<script src="js/createGraph.js"></script>
<script>
init()
animate()
function init() {
var target = document.getElementById('target')
var width = 240,
height = 160
// random points
/*
var x0 = Math.random() * ( width - 40 ) + 20,
y0 = Math.random() * ( height - 40 ) + 20,
xA = [],
yA = [];
for ( var i = 0; i < 10; i++ ) {
xA.push( Math.random() * ( width - 40 ) + 20 );
yA.push( Math.random() * ( height - 40 ) + 20 );
}
*/
// fixed points
var min = 1 / 6,
max = 5 / 6
var x0 = width * min,
y0 = height / 2,
xA = [width * max, width / 2],
yA = [height * min, height * max]
target.appendChild(createPath('Linear', TWEEN.Interpolation.Linear))
target.appendChild(createPath('Bezier', TWEEN.Interpolation.Bezier))
target.appendChild(createPath('CatmullRom', TWEEN.Interpolation.CatmullRom))
target.appendChild(document.createElement('br'))
xA.push(x0)
yA.push(y0)
target.appendChild(createPath('start === end', TWEEN.Interpolation.Linear))
target.appendChild(createPath('', TWEEN.Interpolation.Bezier))
target.appendChild(createPath('', TWEEN.Interpolation.CatmullRom))
function createPath(text, interpolation, width = 240, height = 160) {
var div = document.createElement('div')
div.style.display = 'inline-block'
// +20 for padding
div.style.width = width + 20 + 'px'
div.style.height = height + 20 + 'px'
var canvas = document.createElement('canvas')
canvas.style.width = width + 'px'
canvas.style.height = height + 'px'
canvas.width = toPhysicalPx(width)
canvas.height = toPhysicalPx(height)
var context = canvas.getContext('2d')
context.fillStyle = 'rgb(250,250,250)'
context.fillRect(0, 0, toPhysicalPx(width), toPhysicalPx(height))
context.lineWidth = toPhysicalPx(1)
context.strokeStyle = 'rgb(230,230,230)'
// points
context.fillStyle = 'rgb(200,200,200)'
context.fillRect(
toPhysicalPx(x0) - toPhysicalPx(3),
toPhysicalPx(y0) - toPhysicalPx(3),
toPhysicalPx(6),
toPhysicalPx(6),
)
context.fillRect(
toPhysicalPx(xA[xA.length - 1]) - toPhysicalPx(3),
toPhysicalPx(yA[yA.length - 1]) - toPhysicalPx(3),
toPhysicalPx(6),
toPhysicalPx(6),
)
for (var i = 0; i < xA.length; i++) {
context.fillRect(
toPhysicalPx(xA[i]) - toPhysicalPx(2),
toPhysicalPx(yA[i]) - toPhysicalPx(2),
toPhysicalPx(4),
toPhysicalPx(4),
)
}
//
context.lineWidth = toPhysicalPx(2)
context.strokeStyle = 'rgba(255,127,127,0.9)'
context.beginPath()
context.moveTo(toPhysicalPx(x0), toPhysicalPx(y0))
context.lineCap = 'round'
var position = {x: x0, y: y0}
new TWEEN.Tween(position)
.to({x: xA, y: yA}, 3000)
.easing(TWEEN.Easing.Linear.None)
.interpolation(interpolation)
.onUpdate(function () {
context.lineTo(toPhysicalPx(position.x), toPhysicalPx(position.y))
context.stroke()
})
.start()
div.appendChild(document.createTextNode(text))
div.appendChild(document.createElement('br'))
div.appendChild(canvas)
return div
}
}
function animate(time) {
requestAnimationFrame(animate)
TWEEN.update(time)
}
</script>
</body>
</html>