


For the sake of this example dropping a circle into an area will just change the color, but depending on the project dropping a display object can do all sorts of things. Now for a canvas drag and drop example where dropping a display object is a certain area does something.

There is still a bit more to click in drag with canvas though such as snapping things into place. A single circle at the center of the center of the canvas, and I can then click and drag the circle to any location on the canvas. Once this is all up and running the result is what I would expect. I will not be getting into this method in detail here as I have wrote a post on this subject before hand. This variant of the method seems to work okay for both mouse and touch events just fine for this project at least sense I am not interested in supporting multi touch. So at the start of my basic.js file I have my get canvas relative method that is used to get a canvas relative position from an event object. 1.1 - The get canvas relative method and distance formula The example will involve the use of a method that I worked out for another canvas post of mine that has to do with getting a canvas relative point from and event object, and also the distance formula that will be used for collision detection. This will just be a canvas example where at the center of the canvas there will be a circle, and the circle can just be clicked and dragged around the canvas, thats it nothing special. So in this section I will be going over just a basic canvas drag example. However in this post I am going to be witting about doing this sort of things and related topics with just plain old vanilla javaScript and canvas elements. However the basics of getting started with canvas drag and drop is not so hard, and if things get to intense there is always just going with a framework such as phaser that has all this worked out well and much more. There is more to it then just start though, there are other talking points when it comes to snapping things to a grid or not, and creating user defined events for certain things that are a result of drag and drop. Finally there will be a pointer end event that just sets the boolean back to false leaving the dragged display object at its new location. In the body of the pointer move event I would then set the position of the display object to the canvas relative point that is obtained within the event object of the move event. If an object has been clicked then I could set a boolean value for that display object to true that would then used in the logic of a pointer move event. Then the nest step might be to use that to find out if a display object was clicked or not with some kind of collision detection method such as bounding box. The process of making a canvas drag of sorts might involve first getting a canvas relative point in a pointer event such as mouse down or a touch start event when it comes to touch events. There are ways of dragging whole elements when it comes to client side javaScript in general, but in this post I will be writing about dragging a display object in the canvas which is a little different from that as it just deals with canvas elements alone.

In canvas drag and drop actions are part of many projects when working out a user interface for a project.
