Makes an HTML node draggable. When a node is draggable, the user can drag it over their design and, when they drop it, the app will insert a certain type of element.
In most cases, apps don't need to use this method directly. Instead, we recommend using one of the pre-built components in the starter kit, which are built on top of this method:
Usage
import { getDragAndDrop } from "@canva/dnd";const dragAndDrop = getDragAndDrop();const node = document.querySelector("#some-node");dragAndDrop.makeDraggable({node,dragData: {type: "TEXT",children: ["Hello world"],},onDragStart: () => {console.log("The drag event has started");},onDragEnd: () => {console.log("The drag event has ended");},});
Parameters
The expected parameters depend on the type of content that is to be added to the user's design at the end of a drag event:
- Images
- Text
- Videos
When adding images to a design, the image data can come from a data URL or an external URL. This choice affects the parameters expected by the makeDraggable
method.
- Data URLs
- External URLs
If the image data comes from a data URL, use the following properties:
Options for configuring the behavior of the makeDraggable
method.
object
The node to make draggable.
HTMLElement
A callback function that runs at the start of a drag event.
A use-case of this function is to hide the draggable node at the start of a drag event. This creates the illusion of the node being moved, rather than copied, and is consistent with how draggable nodes work throughout the native Canva experience.
function
A callback function that runs at the end of a drag event. This function always runs at the end of a drag event, regardless of whether or not the user successfully drops something into their design.
function
Options for configuring the element that's added to the user's design at the end of a drag event.
object
The type of element to add to the user's design. To add an image, this must be "IMAGE"
.
string
The URL or data URL of the full-size image that will be added to the user's design at the end of the drag event.
string
The dimensions of the drag preview. The dimensions should have the same aspect ratio as the full-size image.
object
The width of the drag preview, in pixels.
number
The height of the drag preview, in pixels.
number
The dimensions of the full-size image. If omitted, the dimensions of the drag preview will be used.
object
The width of the full-size image, in pixels.
number
The height of the full-size image, in pixels.
number
The URL or data URL of the image to use for the drag preview. This image will appear under the user's cursor as they during the drag event. If omitted, the full-size image will be used.
string
Returns
This method doesn't return anything.