Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何拾取某个元素,然后根据其坐标将整个图形移到屏幕视野的正中央? #361

Open
Wang-Shuo-1991 opened this issue Feb 19, 2025 · 2 comments

Comments

@Wang-Shuo-1991
Copy link

目前接触的项目用到的leafer是比较早期的版本: 1.0.0.0-beta.3. 文档中很多api可能已经删除了,重构也不现实!

我在x轴方向上绘制了很多线段(超出了可视区域),然后将这些线段添加到一个group内。我现在想要选择某个线段,然后希望整个视图(可视区域)移到我所选择的那个线段的地方。就像是地图功能一样,选择了某个城市,整个地图跳转到这个城市的地方。示意图如下:

Image

我现在的做法如下:
` // 计算线段的中心点(本地坐标系)
const centerXLocal = (line.x1 + line.x2) / 2;
const centerYLocal = (line.y1 + line.y2) / 2;

            // 将本地坐标系的中心点转换为世界坐标系
            const worldCenter = group.localToWorld({ x: centerXLocal, y: centerYLocal });
            const centerXWorld = worldCenter.x;
            const centerYWorld = worldCenter.y;

            // 获取编辑器容器的宽度和高度
            const containerWidth = leafer.width;
            const containerHeight = leafer.height;

            // 获取当前缩放比例
            const scale = leafer.scale;

            // 计算需要移动的距离
            const dx = (containerWidth / 2 - centerXWorld) / scale;
            const dy = (containerHeight / 2 - centerYWorld) / scale;

            // 移动组
            group.moveBy(dx, dy);`

但是这么做还是不准确,我知道可能是坐标方面出现了问题,但是不知道怎么处理?

@leaferjs
Copy link
Owner

看一下 @lafer-in/view 插件的源码,包含这个功能,自己可以实现一下

https://www.leaferjs.com/ui/plugin/in/view/zoom.html#%E8%81%9A%E7%84%A6%E5%88%B0%E6%8C%87%E5%AE%9A%E5%85%83%E7%B4%A0

@leaferjs
Copy link
Owner

建议尝试升级一下,更新日志里有注明api的变化

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants