forked from tweenjs/tween.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
06_array_interpolation.html
144 lines (100 loc) · 3.5 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
<!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="../src/Tween.js"></script>
<script src="js/RequestAnimationFrame.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( title, interpolation ) {
var div = document.createElement( 'div' );
div.style.display = 'inline-block';
div.style.width = width + 20 + 'px';
div.style.height = height + 20 + 'px';
var canvas = document.createElement( 'canvas' );
canvas.width = width;
canvas.height = height;
var context = canvas.getContext( '2d' );
context.fillStyle = "rgb(250,250,250)";
context.fillRect( 0, 0, width, height );
context.fillStyle = "rgb(200,200,200)";
context.fillRect( x0 - 3, y0 - 3, 6, 6 );
context.fillRect( xA[ xA.length - 1 ] - 3, yA[ yA.length - 1 ] - 3, 6, 6 );
for ( var i = 0; i < xA.length; i++ ) {
context.fillRect( xA[i] - 2, yA[i] - 2, 4, 4 );
}
context.lineWidth = 2;
context.strokeStyle = "rgba(255,127,127,0.9)";
var obj = { x: x0, y: y0, old: { x: x0, y: y0 } };
new TWEEN.Tween( obj ).to( { x: xA, y: yA }, 3000 ).onUpdate( function(object) {
context.beginPath();
context.moveTo( object.old.x, object.old.y );
context.lineTo( object.x, object.y );
context.closePath();
context.stroke();
object.old.x = object.x;
object.old.y = object.y;
}).interpolation( interpolation ).easing( TWEEN.Easing.Linear.None ).delay( 250 ).start();
div.appendChild( document.createTextNode( title ) );
div.appendChild( document.createElement( 'br' ) );
div.appendChild( canvas );
return div;
}
}
function animate( time ) {
requestAnimationFrame( animate );
TWEEN.update( time );
}
</script>
</body>
</html>