Skip to content

Commit

Permalink
feat: text 支持数值变化动画
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Feb 2, 2024
1 parent 8671b33 commit 1c15d73
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
25 changes: 20 additions & 5 deletions packages/f-engine/src/canvas/render/animator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class Animator extends EE {
iterations,
clip,
direction = 'normal',
onFrame = () => {},
onEnd = () => {},
onFrame,
onEnd,
} = effect;
// shape 动画
if (property.length && duration > 0) {
if ((property.length || onFrame) && duration > 0) {
// 应用样式
const style = { ...omit(start, property), ...omit(end, property) };
applyStyle(shape, style);
Expand All @@ -68,8 +68,23 @@ class Animator extends EE {
direction,
});
if (animation) {
animation.onframe = onFrame;
animation.onfinish = onEnd;
const onframe = onFrame
? (e) => {
const animationTarget = e.target;
const timing = animationTarget.effect.getTiming();
const duration = timing.duration;
const t = e.currentTime / duration;
applyStyle(shape, onFrame(t, e));
}
: null;
animation.onframe = onframe;
animation.onfinish =
onframe || onEnd
? (e) => {
onframe && onframe(e);
onEnd && onEnd(e);
}
: null;

// 过滤无限循环的动画
if (iterations !== Infinity) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions packages/f-engine/test/shape/animation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,41 @@ describe('Canvas', () => {
await delay(1500);
expect(context).toMatchImageSnapshot();
});

it('text number', async () => {
const context = createContext('text-number');
const onFrame = jest.fn();
const { props } = (
<Canvas context={context} pixelRatio={1}>
<text
style={{
x: 10,
y: 10,
fill: '#000',
text: 'index: 100',
}}
animation={{
appear: {
easing: 'quadraticOut',
duration: 100,
// property: [],
onFrame: (t) => {
onFrame();
return {
text: `index: ${(100 * t).toFixed(0)}`,
};
},
},
}}
/>
</Canvas>
);

const canvas = new Canvas(props);
await delay(100);
await canvas.render();
await delay(500);
expect(context).toMatchImageSnapshot();
expect(onFrame).toBeCalled();
});
});

0 comments on commit 1c15d73

Please sign in to comment.