首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在画布上寻找定制(加入div并维护它的关系)

在画布上寻找定制(加入div并维护它的关系)
EN

Stack Overflow用户
提问于 2019-02-19 09:30:43
回答 4查看 4.6K关注 0票数 2

我正在寻找一个javascript库或自定义解决方案,在那里我可以自由地拖放组件并维护它们之间的关系(比如哪个节点与什么相连&在我想要的任何地方自由移动节点)

通过维护这种关系,我的意思是不同的组件应该保持它们的互连流(如流程图)。在绘制完它们之后,我需要获得它们之间关系的JSON数据。

下面是我所说的一些例子

在上面的图片中,您可以看到,我有不同的节点,它们是相互关联的。如何通过库或自定义解决方案实现这些功能?

上面的图像来自反应-反应图 react库。我已经尝试过了,但是它使用SVG,并且缺少我想要的大量定制。

我也尝试过rete.js,但无法根据我的需要定制它(定制形状等)。

我也在考虑从头开始构建一个解决方案,我面临的唯一问题是如何在画布上加入两个或多个div来维护它的关系?

备注我为什么要这么做?

  1. 我这样做的目的是要创建一个可视化编辑器,由非技术人员设计流程,然后导出JSON,将其相应地存储在我的数据库中。
  2. 当我再次加载这个流的画布时,我应该能够根据我将拥有的JSON数据再次呈现与连接节点一起的互连流。

如果你遇到这样的情况,你能给我一些建议吗?你们的任何帮助都是非常感谢的。

EN

回答 4

Stack Overflow用户

发布于 2019-02-20 09:39:30

我很想知道更多关于你心目中的布局的信息。

这是一个演示,你可以点击灰色点。当点击2个点时,在svg画布上绘制两个点之间的连接。

在HTML中,所有元素都在一个#wrap元素中。在div下面有一个svg元素,大小与#wrap相同。divs定位为absolute,并以百分比表示顶部和左侧属性。svg画布有一个viewBox="0 0 100 100"preserveAspectRatio="none",以使绘图适应#wrap的大小。连接器是使用fill:nonevector-effect: non-scaling-stroke;在svg上绘制的路径,用于在拉伸或压缩的画布上进行一致的笔画。

最后,您可以为数据保存点数组。

我希望这能给你一个关于你需要做什么的想法。

代码语言:javascript
运行
AI代码解释
复制
const SVG_NS = 'http://www.w3.org/2000/svg';
let mainBox = wrap.getBoundingClientRect();

let dots = Array.from(document.querySelectorAll(".dot"))

let points = [];
let count = 0;

dots.forEach(d=>{
  d.addEventListener("click",(e)=>{
    
    let bcr = d.getBoundingClientRect();
    mainBox = wrap.getBoundingClientRect()
    // calculate the x and y coordinates for the connectors as a number from 0 to 100 
    let x = map(bcr.left - mainBox.left + bcr.width/2, mainBox.left, mainBox.left + mainBox.width, 0, 100);
    let y = map(bcr.top - mainBox.top + bcr.height/2, mainBox.top, mainBox.top + mainBox.height, 0, 100);
    
    points.push({x,y})
    if(count % 2 == 1){
      // connects the last 2 dots in the array
      drawConnector(points[points.length-1],points[points.length-2])
    }    
    count++;
  })
})

function map(n, a, b, _a, _b) {
  let d = b - a;
  let _d = _b - _a;
  let u = _d / d;
  return _a + n * u;
}


function drawConnector(a,b){
  let path = document.createElementNS(SVG_NS, 'path');
  let d = `M${a.x},${a.y} C50,${a.y} 50 ${b.y} ${b.x} ${b.y}`;
  path.setAttributeNS(null,"d",d);
  svg.appendChild(path)
}
代码语言:javascript
运行
AI代码解释
复制
* {
  box-sizing: border-box;
}
.box {
  width: 20%;
  height: 100px;
  border: 1px solid #bbb;
  border-radius: 10px;
  position: absolute;
  background: #efefef;
}

#wrap {
  position: absolute;
  margin:auto;
  top:0;bottom:0;left:0;right:0;
  width: 60%;
  height: 350px;
  border: 1px solid;
  min-width: 350px;
}

svg {
  position: absolute;
  width: 100%;
  height: 100%;
  background: rgba(0, 100, 250, 0.25);
}

.dot {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid #999;
  background: #d9d9d9;
  position: relative;
  left: calc(100% - 10px);
}

.dot:hover {
  border-color: tomato;
}

path {
  fill: none;
  stroke: black;
  vector-effect: non-scaling-stroke;
  stroke-width: 1px;
  stroke: #555;
}
代码语言:javascript
运行
AI代码解释
复制
<div id="wrap">
<svg id="svg" viewBox="0 0 100 100" preserveAspectRatio="none"></svg>  
  
<div class="box" id="a" style="top: 10%; left: 10%;">
  <div class="dot" style="top:20px" ></div>
  <div class="dot" style="top:40px" ></div>
</div>
<div class="box" id="b" style="top: 60%; left: 10%;">
  <div class="dot" style="top:20px" ></div>
  <div class="dot" style="top:40px" ></div>
</div>
<div class="box"  id="c" style="top: 30%; left: 65%; ">
  <div class="dot" style="top:20px; left:-10px" ></div>
  <div class="dot" style="top:40px; left:-10px" ></div>  
</div>
  
</div>

票数 5
EN

Stack Overflow用户

发布于 2019-02-23 13:40:11

Rete.js可以通过自定义Vue.js组件进行自定义。

示例

框架的可视部分由用于呈现的插件之一表示: vue或stage0。我更喜欢Vue.js,所以我开发了基于它的插件。

创建自定义套接字和节点

代码语言:javascript
运行
AI代码解释
复制
var CustomSocket = {
  template: `<div class="socket"
    :class="[type, socket.name, used()?'used':''] | kebab"
    :title="socket.name+'\\n'+socket.hint"></div>`,
  props: ['type', 'socket', 'used']
}


var CustomNode = {
  template,
  mixins: [VueRenderPlugin.mixin],
  methods:{
    used(io){
      return io.connections.length;
    }
  },
  components: {
    Socket: /*VueRenderPlugin.Socket*/CustomSocket
  }
}


class NumComponent extends Rete.Component {

    constructor(){
        super("Number");
        this.data.component = CustomNode;
    }
...

模板:

代码语言:javascript
运行
AI代码解释
复制
  <div class="node" :class="[selected(), node.name] | kebab">
  <div class="title">{{node.name}}</div>
  <div class="content">
    <div class="col" v-if="node.controls.size&gt;0 || node.inputs.size&gt;0">
      <div class="input" v-for="input in inputs()" :key="input.key" style="text-align: left">
        <Socket v-socket:input="input" type="input" :socket="input.socket" :used="() => input.connections.length"></Socket>
        <div class="input-title" v-show="!input.showControl()">{{input.name}}</div>
        <div class="input-control" v-show="input.showControl()" v-control="input.control"></div>
     </div>
     <div class="control" v-for="control in controls()" v-control="control"></div>
    </div>
    <div class="col">
      <div class="output" v-for="output in outputs()" :key="output.key">
        <div class="output-title">{{output.name}}</div>
        <Socket v-socket:output="output" type="output" :socket="output.socket" :used="() => output.connections.length"></Socket>
      </div>
    </div>
  </div> 
</div>

因此,您可以不受限制地自定义节点、连接和背景。

票数 4
EN

Stack Overflow用户

发布于 2019-02-21 02:39:22

您可以使用GOJS

对于商业项目来说,这是一个很好的解决方案。它是灵活的设置,并使它很容易做惊人的事情。

官方网站上的示例。

代码语言:javascript
运行
AI代码解释
复制
function init() {
  if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this
  var $ = go.GraphObject.make; // for conciseness in defining templates

  myDiagram =
    $(go.Diagram, "myDiagramDiv", {
      validCycle: go.Diagram.CycleNotDirected, // don't allow loops
      // For this sample, automatically show the state of the diagram's model on the page
      "undoManager.isEnabled": true
    });

  // This template is a Panel that is used to represent each item in a Panel.itemArray.
  // The Panel is data bound to the item object.
  var fieldTemplate =
    $(go.Panel, "TableRow", // this Panel is a row in the containing Table
      new go.Binding("portId", "name"), // this Panel is a "port"
      {
        background: "transparent", // so this port's background can be picked by the mouse
        fromSpot: go.Spot.Right, // links only go from the right side to the left side
        toSpot: go.Spot.Left,
        // allow drawing links from or to this port:
        fromLinkable: true,
        toLinkable: true
      },
      $(go.Shape, {
          width: 12,
          height: 12,
          column: 0,
          strokeWidth: 2,
          margin: 4,
          // but disallow drawing links from or to this shape:
          fromLinkable: false,
          toLinkable: false
        },
        new go.Binding("figure", "figure"),
        new go.Binding("fill", "color")),
      $(go.TextBlock, {
          margin: new go.Margin(0, 5),
          column: 1,
          font: "bold 13px sans-serif",
          alignment: go.Spot.Left,
          // and disallow drawing links from or to this text:
          fromLinkable: false,
          toLinkable: false
        },
        new go.Binding("text", "name")),
      $(go.TextBlock, {
          margin: new go.Margin(0, 5),
          column: 2,
          font: "13px sans-serif",
          alignment: go.Spot.Left
        },
        new go.Binding("text", "info"))
    );

  // This template represents a whole "record".
  myDiagram.nodeTemplate =
    $(go.Node, "Auto", {
        copyable: false,
        deletable: false
      },
      new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
      // this rectangular shape surrounds the content of the node
      $(go.Shape, {
        fill: "#EEEEEE"
      }),
      // the content consists of a header and a list of items
      $(go.Panel, "Vertical",
        // this is the header for the whole node
        $(go.Panel, "Auto", {
            stretch: go.GraphObject.Horizontal
          }, // as wide as the whole node
          $(go.Shape, {
            fill: "#1570A6",
            stroke: null
          }),
          $(go.TextBlock, {
              alignment: go.Spot.Center,
              margin: 3,
              stroke: "white",
              textAlign: "center",
              font: "bold 12pt sans-serif"
            },
            new go.Binding("text", "key"))),
        // this Panel holds a Panel for each item object in the itemArray;
        // each item Panel is defined by the itemTemplate to be a TableRow in this Table
        $(go.Panel, "Table", {
            padding: 2,
            minSize: new go.Size(100, 10),
            defaultStretch: go.GraphObject.Horizontal,
            itemTemplate: fieldTemplate
          },
          new go.Binding("itemArray", "fields")
        ) // end Table Panel of items
      ) // end Vertical Panel
    ); // end Node

  myDiagram.linkTemplate =
    $(go.Link, {
        relinkableFrom: true,
        relinkableTo: true, // let user reconnect links
        toShortLength: 4,
        fromShortLength: 2
      },
      $(go.Shape, {
        strokeWidth: 1.5
      }),
      $(go.Shape, {
        toArrow: "Standard",
        stroke: null
      })
    );

  myDiagram.model =
    $(go.GraphLinksModel, {
      copiesArrays: true,
      copiesArrayObjects: true,
      linkFromPortIdProperty: "fromPort",
      linkToPortIdProperty: "toPort",
      nodeDataArray: [{
          key: "Record1",
          fields: [{
              name: "field1",
              info: "",
              color: "#F7B84B",
              figure: "Ellipse"
            },
            {
              name: "field2",
              info: "the second one",
              color: "#F25022",
              figure: "Ellipse"
            },
            {
              name: "fieldThree",
              info: "3rd",
              color: "#00BCF2"
            }
          ],
          loc: "0 0"
        },
        {
          key: "Record2",
          fields: [{
              name: "fieldA",
              info: "",
              color: "#FFB900",
              figure: "Diamond"
            },
            {
              name: "fieldB",
              info: "",
              color: "#F25022",
              figure: "Rectangle"
            },
            {
              name: "fieldC",
              info: "",
              color: "#7FBA00",
              figure: "Diamond"
            },
            {
              name: "fieldD",
              info: "fourth",
              color: "#00BCF2",
              figure: "Rectangle"
            }
          ],
          loc: "280 0"
        }
      ],
      linkDataArray: [{
          from: "Record1",
          fromPort: "field1",
          to: "Record2",
          toPort: "fieldA"
        },
        {
          from: "Record1",
          fromPort: "field2",
          to: "Record2",
          toPort: "fieldD"
        },
        {
          from: "Record1",
          fromPort: "fieldThree",
          to: "Record2",
          toPort: "fieldB"
        }
      ]
    });
}

init();
代码语言:javascript
运行
AI代码解释
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/gojs/2.0.3/go.js"></script>
<div id="sample">
  <div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:300px"></div>
</div>

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54771893

复制
相关文章
Keras 模型多输出 loss weight metrics 设置
keras支持模型多输入多输出,本文记录多输出时loss、loss weight和metrics的设置方式。 模型输出 假设模型具有多个输出 classify: 二维数组,分类softmax输出,需要配置交叉熵损失 segmentation:与输入同尺寸map,sigmoid输出,需要配置二分类损失 others:自定义其他输出,需要自定义损失 具体配置 model 变量均为模型中网络层 inputs = [input_1 , input_2] outputs = [classify, segm
为为为什么
2022/08/05
5770
R语言深度学习Keras循环神经网络(RNN)模型预测多输出变量时间序列
递归神经网络被用来分析序列数据。它在隐藏单元之间建立递归连接,并在学习序列后预测输出。 在本教程中,我们将简要地学习如何用R中的Keras RNN模型来拟合和预测多输出的序列数据,你也可以对时间序列数据应用同样的方法。我们将使用Keras R接口在R中实现神经网络:
拓端
2021/09/30
2.2K0
R语言深度学习Keras循环神经网络(RNN)模型预测多输出变量时间序列
预测金融时间序列——Keras 中的 MLP 模型
本文的目的是展示使用时间序列从数据处理到构建神经网络和验证结果的过程。作为一个例子,金融系列被选择为完全随机的,一般来说,如果传统的神经网络架构能够捕获必要的模式来预测金融工具的行为,那就很有趣了。
磐创AI
2021/11/11
5.4K1
预测金融时间序列——Keras 中的 MLP 模型
Keras学习(一)—— Keras 模型(keras.model): Sequential 顺序模型 和 Model 模型
首先了解Keras的一个很好的途径就是通过 文档 Keras 中文文档地址: https://keras.io/zh/models/about-keras-models/
全栈程序员站长
2022/11/15
1.6K0
Keras模型
文件下载:https://download.csdn.net/download/sxf1061700625/19229828
小锋学长生活大爆炸
2021/06/11
5270
Keras保存模型
一、不保存模型只显示大概结构 model.summary() 这个函数会打印模型结构,但是仅仅是打印到控制台。
润森
2019/11/04
1.1K0
9种平台帮助你深度学习Keras
Keras是一个Python深度学习库,它可以使用高效的Theano或TensorFlow符号数学库作为后端。同时,Keras很容易使用,你可以在几分钟内开发出你的第一个多层感知器,卷积神经网络,或者
AiTechYun
2018/03/02
8390
9种平台帮助你深度学习Keras
6 种用 LSTM 做时间序列预测的模型结构 - Keras 实现
LSTM(Long Short Term Memory Network)长短时记忆网络,是一种改进之后的循环神经网络,可以解决 RNN 无法处理长距离的依赖的问题,在时间序列预测问题上面也有广泛的应用。
杨熹
2019/04/07
10.4K1
Keras 实现 LSTM时间序列预测
本文将介绍如何用 keras 深度学习的框架搭建 LSTM 模型对时间序列做预测。
机器学习AI算法工程
2019/10/28
2.4K1
Keras 学习笔记(三)Keras Sequential 顺序模型
你可以通过将网络层实例的列表传递给 Sequential 的构造器,来创建一个 Sequential 模型:
种花家的奋斗兔
2020/11/12
2.4K0
根据序列,进行中后序列输出
#include #include #include #include typedef struct BiTNode {//二叉树结点 char data; //数据 struct BiTNode* lchild, * rchild; //左右孩子指针 } BiTNode, * BiTree; int nn = 0; int CreateBiTree(BiTree* T) {//按先序序列创建二叉树 char data; sc
川川菜鸟
2021/10/18
2570
序列生成模型(一):序列概率模型
  序列数据在深度学习应用中非常常见,它们是按照时间顺序或者其他顺序排列的数据集合。序列数据的处理通常涉及到捕捉数据中的时间关系、趋势和模式,因此需要使用专门的模型来处理这些信息。以下是一些常见的序列数据类型以及相应的深度学习应用:
Qomolangma
2024/07/30
2380
序列生成模型(一):序列概率模型
可视化Keras模型
您是否曾经想过您的神经网络实际上是如何连接不同的神经元的?如果您可以可视化所设计的模型架构,那不是很好吗?如果您可以将模型架构下载为演示时可以使用的图像,那不是很好吗?如果所有这些都为“是”,那么您来对地方了。 在本文中,我将向你展示一个Ë xciting Python包/模块/库,可用于可视化Keras模型。无论是卷积神经网络还是人工神经网络,该库都将帮助您可视化所创建模型的结构。 Keras Visualizer是一个开源python库,在可视化模型如何逐层连接方面确实很有帮助。因此,让我们开始吧。
致Great
2021/02/25
1.5K0
可视化Keras模型
C#序列化反序列化帮助类
//转载:http://hi.baidu.com/fxh19860822/blog/item/df35230b3ded441495ca6bd5.html 在C#中常见的序列化的方法主要也有三个:BinaryFormatter、SoapFormatter、XML序列化 /// <summary> /// 提供序列化和反序列化对象的相关静态方法。 /// </summary> public class SerializerHelper { ///
跟着阿笨一起玩NET
2018/09/18
1.6K0
Keras多输入模型实例
一般情况下,利用Keras建立模型,会使用线性模型(Sequential),但是在一些特殊情况下,我们或许会有多个input,这样的话,我们就不会使用线性模型,而使用Keras的Model。
Ziyue
2020/01/15
2.4K0
Keras多输入模型实例
keras系列︱Sequential与Model模型、keras基本结构功能(一)
该文章介绍了在深度学习模型中,不同的层对输入进行计算,从而影响模型的性能。文章详细讨论了卷积层、池化层、全连接层和LSTM层的特点和作用,以及如何使用这些层来构建高性能的模型。此外,文章还探讨了如何通过冻结层和重新训练层来提高模型的性能。
悟乙己
2018/01/02
10.2K0
keras系列︱Sequential与Model模型、keras基本结构功能(一)
理解keras中的sequential模型
keras中的主要数据结构是model(模型),它提供定义完整计算图的方法。通过将图层添加到现有模型/计算图,我们可以构建出复杂的神经网络。
云水木石
2019/07/02
3.7K0
keras中文-快速开始Sequential模型
模型需要知道输入数据的shape,因此,Sequential的第一层需要接受一个关于输入数据shape的参数,后面的各个层则可以自动的推导出中间数据的shape,因此不需要为每个层都指定这个参数。有几种方法来为第一层指定输入数据的shape
CreateAMind
2018/07/25
9420
keras中文-快速开始Sequential模型
Keras中的多变量时间序列预测-LSTMs
神经网络诸如长短期记忆(LSTM)递归神经网络,几乎可以无缝地对多变量输入问题进行建模。
朱卫军 AI Python
2022/04/02
3.2K0
Keras中的多变量时间序列预测-LSTMs
python在Keras中使用LSTM解决序列问题
时间序列预测是指我们必须根据时间相关的输入来预测结果的问题类型。时间序列数据的典型示例是股市数据,其中股价随时间变化。
拓端
2020/09/26
3.6K0

相似问题

Keras加载的模型输出与训练模型输出不同。

10

Keras序列模型返回损失'nan‘

30

Keras模型的输出训练参数

10

产生相同输出的Keras模型

50

Keras中间层(注意模型)输出

10
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档