Skip to content

Commit 1c15d73

Browse files
committed
feat: text 支持数值变化动画
1 parent 8671b33 commit 1c15d73

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

packages/f-engine/src/canvas/render/animator.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ class Animator extends EE {
4343
iterations,
4444
clip,
4545
direction = 'normal',
46-
onFrame = () => {},
47-
onEnd = () => {},
46+
onFrame,
47+
onEnd,
4848
} = effect;
4949
// shape 动画
50-
if (property.length && duration > 0) {
50+
if ((property.length || onFrame) && duration > 0) {
5151
// 应用样式
5252
const style = { ...omit(start, property), ...omit(end, property) };
5353
applyStyle(shape, style);
@@ -68,8 +68,23 @@ class Animator extends EE {
6868
direction,
6969
});
7070
if (animation) {
71-
animation.onframe = onFrame;
72-
animation.onfinish = onEnd;
71+
const onframe = onFrame
72+
? (e) => {
73+
const animationTarget = e.target;
74+
const timing = animationTarget.effect.getTiming();
75+
const duration = timing.duration;
76+
const t = e.currentTime / duration;
77+
applyStyle(shape, onFrame(t, e));
78+
}
79+
: null;
80+
animation.onframe = onframe;
81+
animation.onfinish =
82+
onframe || onEnd
83+
? (e) => {
84+
onframe && onframe(e);
85+
onEnd && onEnd(e);
86+
}
87+
: null;
7388

7489
// 过滤无限循环的动画
7590
if (iterations !== Infinity) {

packages/f-engine/test/shape/animation.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,41 @@ describe('Canvas', () => {
343343
await delay(1500);
344344
expect(context).toMatchImageSnapshot();
345345
});
346+
347+
it('text number', async () => {
348+
const context = createContext('text-number');
349+
const onFrame = jest.fn();
350+
const { props } = (
351+
<Canvas context={context} pixelRatio={1}>
352+
<text
353+
style={{
354+
x: 10,
355+
y: 10,
356+
fill: '#000',
357+
text: 'index: 100',
358+
}}
359+
animation={{
360+
appear: {
361+
easing: 'quadraticOut',
362+
duration: 100,
363+
// property: [],
364+
onFrame: (t) => {
365+
onFrame();
366+
return {
367+
text: `index: ${(100 * t).toFixed(0)}`,
368+
};
369+
},
370+
},
371+
}}
372+
/>
373+
</Canvas>
374+
);
375+
376+
const canvas = new Canvas(props);
377+
await delay(100);
378+
await canvas.render();
379+
await delay(500);
380+
expect(context).toMatchImageSnapshot();
381+
expect(onFrame).toBeCalled();
382+
});
346383
});

0 commit comments

Comments
 (0)