// import Group from 'zrender/src/container/Group'; import * as matrix from 'zrender/src/core/matrix'; function createTransform(opts) { let transform = matrix.create(); transform = matrix.scale(matrix.create(), transform, [opts.scaleRate, opts.scaleRate]); transform = matrix.translate(matrix.create(), transform, [-opts.offsetX, -opts.offsetY]); return transform; } function createBoundingRect(view) { const rect = view.getBoundingRect(); const scale = view.scale[0]; const offsetX = view.position[0]; const offsetY = view.position[1]; rect.width = rect.width * scale; rect.height = rect.height * scale; rect.x = rect.x * scale + offsetX; rect.y = rect.y * scale + offsetY; return rect; } class TransformHandle { constructor(painter) { this.$painter = painter; this.parentLevel = this.$painter.getParentLevel(); this.rect = { x: 0, y: 0, width: 0, height: 0 }; this.transform = createTransform({ scaleRate: 1, offsetX: 0, offsetY: 0 }); } checkVisible(view) { return createBoundingRect(view).intersect(this.rect); } revisibleView(view) { if (this.checkVisible(view)) { view.show(); } else { view.hide(); } view.dirty(); } // 视图进行缩放/平移 transformView(view) { if (view) { view.transform = this.transform; view.decomposeTransform(); this.revisibleView(view); } return view; } // 处理所有视图缩放/平移 transformAll() { // const parentLevel = this.$painter.getParentLevel(); // if (parentLevel instanceof Group) { this.parentLevel.eachChild((level) => { level.eachChild((view) => { this.transformView(view); }, this); }, this); // } } // 重新计算显示图形 revisibleAll() { // const parentLevel = this.$painter.getParentLevel(); // if (parentLevel instanceof Group) { this.parentLevel.eachChild((level) => { level.eachChild((view) => { this.revisibleView(view); }, this); }, this); // } } // 更新偏移量 updateTransform(opts) { this.transform = createTransform(opts); this.transformAll(); } // 更新画布尺寸 updateZrSize(opts) { this.rect = { x: 0, y: 0, width: opts.width, height: opts.height }; this.revisibleAll(); } } export default TransformHandle;