Vue.draggable: setDragImage doesnt work

Created on 29 Jan 2018  路  3Comments  路  Source: SortableJS/Vue.Draggable

Same issue as

https://github.com/RubaXa/Sortable/issues/146

Example

https://kryogenix.org/code/browser/custom-drag-image.html

Step by step scenario

Actual Solution

Expected Solution

This should work

<draggable
          v-model="items"
          @start="modify"...

modify (e) {
        let dragIcon = document.createElement('img')
        dragIcon.src = 'https://www.google.no/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'
        e.dataTransfer.setDragImage(dragIcon, 0, 0)
      }

Most helpful comment

Sorry, it can be done with setData method ^^

Accepted types:

https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/Recommended_drag_types

Example:

<draggable :options="{setData: modifyDragItem}"></draggable>
modifyDragItem (dataTransfer) {
        let img = new Image()
        img.src = 'https://www.google.no/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'
        dataTransfer.setDragImage(img, 0, 0)
      }

All 3 comments

Sorry, it can be done with setData method ^^

Accepted types:

https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/Recommended_drag_types

Example:

<draggable :options="{setData: modifyDragItem}"></draggable>
modifyDragItem (dataTransfer) {
        let img = new Image()
        img.src = 'https://www.google.no/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'
        dataTransfer.setDragImage(img, 0, 0)
      }

To achieve this in v2.20 or higher where options prop is deprecated, use setData directly, like:

<draggable :setData="modifyDragItem"></draggable>

JS part remains the same.

<draggable 
    v-model="actionClones"
    :setData="customDragEl"
    :group="groupName" 
    handle=".move-handle" 
    @start="startDrag" 
    @end="endDrag">
</draggable>

when I used this method, the draggable crashes after customDragEl and endDrag didn't get executed.

Edit: my workaround was to change the dragImage on @dragStart on each draggable item

<draggable v-model="items" :group="groupName" handle=".move-handle" @end="endDrag">
    <div
        v-for="nth in items"
        :key="nth.id"
        :item_id="nth.id"
        @dragstart="rewriteDragImg(nth, $event)"
      >
    ...
</draggable>
<script>
...
    rewriteDragImg(item, event) {
      event.dataTransfer.setDragImage(event.originalTarget, 200, 0);
    },
...
</script>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

patpaskoch picture patpaskoch  路  4Comments

Leadaxe picture Leadaxe  路  3Comments

AnnaStuehlmeyer picture AnnaStuehlmeyer  路  3Comments

Kgwkgwkgw picture Kgwkgwkgw  路  3Comments

Sualty picture Sualty  路  3Comments