在画布中拖动对象

发布于 2024-10-09 15:47:04 字数 70 浏览 0 评论 0原文

我正在寻找一种易于使用的方法,将拖动行为分配给画布中的多个对象(图像、形状等)。有谁有好的方法或知道任何用于拖动对象的库?谢谢

Im looking for an easy to use method of assigning drag behavior to multiple objects (images, shapes etc) in canvas. Does anyone have a good way or know of any libraries for dragging objects around? Thanks

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

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

发布评论

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

评论(5

心凉怎暖 2024-10-16 15:47:04

创建您自己的鼠标事件需要一些工作 - 理想情况下您应该创建或使用某种迷你库。我正在考虑在不久的将来创建类似的东西。不管怎样,我在 jsFiddle 上创建了一个拖放演示,展示了如何拖动图像 - 您可以在此处查看

您可以像这样创建可拖动图像:

var myImage = new DragImage(sourcePath, x, y);

如果您对此有任何疑问,请告诉我。希望有帮助。

编辑

拖动多个图像时出现错误。 这是一个新版本

您可能想要检查的另一件事是 easeljs 它有点像 AS3 的风格... mouseEvents 拖动等...

Creating your own mouse events takes a little work - ideally you should either create or use some kind of mini-library. I'm thinking of creating something like this in the near future. Anyway, I created a drag and drop demo on jsFiddle showing how to drag images - you can view it here.

You can create draggable images like this:

var myImage = new DragImage(sourcePath, x, y);

Let me know if you have any questions about this. Hope it helps.

EDIT

There was a bug when dragging multiple images. Here is a new version.

Another thing you might want to check out is easeljs it sort of in the style of AS3... mouseEvents dragging etc...

意中人 2024-10-16 15:47:04

HTML Canvas 与 SVG 或 HTML 不同,使用非保留(或直接)图形 API。这意味着当您在画布上绘制某些内容(例如图像)时,不会保留该内容的知识。唯一剩下的是画布上的像素,与之前的所有像素混合。您无法真正拖动像素的子集;一方面,它们“下方”的像素消失了。您需要做的是:

  1. 跟踪 mousedown 事件并查看它是否位于“正确”的拖动位置。 (您必须跟踪图像/对象的位置并执行鼠标点击检测。)
  2. 当用户拖动鼠标时,从头开始重新绘制整个画布,每次根据之间的偏移在新位置绘制图像当前鼠标位置和初始鼠标按下位置。

我可能建议的一些替代方案:

  • SVG
  • 纯 HTML
  • 多层画布,并将一个透明画布拖到另一个透明画布上。

HTML Canvas 有很多用途。用户与看似不同(但实际上并非如此)的“元素”的交互不是其中之一。

更新:以下是一些显示在画布上拖动的示例:

这些都没有创建一个单独的库来跟踪您的形状然而你。

The HTML Canvas—unlike SVG or HTML—uses a non-retained (or immediate) graphics API. This means that when you draw something (like an image) to the canvas no knowledge of that thing remains. The only thing left is pixels on the canvas, blended with all the previous pixels. You can't really drag a subset of pixels; for one thing, the pixels that were 'under' them are gone. What you would have to do is:

  1. Track the mousedown event and see if it's in the 'right' location for dragging. (You'll have to keep track of what images/objects are where and perform mouse hit detection.)
  2. As the user drags the mouse, redraw the entire canvas from scratch, drawing the image in a new location each time based on the offset between the current mouse location and the initial mousedown location.

Some alternatives that I might suggest:

  • SVG
  • Pure HTML
  • Multiple layered canvases, and drag one transparent canvas over another.

The HTML Canvas is good for a lot of things. User interaction with "elements" that appear to be distinct (but are not) is not one of those things.

Update: Here are some examples showing dragging on the canvas:

None of these have created a separate library for tracking your shapes for you, however.

半山落雨半山空 2024-10-16 15:47:04

KineticJS 就是这样一个 Javascript 库,您可以专门用于动画

这里是链接 html5canvastutorials

KineticJS is one such Javascript Library that u can use exclusively for animations

Heres the Link html5canvastutorials

暮凉 2024-10-16 15:47:04

Canvas 和 jCanvas

你肯定会想看看 jCanvas。它是 Canvas 的一个超级干净的包装器,它在不增加代码复杂性的情况下打开了很多大门。它让这样的事情变得轻而易举。

例如,这里有一个接近您想要的东西的小沙箱,内置了拖动和重绘功能:

在两个元素之间绘制箭头。

绘制箭头

我冒险沿着用 DIV 和 jQuery 做所有事情,但它在交互性和质量上总是达不到要求。

希望能帮助其他人,比如我。

太平绅士

Canvas and jCanvas

You're definitely gonna want to check out jCanvas. It's a super clean wrapper for Canvas, which kicks open a lot of doors without adding code complexity. It makes things like this a breeze.

For example, here's a little sandbox of something close to what you're after, with dragging and redrawing built right in:

Drawing an Arrow Between Two Elements.

Drawing an arrow

I ventured down the road of doing everything with DIVs and jQuery but it always fell short on interactivity and quality.

Hope that helps others, like me.

JP

披肩女神 2024-10-16 15:47:04

当您创建新对象时,无论它们是窗口、卡片、形状还是可拖动的图像,您都可以将它们存储在“当前未选择的对象”数组中。当您单击它们或选择它们或开始拖动它们时,您可以将它们从“未选择的对象”数组中删除。这样,您可以通过检查是否未选择来控制在发生特定 mousedown 事件或 mousemove 事件时可以移动的内容。如果它被选中,它将不会出现在“未选中”数组中,并且您可以在拖动形状时将鼠标指针移动到其他形状上,而不会拖动它们。

创建要拖动的对象数组也有助于层次结构。画布最后绘制属于最前面的对象的像素。因此,如果对象位于数组中,您只需将它们的实例切换为数组中的元素,例如从 objectArray[20] 到 objectArray[4],当您迭代数组并绘制存储在数组元素中的对象时,您可以更改是否其他物体位于其他物体的顶部或后面。

As you create new objects whether they are windows, cards, shapes or images to be draggable, you can store them in an array of "objects currently not selected". When you click on them or select them or start dragging them you can remove them from the array of "objects not selected". This way you can control what can move in the event of a particular mousedown event or mousemove event by checking if it isn't selected. If it is selected it will not be in the "not selected" array and you can move the mouse pointer over other shapes while dragging shapes without them becoming dragged.

Creating arrays of objects you would like to drag also helps with hierarchy. Canvas draws the pixels belonging to the foremost object last. So if the objects are in an array you simply switch their instance as in element in the array say from objectArray[20] to objectArray[4] as you iterate through the array and draw the objects stored in the array elements you can change whether other objects are seen on top or behind other objects.

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