| Start |
|---|
| How shapes & images REALLY(!) "move" on the Canvas A problem: Canvas only remembers pixels, not shapes or images This is an image of a circular beach ball, and of course, you can't drag the ball around the image. It may surprise you that just like an image, if you draw a circle on a Canvas you cannot drag that circle around the canvas. That's because the canvas won't remember where it drew the circle. What the Canvas DOESN'T know...
What the Canvas DOES know... Canvas knows the color of every pixel on it's drawing surface. The canvas can tell you that at x,y==[20,30] there is a blue pixel, but it does not know if this blue pixel is part of a circle. What this means... This means everything drawn on the Canvas is permanent: immovable and unchangeable.
But Canvas can give the I-L-L-U-S-I-O-N of movement Canvas can give the illusion of movement by continuously erasing the circle and redrawing it in a different position. By redrawing the Canvas many times per second, the eye is fooled into seeing the circle move across the Canvas. This code gives the illusion of movement by continuously redrawing a circle in new positions. |
Dragging circles & rectangles around the Canvas What is a "Shape"?You typically save your shapes by creating a JavaScript "shape" object representing each shape. Of course, you're not really saving shapes. Instead, you're saving the definition of how to draw the shapes. Then put every shape-object into an array for easy reference. Using mouse events to do DraggingDragging a shape or image requires responding to these mouse events: On mousedown: Test if any shape is under the mouse. If a shape is under the mouse, the user is intending to drag that shape. So keep a reference to that shape and set a true/false On mousemove: Calculate the distance that the mouse has been dragged since the last On mouseup or mouseout: The user is intending to stop the drag operation, so clear the "isDragging" flag. Dragging is completed. Demo: Dragging circles & rectangles on the canvasThis demo drags circles & rectangles on the canvas by responding to mouse events and giving the illusion of movement by clearing and redrawing. |
| Dragging irregular shapes around the Canvas Most Canvas drawings are either rectangular (rectangles, images, text-blocks) or circular (circles). Circles & rectangles have mathematical tests to check if the mouse is inside them. This makes testing circles and rectangles easy, quick and efficient. You can "hit-test" hundreds of circles or rectangles in a fraction of a second. You can also drag irregular shapes. But irregular shapes have no quick mathematical hit-test. Fortunately, irregular shapes do have a built-in hit-test to determine if a point (mouse) is inside the shape: One requirement when using The irregular shape doesn't need to be as common as the everyday triangle. You can also hit-test any wildly irregular Paths. This annotated example shows how to drag irregular Path shapes as well as circles and rectangles: |
| Dragging images around the Canvas See this Example (opens new window) for a general explanation of dragging Shapes around the Canvas. This annotated example shows how to drag images around the Canvas |
Read Article
https://develop.horje.com/learn/1434/reference