如何从一个 HTML5 画布拖放到另一个画布

发布于 2024-10-10 08:33:57 字数 113 浏览 1 评论 0原文

我试图弄清楚如何将图像从一个画布拖放到另一个画布。假设画布彼此相邻,是否可以无缝地将某些东西拖过边界?如果不是,将 div 拖到画布上,获取其 ID,然后通过响应画布上的 mouseup 位置来放置它是不是更好?

I'm trying to figure out how to drag and drop an image from one canvas to another canvas. Assuming the canvases are next to each other, would it be possible to seamlessly drag something across the border? If not, is it a better idea to drag a div over the canvas, get its ID, and place it by responding to the mouseup location on the canvas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雅心素梦 2024-10-17 08:33:57

您不能在画布上拖动项目。画布是非保留模式(或立即模式) 图形 API。您发出绘制命令并获得像素。模拟拖动包括跟踪用户的鼠标移动,并选择使用不同的参数重复清除和重新绘制画布,以使像素的某些子集看起来像一个粘性对象一样移动。

将此与 HTML 或 SVG 进行对比,在 HTML 或 SVG 中,您实际上可以更改真实 DOM 对象的位置/变换属性,并且当文档的视觉表示自动更新时,手表

会自动更新。如果您有两个画布并想要从中拖动某些内容一对一,我要做的是:

  1. 在“菜单”画布上按下鼠标时,以编程方式创建一个与对象一样大的新画布,然后(使用绝对CSS定位)将其放置在用户单击的项目的顶部在。
  2. 将项目绘制到画布上。
  3. 跟踪文档上的 mousemove 事件,并更新画布相对于鼠标的位置。
  4. 当用户在目标画布上释放鼠标时,丢弃(或隐藏)微小的“拖动”画布,并使用拖动到适当位置的项目重新绘制主画布。

不过,我在这里可能真正要做的是使用 SVG。 ;)

You don't drag items on a canvas. A canvas is a non-retained mode (or immediate mode) graphics API. You issue draw commands and you get pixels. Simulating dragging is comprised of tracking the user's mouse movements and choosing to repeatedly clear and re-draw the canvas with different parameters to make some subset of the pixels appear to move as a cohesive object.

Contrast this with HTML or SVG where you actually change position/transform properties of a real DOM object and the watch as the visual representation of your document updates automatically.

If you have two canvases and want to drag something from one to the other, what I would do is:

  1. On mouse down on the 'menu' canvas, create a new canvas programmatically just as large as the object, and (using absolute CSS positioning) place it over top of the item the user clicked on.
  2. Draw the item onto that canvas.
  3. Track the mousemove event on the document, and update the position of the canvas relative to the mouse.
  4. When the user releases the mouse over the destination canvas, throw away (or hide) your tiny 'dragging' canvas, and re-draw the main canvas with the item that was dragged in the appropriate location.

Though, what I'd probably really do here is use SVG. ;)

决绝 2024-10-17 08:33:57

检查答案。
它用于多选拖放下降,但也许会有用。

Check this answer.
It is for multiple select drag & drop, but maybe will be useful.

少女净妖师 2024-10-17 08:33:57

为什么这需要 2 个画布?画布是您的绘图区域,您可以控制它。为什么需要2个?

Why does this need to be 2 canvases? The canvas is your drawing area, you control it. Why do you need 2?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文