84 lines
2.3 KiB
JavaScript
84 lines
2.3 KiB
JavaScript
|
|
import {createTransform, createBoundingRect} from './utils/parser';
|
|
|
|
class TransformHandle {
|
|
constructor(painter) {
|
|
this.$painter = painter;
|
|
|
|
this.parentLevel = painter.getParentLevel();
|
|
|
|
this.rect = { x: 0, y: 0, width: 0, height: 0 };
|
|
this.rectList = [];
|
|
this.transform = [createTransform({ scaleRate: 1, offsetX: 0, offsetY: 0 })];
|
|
}
|
|
|
|
checkVisible(view, rect) {
|
|
// return createBoundingRect(view).intersect(this.rect); // 判断是否相交
|
|
// return createBoundingRect(view).intersect(rect); // 判断是否相交
|
|
const rectCopy = createBoundingRect(view);
|
|
const x = rectCopy.x + rectCopy.width;
|
|
if (x <= rect.width) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 视图进行缩放/平移
|
|
transformView(view) {
|
|
if (view) {
|
|
for (let i = 0; i < this.transform.length; i++) {
|
|
const rect = this.rectList[i];
|
|
if (this.checkVisible(view, rect)) {
|
|
view.transform = this.transform[i];
|
|
view.decomposeTransform(); // 修改 transform 后同步位置
|
|
if (view.screenShow) {
|
|
view.screenShow();
|
|
} else {
|
|
view.show();
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
view.dirty(); // 更新
|
|
}
|
|
}
|
|
|
|
// 处理所有视图缩放/平移
|
|
transformAll() {
|
|
this.traverse(this.transformView, this);
|
|
}
|
|
|
|
// 重新计算显示图形
|
|
revisibleAll() {
|
|
this.traverse(this.transformView, this);
|
|
}
|
|
|
|
// 更新偏移量
|
|
updateTransform(list, rectList) {
|
|
this.rectList = rectList;
|
|
this.transform = [];
|
|
list.forEach(item => {
|
|
this.transform.push(createTransform(item));
|
|
});
|
|
this.transformAll();
|
|
}
|
|
|
|
// 更新画布尺寸
|
|
updateZrSize(opts) {
|
|
this.rect = { x: 0, y: 0, width: opts.width, height: opts.height };
|
|
this.revisibleAll();
|
|
}
|
|
|
|
// 遍历group执行回调
|
|
traverse(cb, context) {
|
|
this.parentLevel.eachChild(level => {
|
|
level.eachChild((view) => {
|
|
cb.call(context, view);
|
|
}, context);
|
|
}, context);
|
|
}
|
|
}
|
|
|
|
export default TransformHandle;
|