Skip to content

Commit

Permalink
feat: shape增加step类型
Browse files Browse the repository at this point in the history
  • Loading branch information
xuying.xu committed Apr 10, 2024
1 parent f307f6d commit cbaf267
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 38 deletions.
31 changes: 29 additions & 2 deletions packages/f-engine/src/shape/smoothPolyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export class SmoothPolyline extends Path {

setAttribute(name, value, force?: boolean) {
super.setAttribute(name, value, force);
if (['smooth', 'points'].indexOf(name) > -1) {
if (['smooth', 'points', 'stepMiddle', 'stepStart', 'stepEnd'].indexOf(name) > -1) {
this.updatePath();
}
}

private updatePath() {
const { smooth, points } = this.parsedStyle;
const { smooth, points, stepMiddle, stepStart, stepEnd } = this.parsedStyle;
const { points: pos } = points;

const d: PathArray = [['M', pos[0][0], pos[0][1]]];
Expand All @@ -44,6 +44,33 @@ export class SmoothPolyline extends Path {
const sp = sps[i];
d.push(['C', sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]]);
}
} else if (stepEnd) {
let i;
let l;
for (i = 1, l = pos.length; i < l; i++) {
const x = pos[i][0]
d.push(['L', x, pos[i - 1][1]])
d.push(['L', x, pos[i][1]])
d.push(['L', pos[i][0], pos[i][1]]);
}
} else if (stepMiddle) {
let i;
let l;
for (i = 1, l = pos.length; i < l; i++) {
const x = (pos[i][0] + pos[i - 1][0]) / 2
d.push(['L', x, pos[i - 1][1]])
d.push(['L', x, pos[i][1]])
d.push(['L', pos[i][0], pos[i][1]]);
}
} else if (stepStart) {
let i;
let l;
for (i = 1, l = pos.length; i < l; i++) {
const x = pos[i - 1][0]
d.push(['L', x, pos[i - 1][1]])
d.push(['L', x, pos[i][1]])
d.push(['L', pos[i][0], pos[i][1]]);
}
} else {
let i;
let l;
Expand Down
75 changes: 39 additions & 36 deletions packages/f-engine/src/types/shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,67 +100,70 @@ type omitStyleProps = 'display';

export interface GroupStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GGroupStyleProps, omitStyleProps> {}
StyleClipProps,
Omit<GGroupStyleProps, omitStyleProps> { }
export interface RectStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GRectStyleProps, omitStyleProps | 'width' | 'height' | 'radius'> {
StyleClipProps,
Omit<GRectStyleProps, omitStyleProps | 'width' | 'height' | 'radius'> {
radius?: ArrayAttribute;
}
export interface CircleStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GCircleStyleProps, omitStyleProps | 'r'> {
StyleClipProps,
Omit<GCircleStyleProps, omitStyleProps | 'r'> {
r?: string | number;
}
export interface LineStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GLineStyleProps, omitStyleProps | 'x1' | 'y1' | 'x2' | 'y2'> {
StyleClipProps,
Omit<GLineStyleProps, omitStyleProps | 'x1' | 'y1' | 'x2' | 'y2'> {
x1?: string | number;
y1?: string | number;
x2?: string | number;
y2?: string | number;
}
export interface PolygonStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GPolygonStyleProps, omitStyleProps | 'points'> {
StyleClipProps,
Omit<GPolygonStyleProps, omitStyleProps | 'points'> {
points: [number, number][] | [string, string][] | [number, string][] | [string, number][];
smooth?: boolean;
}
export interface PolylineStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GPolylineStyleProps, omitStyleProps | 'points'> {
StyleClipProps,
Omit<GPolylineStyleProps, omitStyleProps | 'points'> {
points: [number, number][] | [string, string][] | [number, string][] | [string, number][];
smooth?: boolean;
stepMiddle?: boolean;
stepStart?: boolean;
stepEnd?: boolean;
}
export interface ArcStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GArcStyleProps, omitStyleProps> {}
StyleClipProps,
Omit<GArcStyleProps, omitStyleProps> { }
export interface SectorStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GSectorStyleProps, omitStyleProps> {}
StyleClipProps,
Omit<GSectorStyleProps, omitStyleProps> { }
export interface TextStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GTextStyleProps, omitStyleProps> {}
StyleClipProps,
Omit<GTextStyleProps, omitStyleProps> { }
export interface ImageStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GImageStyleProps, omitStyleProps> {}
StyleClipProps,
Omit<GImageStyleProps, omitStyleProps> { }
export interface PathStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GPathStyleProps, omitStyleProps> {}
StyleClipProps,
Omit<GPathStyleProps, omitStyleProps> { }
export interface MarkerStyleProps
extends StyleFlexProps,
StyleClipProps,
Omit<GMarkerStyleProps, omitStyleProps> {}
StyleClipProps,
Omit<GMarkerStyleProps, omitStyleProps> { }

interface AnimationBase {
// 缓动函数
Expand Down Expand Up @@ -198,15 +201,15 @@ export interface ShapeElementProps<T> extends IProps {
animation?: AnimationProps;
}

export interface GroupShapeProps extends ShapeElementProps<GroupStyleProps> {}
export interface RectShapeProps extends ShapeElementProps<RectStyleProps> {}
export interface CircleShapeProps extends ShapeElementProps<CircleStyleProps> {}
export interface LineShapeProps extends ShapeElementProps<LineStyleProps> {}
export interface PolygonShapeProps extends ShapeElementProps<PolygonStyleProps> {}
export interface PolylineShapeProps extends ShapeElementProps<PolylineStyleProps> {}
export interface ArcShapeProps extends ShapeElementProps<ArcStyleProps> {}
export interface SectorShapeProps extends ShapeElementProps<SectorStyleProps> {}
export interface TextShapeProps extends ShapeElementProps<TextStyleProps> {}
export interface ImageShapeProps extends ShapeElementProps<ImageStyleProps> {}
export interface PathShapeProps extends ShapeElementProps<PathStyleProps> {}
export interface MarkerShapeProps extends ShapeElementProps<MarkerStyleProps> {}
export interface GroupShapeProps extends ShapeElementProps<GroupStyleProps> { }
export interface RectShapeProps extends ShapeElementProps<RectStyleProps> { }
export interface CircleShapeProps extends ShapeElementProps<CircleStyleProps> { }
export interface LineShapeProps extends ShapeElementProps<LineStyleProps> { }
export interface PolygonShapeProps extends ShapeElementProps<PolygonStyleProps> { }
export interface PolylineShapeProps extends ShapeElementProps<PolylineStyleProps> { }
export interface ArcShapeProps extends ShapeElementProps<ArcStyleProps> { }
export interface SectorShapeProps extends ShapeElementProps<SectorStyleProps> { }
export interface TextShapeProps extends ShapeElementProps<TextStyleProps> { }
export interface ImageShapeProps extends ShapeElementProps<ImageStyleProps> { }
export interface PathShapeProps extends ShapeElementProps<PathStyleProps> { }
export interface MarkerShapeProps extends ShapeElementProps<MarkerStyleProps> { }
196 changes: 196 additions & 0 deletions packages/f-engine/test/shape/polyline.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import { jsx, Canvas, Component, createRef } from '../../src';
import { createContext, delay } from '../util';
const context = createContext();
import { Renderer } from '@antv/g-mobile-canvas';


describe('polyline', () => {
it('polyline', async () => {
const renderer = new Renderer();
const context = createContext('polyline');

const { props } = (
<Canvas renderer={renderer} context={context}>
<polyline
style={{
points: [
[0, 10],
[20, 80],
[40, 40],
[60, 10],
[80, 60]
],
stroke: 'blue'
}}
animation={{
update: {
easing: 'linear',
duration: 100,
property: ['points'],
},
}}
/>
</Canvas>
);
const canvas = new Canvas(props);
await canvas.render();
await delay(500);
expect(context).toMatchImageSnapshot();
})
it('step start polyline', async () => {
const renderer = new Renderer();
const context = createContext('step start polyline');

const { props } = (
<Canvas renderer={renderer} context={context}>
<polyline
style={{
points: [
[0, 10],
[20, 80],
[40, 40],
[60, 10],
[80, 60]
],
stroke: 'blue'
}}
animation={{
update: {
easing: 'linear',
duration: 100,
property: ['points'],
},
}}
/>
<polyline
style={{
points: [
[0, 10],
[20, 80],
[40, 40],
[60, 10],
[80, 60]
],
stroke: 'red',
stepStart: true,
}}
animation={{
update: {
easing: 'linear',
duration: 200,
property: ['points'],
},
}}
/>
</Canvas>
);
const canvas = new Canvas(props);
await canvas.render();
await delay(500);
expect(context).toMatchImageSnapshot();
})
it('step middle polyline', async () => {
const renderer = new Renderer();
const context = createContext('step middle polyline');

const { props } = (
<Canvas renderer={renderer} context={context}>
<polyline
style={{
points: [
[0, 10],
[20, 80],
[40, 40],
[60, 10],
[80, 60]
],
stroke: 'blue'
}}
animation={{
update: {
easing: 'linear',
duration: 100,
property: ['points'],
},
}}
/>
<polyline
style={{
points: [
[0, 10],
[20, 80],
[40, 40],
[60, 10],
[80, 60]
],
stroke: 'red',
stepMiddle: true,
}}
animation={{
update: {
easing: 'linear',
duration: 200,
property: ['points'],
},
}}
/>
</Canvas>
);
const canvas = new Canvas(props);
await canvas.render();
await delay(500);
expect(context).toMatchImageSnapshot();
})
it('step end polyline', async () => {
const renderer = new Renderer();
const context = createContext('step end polyline');

const { props } = (
<Canvas renderer={renderer} context={context}>
<polyline
style={{
points: [
[0, 10],
[20, 80],
[40, 40],
[60, 10],
[80, 60]
],
stroke: 'blue'
}}
animation={{
update: {
easing: 'linear',
duration: 100,
property: ['points'],
},
}}
/>
<polyline
style={{
points: [
[0, 10],
[20, 80],
[40, 40],
[60, 10],
[80, 60]
],
stroke: 'red',
stepEnd: true,
}}
animation={{
update: {
easing: 'linear',
duration: 200,
property: ['points'],
},
}}
/>
</Canvas>
);
const canvas = new Canvas(props);
await canvas.render();
await delay(500);
expect(context).toMatchImageSnapshot();
})
});

0 comments on commit cbaf267

Please sign in to comment.