Skip to content

Commit 8d2cbba

Browse files
committed
bump 2.4.0, support object animation, export Tween;
1 parent 78136cc commit 8d2cbba

File tree

9 files changed

+66
-25
lines changed

9 files changed

+66
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-tween-one",
3-
"version": "2.3.4",
3+
"version": "2.4.0",
44
"description": "tween-one anim component for react",
55
"keywords": [
66
"react",

src/Tween.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import easingTypes from './easing';
1818
import _plugin from './plugins';
1919
import StylePlugin from './plugin/StylePlugin';
20-
import { startConvertToEndUnit, transformOrFilter } from './util.js';
20+
import { startConvertToEndUnit, transformOrFilter, dataToArray } from './util.js';
2121

2222
const DEFAULT_EASING = 'easeInOutQuad';
2323
const DEFAULT_DURATION = 450;
@@ -47,9 +47,10 @@ function defaultData(vars, now) {
4747
};
4848
}
4949

50-
const Tween = function (target, toData, props) {
50+
const Tween = function (target, to, attr) {
51+
const toData = dataToArray(to);
5152
this.target = target;
52-
this.attr = props.attr || 'style';
53+
this.attr = attr || 'style';
5354
// 时间精度补齐;
5455
this.accuracy = 0.00001;
5556
// 记录总时间;
@@ -80,12 +81,13 @@ const Tween = function (target, toData, props) {
8081
const p = Tween.prototype;
8182
p.setAttrIsStyle = function () {
8283
const data = [];
84+
const defaultParam = defaultData({}, 0);
8385
this.data.forEach((d, i) => {
8486
const _d = { ...d };
8587
if (this.attr === 'style') {
8688
data[i] = {};
8789
Object.keys(_d).forEach(key => {
88-
if (key in defaultData({}, 0)) {
90+
if (key in defaultParam) {
8991
data[i][key] = _d[key];
9092
delete _d[key];
9193
}
@@ -102,7 +104,10 @@ p.setAttrIsStyle = function () {
102104
delete _d[key];
103105
this.startDefaultData.style = this.target.getAttribute('style') || '';
104106
} else {
105-
this.startDefaultData[key] = this.target.getAttribute(key);
107+
if (key in defaultParam) {
108+
return;
109+
}
110+
this.startDefaultData[key] = this.getValue(key);
106111
}
107112
});
108113
data[i] = _d;
@@ -128,15 +133,15 @@ p.setDefaultData = function (_vars) {
128133
const _data = item[_key];
129134
if (_key in _plugin) {
130135
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);
131138
} else if (_key.match(/color/i) || _key === 'stroke' || _key === 'fill') {
132139
tweenData.vars[_key] = { type: 'color', vars: parseColor(_data) };
133140
} else if (typeof _data === 'number' || _data.split(/[,|\s]/g).length <= 1) {
134141
const vars = parseFloat(_data);
135142
const unit = _data.toString().replace(/[^a-z|%]/g, '');
136143
const count = _data.toString().replace(/[^+|=|-]/g, '');
137144
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);
140145
}
141146
}
142147
});
@@ -190,13 +195,13 @@ p.getAnimStartData = function (item) {
190195
const start = {};
191196
Object.keys(item).forEach(_key => {
192197
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());
194199
start[_key] = item[_key].getAnimStart(this.computedStyle, this.tween, this.isSvg);
195200
return;
196201
}
197202
if (this.attr === 'attr') {
198203
// 除了d和这points外的标签动画;
199-
const attribute = this.target.getAttribute(_key);
204+
const attribute = this.getValue(_key);
200205
let data = attribute === 'null' || !attribute ? 0 : attribute;
201206
if (_key.match(/color/i) || _key === 'stroke' || _key === 'fill') {
202207
data = !data && _key === 'stroke' ? 'rgba(255, 255, 255, 0)' : data;
@@ -236,7 +241,7 @@ p.setRatio = function (ratio, endData, i) {
236241
if (!endVars.type) {
237242
data = endVars.unit.charAt(1) === '=' ? startVars + endVars.vars * ratio + endVars.unit :
238243
(endVars.vars - startVars) * ratio + startVars + endVars.unit;
239-
this.target.setAttribute(_key, data);
244+
this.setValue(_key, endVars.unit ? data : parseFloat(data));
240245
} else if (endVars.type === 'color') {
241246
if (endVars.vars.length === 3 && startVars.length === 4) {
242247
endVars.vars[3] = 1;
@@ -245,12 +250,22 @@ p.setRatio = function (ratio, endData, i) {
245250
const startData = startVars[_i] || 0;
246251
return (_endData - startData) * ratio + startData;
247252
});
248-
this.target.setAttribute(_key, getColor(data));
253+
this.setValue(_key, getColor(data));
249254
}
250255
}
251256
});
252257
this.setAnimData(this.tween);
253258
};
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+
}
254269
p.render = function () {
255270
const reverse = this.reverse;
256271
this.defaultData.forEach((item, i) => {
@@ -389,6 +404,9 @@ p.frame = function (moment) {
389404
});
390405
this.render();
391406
};
407+
408+
p.init = p.frame;
409+
392410
p.resetAnimData = function () {
393411
this.tween = {};
394412
this.start = {};
@@ -441,9 +459,9 @@ p.resetDefaultStyle = function () {
441459
const value = getDefaultStyle(this.target.style.cssText,
442460
this.startDefaultData.style,
443461
this.data);
444-
this.target.setAttribute(key, value);
462+
this.setValue(key, value);
445463
} else {
446-
this.target.setAttribute(key, this.startDefaultData[key]);
464+
this.setValue(key, this.startDefaultData[key]);
447465
}
448466
this.computedStyle = null;
449467
}

src/TweenOne.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import ReactDom from 'react-dom';
44

5-
import { dataToArray, objectEqual } from './util';
5+
import { objectEqual } from './util';
66
import Tween from './Tween';
77
import ticker from './ticker';
88

@@ -174,7 +174,7 @@ class TweenOne extends Component {
174174
}
175175
}
176176

177-
setDefalut = (props) => {
177+
setDefault = (props) => {
178178
this.moment = props.moment || 0;
179179
this.startMoment = props.moment || 0;
180180
this.startTime = ticker.time;
@@ -196,9 +196,9 @@ class TweenOne extends Component {
196196
this.updateAnim = false;
197197
const props = this.props;
198198
if (props.animation && Object.keys(props.animation).length) {
199-
this.setDefalut(props);
200-
this.tween = new Tween(this.dom, dataToArray(props.animation),
201-
{ attr: props.attr });
199+
this.setDefault(props);
200+
this.tween = new Tween(this.dom, props.animation,
201+
props.attr);
202202
this.tween.reverse = this.reverse;
203203
// 预先注册 raf, 初始动画数值。
204204
this.raf();

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// export this package's api
22
import TweenOne from './TweenOne';
3+
import _tween from './Tween';
34
import group from './TweenOneGroup';
45
import _easing from './easing';
56
import _plugins from './plugins';
@@ -9,6 +10,7 @@ TweenOne.TweenOneGroup = group;
910
TweenOne.easing = _easing;
1011
TweenOne.plugins = _plugins;
1112
TweenOne.ticker = _ticker;
13+
TweenOne.Tween = _tween
1214

1315
export default TweenOne;
1416

@@ -19,3 +21,5 @@ export const easing = _easing;
1921
export const plugins = _plugins;
2022

2123
export const ticker = _ticker;
24+
25+
export const Tween = _tween;

src/plugin/SvgMorphPlugin.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ p.polygonPoints = function (start, end) {
2828
return [startArray, endArray];
2929
};
3030
p.getAnimStart = function () {
31-
this.start = this.target.getAttribute(this.key);
31+
this.start = this.target.getAttribute ? this.target.getAttribute(this.key) : this.target[this.key];
3232
if (this.key === 'd') {
3333
this.pathArray = path2curve(this.start, this.vars);
3434
} else {
@@ -58,7 +58,11 @@ p.setRatio = function (ratio, tween) {
5858
let vars = ratio === 1 ? this.vars : tween[this.key].join(' ');
5959
vars = ratio === 0 ? this.start : vars;
6060
if (vars) {
61-
this.target.setAttribute(this.key, vars);
61+
if (this.target.setAttribute) {
62+
this.target.setAttribute(this.key, vars);
63+
} else {
64+
this.target[this.key] = vars;
65+
}
6266
}
6367
};
6468
export default SvgPlugin;

tests/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import TweenOne from '../typings';
55
const path = 'M0,100 C30,60 0,20 50,50 C70,70 60,0 100,0';
66
const ease = TweenOne.easing.path(path, { rect: 100, lengthPixel: 200 });
77
const Group = TweenOne.TweenOneGroup;
8+
const tween = new TweenOne.Tween({}, { x: 100 });
9+
tween.init();
810
export default () => (
911
<div>
1012
<TweenOne

tests/tweenOne.spec.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import expect from 'expect.js';
66
import TestUtils from 'react-dom/test-utils';
77
import { checkStyleName } from 'style-utils';
88

9-
import Tween, { easing, plugins } from '../src';
9+
import TweenOne, { easing, plugins } from '../src';
1010
import ticker from '../src/ticker';
1111
import BezierPlugin from '../src/plugin/BezierPlugin';
1212

@@ -42,7 +42,7 @@ describe('rc-tween-one', () => {
4242
render() {
4343
if (this.props.component === 'rect') {
4444
return (<svg>
45-
<Tween
45+
<TweenOne
4646
{...this.props}
4747
reverse={this.state.reverse}
4848
animation={this.state.animation} style={this.state.style}
@@ -53,7 +53,7 @@ describe('rc-tween-one', () => {
5353
/>
5454
</svg>);
5555
}
56-
return (<Tween {...this.props}
56+
return (<TweenOne {...this.props}
5757
reverse={this.state.reverse}
5858
animation={this.state.animation}
5959
style={this.state.style}
@@ -62,7 +62,7 @@ describe('rc-tween-one', () => {
6262
paused={this.state.paused}
6363
>
6464
<span>demo</span>
65-
</Tween>);
65+
</TweenOne>);
6666
}
6767
}
6868
const objectOrArray = PropTypes.oneOfType([PropTypes.object,

typings/Tween.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { IAnimObject } from './AnimObject';
2+
3+
export default class RcTweenOneGroup {
4+
constructor(target: Object, animation: IAnimObject | IAnimObject[], attr?: 'style' | 'attr');
5+
init: () => void;
6+
frame: (moment?: Number) => void;
7+
target: Object;
8+
progressTime: Number;
9+
totalTime: Number;
10+
}

typings/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import * as React from 'react';
66

77
import Group from './TweenOneGroup';
8+
import Tween from './Tween';
89
import { IAnimObject, IEaseCallBack } from './AnimObject';
910

1011
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
@@ -44,4 +45,6 @@ export default class RcTweenOne<T> extends React.Component<IProps<T>> {
4445
push(plugin: any): void;
4546
}
4647
static TweenOneGroup: typeof Group;
48+
49+
static Tween: typeof Tween;
4750
}

0 commit comments

Comments
 (0)