The Wayback Machine - https://web.archive.org/web/20210531112109/https://github.com/dotnet/aspnetcore/issues/33158
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Not a function" when calling setDragImage with Drag & Drop #33158

Open
rogihee opened this issue May 31, 2021 · 0 comments
Open

"Not a function" when calling setDragImage with Drag & Drop #33158

rogihee opened this issue May 31, 2021 · 0 comments

Comments

@rogihee
Copy link

@rogihee rogihee commented May 31, 2021

I have a div that I want to give a custom draggable image (which can be an image or any element):
https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/setDragImage

I have a div I want to drag:

<div class="my-dragging-div"
     draggable="true"
     @ref="@DragElement"
     ondragover="event.preventDefault();"
     @ondragstart="@OnDragStart"
     @ondragend="@OnDragEnd">
</div>

The callback is:

private async Task OnDragStart(DragEventArgs args)
{
    await JSRuntime.InvokeVoidAsync("setDragImage", args, DragElement, 0, 0);
}

This JavaScript function is:

setDragImage(event: DragEvent, element: HTMLElement, xOffset: number = 0, yOffset: number = 0): void {
        event.dataTransfer.setDragImage(element, xOffset, yOffset);
}

When calling the DataTransfer.setDragImage JavaScript function via JSRuntime via a @ondragstart Blazor callback, and passing a reference to the DragEventArgs I get an error: event.dataTransfer.setDragImage is not a function.

Microsoft.JSInterop.JSException: event.dataTransfer.setDragImage is not a function

Apparantly, when the DragEventArgs is serialized / deserialized the function pointers are lost.

Please note that on Windows drag image elements with a width over 300px get a gradient overlay that is quite ugly depending on your underlying div and your personal feelings for aesthetics :-). Thus, for our solution we ended up with a completely custom implementation with a cloned ghost element that is following the mouse position. With a custom javascript event subscription we clear the dragImage:

clearDragImage(event: DragEvent): void {
        const img: HTMLImageElement = new Image();
        img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=";
        event.dataTransfer.setDragImage(img, 0, 0);
 };

Perhaps this information is useful when working on issue #18754.

Further technical details

  • ASP.NET Core version: 5.0.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant