Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and drop events in embedded SVG?

Is there any possibility of receiving drag and drop events from SVG elements within a web page?

I tried the Google Closure library, to no avail.

Specifically, suppose my page contains

<ul id = "list">
  <li class="item" id="item1">foo</li>
  <li class="item">bar</li>
  <li class="item">baz</li>
</ul>

And my script contains (Clojurescript/C2)

(let [items (select-all ".item")
      lst (select "#list")
      target (fx/DragDrop. lst nil)]
  (dorun (map
    (fn [item]
      (let [source (fx/DragDrop. item nil)]
        (. source (addTarget target))
        (. source (init))))
    items))
  (. target (init)))

Then I do get a drag image (ghost), although I do not manage to receive drag events e.g. by doing

(on-raw "#item1" :dragstart (fn [e] (.log js/console (str "dragstart " e))))

Using similar code for SVG elements, I do not even get a ghost...

Any hints?

Thanks

like image 643
Rom1 Avatar asked Sep 05 '12 13:09

Rom1


People also ask

Which event is triggered when a draggable object is moved inside an element?

The ondrag event occurs when an element or text selection is being dragged. Drag and drop is a very common feature in HTML5. It is when you "grab" an object and drag it to a different location.


2 Answers

Drag events are not supported on SVG Elements: http://www.w3.org/TR/SVG/svgdom.html#RelationshipWithDOM2Events.

You can fake the drag events with mouse events, see http://svg-whiz.com/svg/DragAndDrop.svg (view the source).

like image 159
methodofaction Avatar answered Oct 25 '22 06:10

methodofaction


You can always implement it. Basically, you have to check if the element is touching another one when you are dragging:

this.isTouching = function(element){
        if(this.x <= element.x && element.x <= (this.x + this.width) && 
           this.y <= element.y && element.y <= (this.y + this.height)){
           return true;
        }else{
            return false;
        }
    };

And it works in all browsers. Hope it helps :)

like image 43
Ricardo Anjos Avatar answered Oct 25 '22 08:10

Ricardo Anjos