-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support polygon sector crosshair for non-smooth angle axis
- Loading branch information
Showing
6 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
packages/vrender-components/__tests__/browser/examples/crosshair-polygon-sector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import '@visactor/vrender'; | ||
import render from '../../util/render'; | ||
import { PolygonSectorCrosshair, SectorCrosshair } from '../../../src'; | ||
|
||
export function run() { | ||
const startAngle = 1.3 * Math.PI; | ||
const endAngle = 1.7 * Math.PI; | ||
const crosshair = new PolygonSectorCrosshair({ | ||
center: { | ||
x: 250, | ||
y: 250 | ||
}, | ||
radius: 100, | ||
innerRadius: 30, | ||
startAngle, | ||
endAngle | ||
}); | ||
|
||
const stage = render([crosshair], 'main'); | ||
|
||
stage.addEventListener('pointermove', e => { | ||
crosshair.setLocation({ | ||
x: e.viewX, | ||
y: e.viewY | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/vrender-components/src/crosshair/polygon-sector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @description sector 类型 crosshair,用于极坐标系下 | ||
*/ | ||
import type { IGroup } from '@visactor/vrender-core'; | ||
import { merge, getAngleByPoint, radianToDegree, polarToCartesian } from '@visactor/vutils'; | ||
import type { PointLocationCfg } from '../core/type'; | ||
import { POLAR_END_ANGLE, POLAR_START_ANGLE } from '../constant'; | ||
import { CrosshairBase } from './base'; | ||
import type { PolygonSectorCrosshairAttrs } from './type'; | ||
import type { ComponentOptions } from '../interface'; | ||
import { loadPolygonSectorCrosshairComponent } from './register'; | ||
|
||
loadPolygonSectorCrosshairComponent(); | ||
export class PolygonSectorCrosshair extends CrosshairBase<PolygonSectorCrosshairAttrs> { | ||
static defaultAttributes = { | ||
polygonSectorStyle: { | ||
fill: '#b2bacf', | ||
opacity: 0.2 | ||
} | ||
}; | ||
|
||
constructor(attributes: PolygonSectorCrosshairAttrs, options?: ComponentOptions) { | ||
super(options?.skipDefault ? attributes : merge({}, PolygonSectorCrosshair.defaultAttributes, attributes)); | ||
} | ||
|
||
protected renderCrosshair(container: IGroup) { | ||
const { center, radius, innerRadius = 0, polygonSectorStyle } = this.attribute as PolygonSectorCrosshairAttrs; | ||
const { startAngle, endAngle } = this.attribute; | ||
const points = []; | ||
points.push(polarToCartesian(center, innerRadius, startAngle)); | ||
points.push(polarToCartesian(center, radius * Math.cos((endAngle - startAngle) / 2), startAngle)); | ||
points.push(polarToCartesian(center, radius, (startAngle + endAngle) / 2)); | ||
points.push(polarToCartesian(center, radius * Math.cos((endAngle - startAngle) / 2), endAngle)); | ||
points.push(polarToCartesian(center, innerRadius, endAngle)); | ||
const path = points.reduce((acc, cur, index) => { | ||
if (index === 0) { | ||
return `M${cur.x},${cur.y}`; | ||
} | ||
return acc + `L${cur.x},${cur.y}`; | ||
}, ''); | ||
|
||
const polygon = container.createOrUpdateChild( | ||
'crosshair-polygon-sector', | ||
{ | ||
path, | ||
...polygonSectorStyle | ||
}, | ||
'path' | ||
); | ||
return polygon; | ||
} | ||
|
||
setLocation(point: PointLocationCfg) { | ||
const { | ||
center, | ||
startAngle = POLAR_START_ANGLE, | ||
endAngle = POLAR_END_ANGLE | ||
} = this.attribute as PolygonSectorCrosshairAttrs; | ||
const sectorAngle = endAngle - startAngle; | ||
const pointAngle = radianToDegree(getAngleByPoint(center, point)); | ||
this.setAttributes({ | ||
startAngle: pointAngle - sectorAngle / 2, | ||
endAngle: pointAngle + sectorAngle / 2 | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters