/Raphaelle

Drag and drop for Raphaël objects

Primary LanguageJavaScript

Drag and drop for Raphael elements.

Original Author: Gabe Hollombe. Rewritten several times over by Clifford Heath.
(c) Copyright. Subject to MIT License.

Just call some_raphael_object.draggable() and you're done.
Or pass options and/or add methods to customise the behaviour.
You need to include jquery and Raphael first.

When you call draggable() on any Raphael object, it becomes a handle that can start a drag.
A mousedown on the handle followed by enough motion causes a drag to start.

The dragged object may be the handle itself, another object defined in the options to
draggable(), or an object returned from the handle's dragStart method (if defined).

The drag proceeds by calling drag_obj.dragUpdate for each mouse motion, see below.
If no dragUpdate is defined, a Raphael translate() is used to provide simple motion.

If the Escape key is pressed during the drag, the motion is reverted and the drag stops.
In this case, dragCancel will be called, see below.

When a drag finished normally, dragFinish is called as defined below.

Options:

  drag_obj
	A click on the handle will start a drag on this object.
	Otherwise handle will be dragged, unless handle.dragStart()
	returns a different draggable object.

  right_button
	unset means drag using either button, otherwise false/true means left/right only.

  reluctance
	Number of pixels of motion before a drag starts (default 3).

Optional method on handle. The events are passed so you can see the modifier keys.

  dragStart(x, y, mousedownevent, mousemoveevent)
	called (after reluctance) with the mousedown event and canvas location.
	Must return the object to drag. Good place to call toFront so the drag_obj doesn't hide.

Optional methods on drag_obj. The event is passed so you can see the modifier keys.

  dragUpdate(dragging_over, dx, dy, event)
	dragging_over:	the object under the cursor (after hiding drag_obj)
	dx,dy:		the number of units of motion
	event:		the mousemotion event

  dragFinish(dropped_on, x, y, event)
	dropped_on:	the object under the cursor (after hiding drag_obj)
	x,y:		the page location
	event:		the mouseup event

  dragCancel()
	called if the drag is cancelled

The escape key can be used to cancel a drag and revert the motion.

Simple example:

      paper = Raphael($("notepad"), 200, 200);
      rect = paper.rect(5, 10, 60, 20, 10).attr({stroke: "#009", "stroke-width": 3});
      rect.draggable();

If the object supports an dragUpdate method, that will be called with the
x and y deltas and the event for each mouse move while dragging.

Example (resize the rectangle instead of dragging it):

      paper = Raphael($("notepad"), 200, 200);
      rect = paper.rect(5, 10, 60, 20, 10).attr({stroke: "#009", "stroke-width": 3});
      rect.draggable({reluctance: 10});

      rect.dragUpdate = function(dragging_over, dx, dy, event) {
        rect.attr({
          width: rect.attrs.width + dx,
          height: rect.attrs.height + dy
        });
      };

See example.html for further examples.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.