框架代码同步

This commit is contained in:
fan 2023-06-25 15:06:47 +08:00
parent 4035943936
commit 58ed710688
5 changed files with 119 additions and 24 deletions

@ -1 +1 @@
Subproject commit bf2d81625f3fa89eaca241ea3a04f2198dcfbba2 Subproject commit 218cae4c5cf47aa9aad70b19393fbd0e3fc54825

View File

@ -148,6 +148,23 @@ DisplayObject.prototype.getAllParentScaled =
}); });
return scaled; return scaled;
}; };
DisplayObject.prototype.getPositionOnCanvas =
function getPositionOnCanvas(): Point {
if (this.parent.isCanvas()) {
return this.position;
} else {
return this.parent.localToCanvasPoint(this.position);
}
};
DisplayObject.prototype.updatePositionByCanvasPosition =
function updatePositionByCanvasPosition(p: IPointData): void {
if (this.parent.isCanvas()) {
this.position.copyFrom(p);
} else {
const localPosition = this.parent.canvasToLocalPoint(p);
this.position.copyFrom(localPosition);
}
};
DisplayObject.prototype.saveTransform = function saveTransform() { DisplayObject.prototype.saveTransform = function saveTransform() {
return GraphicTransform.fromObject(this); return GraphicTransform.fromObject(this);
}; };

View File

@ -60,9 +60,28 @@ declare namespace GlobalMixins {
_rotatable: boolean; // 是否可旋转 _rotatable: boolean; // 是否可旋转
rotatable: boolean; rotatable: boolean;
worldAngle: number; // 世界角度,(-180, 180] worldAngle: number; // 世界角度,(-180, 180]
/**
*
*/
getAllParentScaled(): PointType; getAllParentScaled(): PointType;
saveTransform(): GraphicTransform; // 保存变换 /**
loadTransform(transform: GraphicTransform): void; // 加载变换 *
*/
getPositionOnCanvas(): PointType;
/**
*
* @param p
*/
updatePositionByCanvasPosition(p: IPointData): void;
/**
*
*/
saveTransform(): GraphicTransform;
/**
*
* @param transform
*/
loadTransform(transform: GraphicTransform): void;
isChild(obj: DisplayObject): boolean; // 是否子元素 isChild(obj: DisplayObject): boolean; // 是否子元素
isParent(obj: DisplayObject): boolean; // 是否父元素 isParent(obj: DisplayObject): boolean; // 是否父元素
isAssistantAppend(): boolean; // 是否辅助附加图形 isAssistantAppend(): boolean; // 是否辅助附加图形
@ -83,14 +102,45 @@ declare namespace GlobalMixins {
isCanvas(): boolean; // 是否画布对象 isCanvas(): boolean; // 是否画布对象
getViewport(): Viewport; // 获取视口 getViewport(): Viewport; // 获取视口
getGraphicApp(): GraphicApp; // 获取图形app getGraphicApp(): GraphicApp; // 获取图形app
localToCanvasPoint(p: IPointData): PointType; // 图形本地坐标转为画布坐标 /**
localToCanvasPoints(...points: IPointData[]): PointType[]; // 批量转换 *
canvasToLocalPoint(p: IPointData): PointType; // 画布坐标转为图形本地坐标 * @param p
canvasToLocalPoints(...points: IPointData[]): PointType[]; // 批量转换 */
localToCanvasPoint(p: IPointData): PointType;
localToScreenPoint(p: IPointData): PointType; // 本地坐标转为屏幕坐标 /**
localToScreenPoints(...points: IPointData[]): PointType[]; // 批量 *
screenToLocalPoint(p: IPointData): PointType; // 屏幕坐标转为本地坐标 * @param points
*/
localToCanvasPoints(...points: IPointData[]): PointType[];
/**
*
* @param p
*/
canvasToLocalPoint(p: IPointData): PointType;
/**
*
* @param points
*/
canvasToLocalPoints(...points: IPointData[]): PointType[];
/**
*
* @param p
*/
localToScreenPoint(p: IPointData): PointType;
/**
*
* @param points
*/
localToScreenPoints(...points: IPointData[]): PointType[];
/**
*
* @param p
*/
screenToLocalPoint(p: IPointData): PointType;
/**
*
* @param points
*/
screenToLocalPoints(...points: IPointData[]): PointType[]; // 批量 screenToLocalPoints(...points: IPointData[]): PointType[]; // 批量
localBoundsToCanvasPoints(): PointType[]; // 本地包围框转为多边形点坐标 localBoundsToCanvasPoints(): PointType[]; // 本地包围框转为多边形点坐标

View File

@ -72,6 +72,11 @@ export default class AbsorbablePoint
absorbRange: number; absorbRange: number;
scaledListenerOn = false; scaledListenerOn = false;
/**
*
* @param point
* @param absorbRange
*/
constructor(point: IPointData, absorbRange = 10) { constructor(point: IPointData, absorbRange = 10) {
super(AbsorbablePointGraphic.geometry); super(AbsorbablePointGraphic.geometry);
this._point = new Point(point.x, point.y); this._point = new Point(point.x, point.y);
@ -98,11 +103,16 @@ export default class AbsorbablePoint
tryAbsorb(...objs: DisplayObject[]): void { tryAbsorb(...objs: DisplayObject[]): void {
for (let i = 0; i < objs.length; i++) { for (let i = 0; i < objs.length; i++) {
const obj = objs[i]; const obj = objs[i];
const canvasPosition = obj.getPositionOnCanvas();
if ( if (
distance(this._point.x, this._point.y, obj.position.x, obj.position.y) < distance(
this.absorbRange this._point.x,
this._point.y,
canvasPosition.x,
canvasPosition.y
) < this.absorbRange
) { ) {
obj.position.copyFrom(this._point); obj.updatePositionByCanvasPosition(this._point);
} }
} }
} }
@ -122,6 +132,12 @@ export class AbsorbableLine extends Graphics implements AbsorbablePosition {
absorbRange: number; absorbRange: number;
_color = '#E77E0E'; _color = '#E77E0E';
/**
*
* @param p1
* @param p2
* @param absorbRange
*/
constructor(p1: IPointData, p2: IPointData, absorbRange = 20) { constructor(p1: IPointData, p2: IPointData, absorbRange = 20) {
super(); super();
this.p1 = new Point(p1.x, p1.y); this.p1 = new Point(p1.x, p1.y);
@ -155,10 +171,14 @@ export class AbsorbableLine extends Graphics implements AbsorbablePosition {
tryAbsorb(...objs: DisplayObject[]): void { tryAbsorb(...objs: DisplayObject[]): void {
for (let i = 0; i < objs.length; i++) { for (let i = 0; i < objs.length; i++) {
const obj = objs[i]; const obj = objs[i];
const p = obj.position.clone(); const canvasPosition = obj.getPositionOnCanvas();
if (linePoint(this.p1, this.p2, p, this.absorbRange, true)) { if (linePoint(this.p1, this.p2, canvasPosition, this.absorbRange, true)) {
const fp = calculateFootPointFromPointToLine(this.p1, this.p2, p); const fp = calculateFootPointFromPointToLine(
obj.position.copyFrom(fp); this.p1,
this.p2,
canvasPosition
);
obj.updatePositionByCanvasPosition(fp);
} }
} }
} }
@ -173,6 +193,12 @@ export class AbsorbableCircle extends Graphics implements AbsorbablePosition {
radius: number; radius: number;
_color = '#E77E0E'; _color = '#E77E0E';
/**
*
* @param p
* @param radius
* @param absorbRange
*/
constructor(p: IPointData, radius: number, absorbRange = 10) { constructor(p: IPointData, radius: number, absorbRange = 10) {
super(); super();
this.p0 = new Point(p.x, p.y); this.p0 = new Point(p.x, p.y);
@ -202,11 +228,12 @@ export class AbsorbableCircle extends Graphics implements AbsorbablePosition {
tryAbsorb(...objs: DisplayObject[]): void { tryAbsorb(...objs: DisplayObject[]): void {
for (let i = 0; i < objs.length; i++) { for (let i = 0; i < objs.length; i++) {
const obj = objs[i]; const obj = objs[i];
const canvasPosition = obj.getPositionOnCanvas();
const len = distance( const len = distance(
this.p0.x, this.p0.x,
this.p0.y, this.p0.y,
obj.position.x, canvasPosition.x,
obj.position.y canvasPosition.y
); );
if ( if (
len > this.radius - this.absorbRange && len > this.radius - this.absorbRange &&
@ -216,9 +243,9 @@ export class AbsorbableCircle extends Graphics implements AbsorbablePosition {
const p = calculateIntersectionPointOfCircleAndPoint( const p = calculateIntersectionPointOfCircleAndPoint(
this.p0, this.p0,
this.radius, this.radius,
obj.position canvasPosition
); );
obj.position.copyFrom(p); obj.updatePositionByCanvasPosition(p);
} }
} }
} }

View File

@ -67,15 +67,16 @@ export function recursiveFindChild(
container: Container, container: Container,
finder: (child: DisplayObject) => boolean finder: (child: DisplayObject) => boolean
): DisplayObject | null { ): DisplayObject | null {
let result = null;
for (let i = 0; i < container.children.length; i++) { for (let i = 0; i < container.children.length; i++) {
const child = container.children[i]; const child = container.children[i];
if (finder(child)) { if (finder(child)) {
return child; return child;
} else if (child.children) { } else if (child.children) {
return recursiveFindChild(child as Container, finder); result = recursiveFindChild(child as Container, finder);
} }
} }
return null; return result;
} }
export interface BezierParam { export interface BezierParam {