框架代码同步

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;
};
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() {
return GraphicTransform.fromObject(this);
};

View File

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

View File

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

View File

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