@@ -25,6 +25,7 @@ <h2>06 _ array interpolation</h2>
2525 < div id ="target "> </ div >
2626
2727 < script src ="../dist/tween.umd.js "> </ script >
28+ < script src ="js/createGraph.js "> </ script >
2829 < script >
2930 init ( )
3031 animate ( )
@@ -73,51 +74,70 @@ <h2>06 _ array interpolation</h2>
7374 target . appendChild ( createPath ( '' , TWEEN . Interpolation . Bezier ) )
7475 target . appendChild ( createPath ( '' , TWEEN . Interpolation . CatmullRom ) )
7576
76- function createPath ( title , interpolation ) {
77+ function createPath ( text , interpolation , width = 240 , height = 160 ) {
7778 var div = document . createElement ( 'div' )
7879 div . style . display = 'inline-block'
80+ // +20 for padding
7981 div . style . width = width + 20 + 'px'
8082 div . style . height = height + 20 + 'px'
8183
8284 var canvas = document . createElement ( 'canvas' )
83- canvas . width = width
84- canvas . height = height
85+ canvas . style . width = width + 'px'
86+ canvas . style . height = height + 'px'
87+ canvas . width = toPhysicalPx ( width )
88+ canvas . height = toPhysicalPx ( height )
8589
8690 var context = canvas . getContext ( '2d' )
8791 context . fillStyle = 'rgb(250,250,250)'
88- context . fillRect ( 0 , 0 , width , height )
92+ context . fillRect ( 0 , 0 , toPhysicalPx ( width ) , toPhysicalPx ( height ) )
8993
94+ context . lineWidth = toPhysicalPx ( 1 )
95+ context . strokeStyle = 'rgb(230,230,230)'
96+
97+ // points
9098 context . fillStyle = 'rgb(200,200,200)'
91- context . fillRect ( x0 - 3 , y0 - 3 , 6 , 6 )
92- context . fillRect ( xA [ xA . length - 1 ] - 3 , yA [ yA . length - 1 ] - 3 , 6 , 6 )
99+ context . fillRect (
100+ toPhysicalPx ( x0 ) - toPhysicalPx ( 3 ) ,
101+ toPhysicalPx ( y0 ) - toPhysicalPx ( 3 ) ,
102+ toPhysicalPx ( 6 ) ,
103+ toPhysicalPx ( 6 ) ,
104+ )
105+ context . fillRect (
106+ toPhysicalPx ( xA [ xA . length - 1 ] ) - toPhysicalPx ( 3 ) ,
107+ toPhysicalPx ( yA [ yA . length - 1 ] ) - toPhysicalPx ( 3 ) ,
108+ toPhysicalPx ( 6 ) ,
109+ toPhysicalPx ( 6 ) ,
110+ )
93111
94112 for ( var i = 0 ; i < xA . length ; i ++ ) {
95- context . fillRect ( xA [ i ] - 2 , yA [ i ] - 2 , 4 , 4 )
113+ context . fillRect (
114+ toPhysicalPx ( xA [ i ] ) - toPhysicalPx ( 2 ) ,
115+ toPhysicalPx ( yA [ i ] ) - toPhysicalPx ( 2 ) ,
116+ toPhysicalPx ( 4 ) ,
117+ toPhysicalPx ( 4 ) ,
118+ )
96119 }
120+ //
97121
98- context . lineWidth = 2
122+ context . lineWidth = toPhysicalPx ( 2 )
99123 context . strokeStyle = 'rgba(255,127,127,0.9)'
124+ context . beginPath ( )
125+ context . moveTo ( toPhysicalPx ( x0 ) , toPhysicalPx ( y0 ) )
126+ context . lineCap = 'round'
100127
101- var obj = { x : x0 , y : y0 , old : { x : x0 , y : y0 } }
128+ var position = { x : x0 , y : y0 }
102129
103- new TWEEN . Tween ( obj )
130+ new TWEEN . Tween ( position )
104131 . to ( { x : xA , y : yA } , 3000 )
105- . onUpdate ( function ( object ) {
106- context . beginPath ( )
107- context . moveTo ( object . old . x , object . old . y )
108- context . lineTo ( object . x , object . y )
109- context . closePath ( )
132+ . easing ( TWEEN . Easing . Linear . None )
133+ . interpolation ( interpolation )
134+ . onUpdate ( function ( ) {
135+ context . lineTo ( toPhysicalPx ( position . x ) , toPhysicalPx ( position . y ) )
110136 context . stroke ( )
111-
112- object . old . x = object . x
113- object . old . y = object . y
114137 } )
115- . interpolation ( interpolation )
116- . easing ( TWEEN . Easing . Linear . None )
117- . delay ( 250 )
118138 . start ( )
119139
120- div . appendChild ( document . createTextNode ( title ) )
140+ div . appendChild ( document . createTextNode ( text ) )
121141 div . appendChild ( document . createElement ( 'br' ) )
122142 div . appendChild ( canvas )
123143
0 commit comments