@@ -17,7 +17,7 @@ import {
17
17
import easingTypes from './easing' ;
18
18
import _plugin from './plugins' ;
19
19
import StylePlugin from './plugin/StylePlugin' ;
20
- import { startConvertToEndUnit , transformOrFilter } from './util.js' ;
20
+ import { startConvertToEndUnit , transformOrFilter , dataToArray } from './util.js' ;
21
21
22
22
const DEFAULT_EASING = 'easeInOutQuad' ;
23
23
const DEFAULT_DURATION = 450 ;
@@ -47,9 +47,10 @@ function defaultData(vars, now) {
47
47
} ;
48
48
}
49
49
50
- const Tween = function ( target , toData , props ) {
50
+ const Tween = function ( target , to , attr ) {
51
+ const toData = dataToArray ( to ) ;
51
52
this . target = target ;
52
- this . attr = props . attr || 'style' ;
53
+ this . attr = attr || 'style' ;
53
54
// 时间精度补齐;
54
55
this . accuracy = 0.00001 ;
55
56
// 记录总时间;
@@ -80,12 +81,13 @@ const Tween = function (target, toData, props) {
80
81
const p = Tween . prototype ;
81
82
p . setAttrIsStyle = function ( ) {
82
83
const data = [ ] ;
84
+ const defaultParam = defaultData ( { } , 0 ) ;
83
85
this . data . forEach ( ( d , i ) => {
84
86
const _d = { ...d } ;
85
87
if ( this . attr === 'style' ) {
86
88
data [ i ] = { } ;
87
89
Object . keys ( _d ) . forEach ( key => {
88
- if ( key in defaultData ( { } , 0 ) ) {
90
+ if ( key in defaultParam ) {
89
91
data [ i ] [ key ] = _d [ key ] ;
90
92
delete _d [ key ] ;
91
93
}
@@ -102,7 +104,10 @@ p.setAttrIsStyle = function () {
102
104
delete _d [ key ] ;
103
105
this . startDefaultData . style = this . target . getAttribute ( 'style' ) || '' ;
104
106
} else {
105
- this . startDefaultData [ key ] = this . target . getAttribute ( key ) ;
107
+ if ( key in defaultParam ) {
108
+ return ;
109
+ }
110
+ this . startDefaultData [ key ] = this . getValue ( key ) ;
106
111
}
107
112
} ) ;
108
113
data [ i ] = _d ;
@@ -128,15 +133,15 @@ p.setDefaultData = function (_vars) {
128
133
const _data = item [ _key ] ;
129
134
if ( _key in _plugin ) {
130
135
tweenData . vars [ _key ] = new _plugin [ _key ] ( this . target , _data , tweenData . type ) ;
136
+ } else if ( ( _key === 'd' || _key === 'points' ) && 'SVGMorph' in _plugin ) {
137
+ tweenData . vars [ _key ] = new _plugin . SVGMorph ( this . target , _data , _key ) ;
131
138
} else if ( _key . match ( / c o l o r / i) || _key === 'stroke' || _key === 'fill' ) {
132
139
tweenData . vars [ _key ] = { type : 'color' , vars : parseColor ( _data ) } ;
133
140
} else if ( typeof _data === 'number' || _data . split ( / [ , | \s ] / g) . length <= 1 ) {
134
141
const vars = parseFloat ( _data ) ;
135
142
const unit = _data . toString ( ) . replace ( / [ ^ a - z | % ] / g, '' ) ;
136
143
const count = _data . toString ( ) . replace ( / [ ^ + | = | - ] / g, '' ) ;
137
144
tweenData . vars [ _key ] = { unit, vars, count } ;
138
- } else if ( ( _key === 'd' || _key === 'points' ) && 'SVGMorph' in _plugin ) {
139
- tweenData . vars [ _key ] = new _plugin . SVGMorph ( this . target , _data , _key ) ;
140
145
}
141
146
}
142
147
} ) ;
@@ -190,13 +195,13 @@ p.getAnimStartData = function (item) {
190
195
const start = { } ;
191
196
Object . keys ( item ) . forEach ( _key => {
192
197
if ( _key in _plugin || ( this . attr === 'attr' && ( _key === 'd' || _key === 'points' ) ) ) {
193
- this . computedStyle = this . computedStyle || this . getComputedStyle ( ) ;
198
+ this . computedStyle = this . computedStyle || ( ! this . target . getAttribute ? { ... this . target } : this . getComputedStyle ( ) ) ;
194
199
start [ _key ] = item [ _key ] . getAnimStart ( this . computedStyle , this . tween , this . isSvg ) ;
195
200
return ;
196
201
}
197
202
if ( this . attr === 'attr' ) {
198
203
// 除了d和这points外的标签动画;
199
- const attribute = this . target . getAttribute ( _key ) ;
204
+ const attribute = this . getValue ( _key ) ;
200
205
let data = attribute === 'null' || ! attribute ? 0 : attribute ;
201
206
if ( _key . match ( / c o l o r / i) || _key === 'stroke' || _key === 'fill' ) {
202
207
data = ! data && _key === 'stroke' ? 'rgba(255, 255, 255, 0)' : data ;
@@ -236,7 +241,7 @@ p.setRatio = function (ratio, endData, i) {
236
241
if ( ! endVars . type ) {
237
242
data = endVars . unit . charAt ( 1 ) === '=' ? startVars + endVars . vars * ratio + endVars . unit :
238
243
( endVars . vars - startVars ) * ratio + startVars + endVars . unit ;
239
- this . target . setAttribute ( _key , data ) ;
244
+ this . setValue ( _key , endVars . unit ? data : parseFloat ( data ) ) ;
240
245
} else if ( endVars . type === 'color' ) {
241
246
if ( endVars . vars . length === 3 && startVars . length === 4 ) {
242
247
endVars . vars [ 3 ] = 1 ;
@@ -245,12 +250,22 @@ p.setRatio = function (ratio, endData, i) {
245
250
const startData = startVars [ _i ] || 0 ;
246
251
return ( _endData - startData ) * ratio + startData ;
247
252
} ) ;
248
- this . target . setAttribute ( _key , getColor ( data ) ) ;
253
+ this . setValue ( _key , getColor ( data ) ) ;
249
254
}
250
255
}
251
256
} ) ;
252
257
this . setAnimData ( this . tween ) ;
253
258
} ;
259
+ p . getValue = function ( key ) {
260
+ return this . target . getAttribute ? this . target . getAttribute ( key ) : this . target [ key ] ;
261
+ }
262
+ p . setValue = function ( key , value ) {
263
+ if ( this . target . setAttribute ) {
264
+ this . target . setAttribute ( key , value ) ;
265
+ } else {
266
+ this . target [ key ] = value ;
267
+ }
268
+ }
254
269
p . render = function ( ) {
255
270
const reverse = this . reverse ;
256
271
this . defaultData . forEach ( ( item , i ) => {
@@ -389,6 +404,9 @@ p.frame = function (moment) {
389
404
} ) ;
390
405
this . render ( ) ;
391
406
} ;
407
+
408
+ p . init = p . frame ;
409
+
392
410
p . resetAnimData = function ( ) {
393
411
this . tween = { } ;
394
412
this . start = { } ;
@@ -441,9 +459,9 @@ p.resetDefaultStyle = function () {
441
459
const value = getDefaultStyle ( this . target . style . cssText ,
442
460
this . startDefaultData . style ,
443
461
this . data ) ;
444
- this . target . setAttribute ( key , value ) ;
462
+ this . setValue ( key , value ) ;
445
463
} else {
446
- this . target . setAttribute ( key , this . startDefaultData [ key ] ) ;
464
+ this . setValue ( key , this . startDefaultData [ key ] ) ;
447
465
}
448
466
this . computedStyle = null ;
449
467
}
0 commit comments