简体   繁体   English

拖放图像时如何提取画布上图像的ID?

[英]How to extract ids of images on a canvas when the images are drag and drop?

I have a finite state machine drawing tool and as you can see I have drag and drop for the different images( ie: states and arrows) and for every arrow a div tag is getting created for the transition. 我有一个有限状态机绘图工具,正如您所看到的,我已经拖放了不同的图像(即状态和箭头),并且为每个箭头都为过渡创建了div标签。 Every state, arrow and transition alphabet has an id. 每个状态,箭头和过渡字母都有一个ID。

How can I extract the ids of all images that are present in the canvas when I press a button like save? 当我按下保存按钮时,如何提取画布中存在的所有图像的ID? Unfortunately, I can't save the image of the canvas so I need to someone figure out what all images are there in the canvas and save that information in an array or something upon which I could do some processing on the back end. 不幸的是,我无法保存画布的图像,因此我需要有人找出画布中所有图像的内容,然后将该信息保存为数组或可以在后端进行某些处理的内容。

您需要将实际状态弄糟,不要试图找回渲染结果,这将变得更加困难。

Short answer : You can't 简短答案:您不能

Everything you draw on canvas cannot be "extracted" like moving DOM from one to another using appendChild(). 您在画布上绘制的所有内容都无法像使用appendChild()将DOM从一个移动到另一个那样“提取”。 So the only thing you can do is store the elements id when the drag event on the correspondent element is triggered, and then get all the stored ids when you click the save button. 因此,您唯一可以做的就是在触发对应元素上的拖动事件时存储元素ID,然后在单击“保存”按钮时获取所有存储的ID。


EDIT 编辑

To be able retrieving and maintaining the elements id, you must implement an undo button and validation for input order. 为了能够检索和维护元素id,必须实现撤消按钮和输入顺序验证 Every symbol that you draw on canvas will be exported into an image, hence you can then retrieve the id of individual symbol. 您在画布上绘制的每个符号都会导出到图像中,因此您可以检索单个符号的ID。 The other drawing tools should be used only for highlighting important part of your canvas and shall not affect the finite state element (meaning you will use a different canvas element). 其他绘图工具应用于突出显示画布的重要部分,并且不应影响有限状态元素(这意味着您将使用其他画布元素)。

Here's an example : 这是一个例子:

<div id='input'>
  <button id='state'>State</button>
  <button id='transition' disabled>Transition</button>
  <button id='undo' disabled>Undo</button>
  <span id='info'></span>
</div>

<div id='canvas-container'>
  <canvas id='fsm' width='400' height='200'></canvas>
  <canvas id='preview' width='400' height='200'></canvas>
</div>

<hr/>
<div>History : <span id='history'></span></div>

<div id='limbo'>
  <!-- temporary canvas to get FSM symbol -->
  <canvas id='canvasLimbo' width='50' height='50'></canvas>
</div>

Also, here's the fiddle : https://jsfiddle.net/tmpmachine/dn2p7zj5/172/ 另外,这是小提琴: https : //jsfiddle.net/tmpmachine/dn2p7zj5/172/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM