前端使用 Konva 实现可视化设计器(10)- 对齐线

news/2024/9/28 3:30:04

请大家动动小手,给我一个免费的 Star 吧~

大家如果发现了 Bug,欢迎来提 Issue 哟~

github源码

gitee源码

示例地址

不知不觉来到第 10 章了,感觉接近尾声了。。。

对齐线

先看效果:
image

这里交互有两个部分:
1、节点之间的对齐线
2、对齐磁贴

多选的情况下,效果是一样的:
image

主要逻辑会放在控制“选择”的代码文件里:
src\Render\handlers\SelectionHandlers.ts
这里需要一些辅助都定义:

interface SortItem {id?: number // 有 id 就是其他节点,否则就是 选择目标value: number // 左、垂直中、右的 x 坐标值; 上、水平中、下的 y 坐标值;
}type SortItemPair = [SortItem, SortItem]

尝试画个图说明一下上面的含义:

这里以纵向(基于 x 坐标值)为例:

image
这里的 x1~x9,就是 SortItem,横向(基于 y 坐标值)同理,特别地,如果是正在拖动的目标节点,会把该节点的 _id 记录在 SortItem 以示区分。

会存在一个处理,把一个方向上的所有 x 坐标进行从小到大的排序,然后一双一双的遍历,需要符合以下条件“必须分别属于相邻的两个节点”的 SortItem 对,也就是 SortItemPair。

在查找所有 SortItemPair 的同时,只会更新并记录节点距离最短的那些 SortItemPair(可能会存在多个)。

核心逻辑代码:

  // 磁吸逻辑attract = (newPos: Konva.Vector2d) => {// 对齐线清除this.alignLinesClear()// stage 状态const stageState = this.render.getStageState()const width = this.render.transformer.width()const height = this.render.transformer.height()let newPosX = newPos.xlet newPosY = newPos.ylet isAttract = falselet pairX: SortItemPair | null = nulllet pairY: SortItemPair | null = null// 对齐线 磁吸逻辑if (this.render.config.attractNode) {// 横向所有需要判断对齐的 x 坐标const sortX: Array<SortItem> = []// 纵向向所有需要判断对齐的 y 坐标const sortY: Array<SortItem> = []// 选择目标所有的对齐 xsortX.push({value: this.render.toStageValue(newPos.x - stageState.x) // 左},{value: this.render.toStageValue(newPos.x - stageState.x + width / 2) // 垂直中},{value: this.render.toStageValue(newPos.x - stageState.x + width) // 右})// 选择目标所有的对齐 ysortY.push({value: this.render.toStageValue(newPos.y - stageState.y) // 上},{value: this.render.toStageValue(newPos.y - stageState.y + height / 2) // 水平中},{value: this.render.toStageValue(newPos.y - stageState.y + height) // 下})// 拖动目标const targetIds = this.render.selectionTool.selectingNodes.map((o) => o._id)// 除拖动目标的其他const otherNodes = this.render.layer.getChildren((node) => !targetIds.includes(node._id))// 其他节点所有的 x / y 坐标for (const node of otherNodes) {// xsortX.push({id: node._id,value: node.x() // 左},{id: node._id,value: node.x() + node.width() / 2 // 垂直中},{id: node._id,value: node.x() + node.width() // 右})// ysortY.push({id: node._id,value: node.y() // 上},{id: node._id,value: node.y() + node.height() / 2 // 水平中},{id: node._id,value: node.y() + node.height() // 下})}// 排序sortX.sort((a, b) => a.value - b.value)sortY.sort((a, b) => a.value - b.value)// x 最短距离let XMin = Infinity// x 最短距离的【对】(多个)let pairXMin: Array<SortItemPair> = []// y 最短距离let YMin = Infinity// y 最短距离的【对】(多个)let pairYMin: Array<SortItemPair> = []// 一对对比较距离,记录最短距离的【对】// 必须是 选择目标 与 其他节点 成【对】// 可能有多个这样的【对】for (let i = 0; i < sortX.length - 1; i++) {// 相邻两个点,必须为 目标节点 + 非目标节点if ((sortX[i].id === void 0 && sortX[i + 1].id !== void 0) ||(sortX[i].id !== void 0 && sortX[i + 1].id === void 0)) {// 相邻两个点的 x 距离const offset = Math.abs(sortX[i].value - sortX[i + 1].value)if (offset < XMin) {// 更新 x 最短距离 记录XMin = offset// 更新 x 最短距离的【对】 记录pairXMin = [[sortX[i], sortX[i + 1]]]} else if (offset === XMin) {// 存在多个 x 最短距离pairXMin.push([sortX[i], sortX[i + 1]])}}}for (let i = 0; i < sortY.length - 1; i++) {// 相邻两个点,必须为 目标节点 + 非目标节点if ((sortY[i].id === void 0 && sortY[i + 1].id !== void 0) ||(sortY[i].id !== void 0 && sortY[i + 1].id === void 0)) {// 相邻两个点的 y 距离const offset = Math.abs(sortY[i].value - sortY[i + 1].value)if (offset < YMin) {// 更新 y 最短距离 记录YMin = offset// 更新 y 最短距离的【对】 记录pairYMin = [[sortY[i], sortY[i + 1]]]} else if (offset === YMin) {// 存在多个 y 最短距离pairYMin.push([sortY[i], sortY[i + 1]])}}}// 取第一【对】,用于判断距离是否在阈值内if (pairXMin[0]) {if (Math.abs(pairXMin[0][0].value - pairXMin[0][1].value) < this.render.bgSize / 2) {pairX = pairXMin[0]}}if (pairYMin[0]) {if (Math.abs(pairYMin[0][0].value - pairYMin[0][1].value) < this.render.bgSize / 2) {pairY = pairYMin[0]}}// 优先对齐节点// 存在 1或多个 x 最短距离 满足阈值if (pairX?.length === 2) {for (const pair of pairXMin) {// 【对】里的那个非目标节点const other = pair.find((o) => o.id !== void 0)if (other) {// x 对齐线const line = new Konva.Line({points: _.flatten([[other.value, this.render.toStageValue(-stageState.y)],[other.value, this.render.toStageValue(this.render.stage.height() - stageState.y)]]),stroke: 'blue',strokeWidth: this.render.toStageValue(1),dash: [4, 4],listening: false})this.alignLines.push(line)this.render.layerCover.add(line)}}// 磁贴第一个【对】const target = pairX.find((o) => o.id === void 0)const other = pairX.find((o) => o.id !== void 0)if (target && other) {// 磁铁坐标值newPosX = newPosX - this.render.toBoardValue(target.value - other.value)isAttract = true}}// 存在 1或多个 y 最短距离 满足阈值if (pairY?.length === 2) {for (const pair of pairYMin) {// 【对】里的那个非目标节点const other = pair.find((o) => o.id !== void 0)if (other) {// y 对齐线const line = new Konva.Line({points: _.flatten([[this.render.toStageValue(-stageState.x), other.value],[this.render.toStageValue(this.render.stage.width() - stageState.x), other.value]]),stroke: 'blue',strokeWidth: this.render.toStageValue(1),dash: [4, 4],listening: false})this.alignLines.push(line)this.render.layerCover.add(line)}}// 磁贴第一个【对】const target = pairY.find((o) => o.id === void 0)const other = pairY.find((o) => o.id !== void 0)if (target && other) {// 磁铁坐标值newPosY = newPosY - this.render.toBoardValue(target.value - other.value)isAttract = true}}}

虽然代码比较冗长,不过逻辑相对还是比较清晰,找到满足条件(小于阈值,足够近,这里阈值为背景网格的一半大小)的 SortItemPair,就可以根据记录的坐标值大小,定义并绘制相应的线条(添加到 layerCover 中),记录在某个变量中:

  // 对齐线alignLines: Konva.Line[] = []// 对齐线清除alignLinesClear() {for (const line of this.alignLines) {line.remove()}this.alignLines = []}

在适合的时候,执行 alignLinesClear 清空失效的对齐线即可。

接下来,计划实现下面这些功能:

  • 连接线
  • 等等。。。

More Stars please!勾勾手指~

源码

gitee源码

示例地址

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/30379.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

pwn知识——劫持IO-file_jumps攻击和environ攻击

导言 哎,异或fd指针真是令人讨厌 IO_file_jumps _IO_lock_t _IO_stdfile,_IO_wide_data(针对宽字节的虚函数表),_IO_FILE_plus(含有stdin,stdout)三者均被定义为IO_file_jumps 原理 IO_file_jumps是一个全局变量符号,存有以下符号这个结构体主要跟缓冲区有关,比如调用…

【资源分享】野比大雄的生化危机宫格解密工具

一款简单的游戏工具*----------------------------------------------[下载区]----------------------------------------------* 蓝奏云(提取码:ysgg) *----------------------------------------------[下载区]----------------------------------------------**---------…

嵌入式软硬件设计流程

转载自:https://blog.csdn.net/jiangjunjie_2005/article/details/44024933从图书馆看到一经典国外嵌入式设计书籍,其中关于“软硬件设计流程”画得精彩,特列出如下:

WPF 基础、WPF 相关知识、学习、参考项目

前言:最初参加工作时,做过WPF项目 ,后面几年后者虽然有写WPF项目,但多数都是边边角角,写一点满足工作需要。现在写下WPF,主要就是玩一玩,尝试下不同的东西。这是我的代码仓库:地址 (如果对您有帮助,给颗小星星奖励下吧),在WPF/Lesson 10 Practice/Practice/下面。基…

智能工作流:Spring AI高效批量化提示访问方案

集用SpringAI搭建系统,依靠线程池\负载均衡等技术进行请求优化,用于解决科研&开发过程中对GPT接口进行批量化接口请求中出现的问题。大语言模型接口以OpenAI的GPT 3.5为例,JDK版本为17。基于SpringAI搭建系统,依靠线程池\负载均衡等技术进行请求优化,用于解决科研&…

Windows 下 PyTorch 入门深度学习环境安装(CPU版本)

Windows 下 PyTorch 入门深度学习环境安装(CPU版本) 一、安装Anaconda 二、虚拟环境配置 2.1 基础命令 列出虚拟环境 conda env list创建虚拟环境 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main conda create -n 虚拟环境名字 python=版本 -c镜像地址激活环境 co…

PyTorch深度学习快速入门教程

PyTorch深度学习快速入门教程 一、基础知识 1.1 Python学习中的两大法宝1.2 pycharm以及jupyter使用及对比将环境写入Notebook的kernel中: python -m ipykernel install --user --name 环境名称 --display-name "Python (环境名称)" 打开Jupyter notebook,新建Pyth…

wps的VLOOKUP函数只显示公式不显示结果,在公式中已经出现结果了,但在表格中不显示结果

在公式中已经有结果了,但是表格中只显示公式1、 在公式那里点击“显示公式”就可以 2、选中公式列后更改格式