When using the HTML5 drag and drop API, it would appears though the .width
property on has no impact on the width of an image being used as an icon for dragging. Take this fiddle for example: http://jsfiddle.net/cnAHv/7/. You can set dragIcon.width
to anything you want, and it will not change the actual width of the icon.
Most of the resources on the web seem to arbitrarily set dragIcon.width
to 100. Is this just a matter of people copying eachother’s code without checking functionality?
So…
- Is the width property on an image variable actually something that setDragImage() will accept?
- If not, how would you set the width of the icon without manually changing sizing the image in a program like photoshop?
2
Answers
When you use an
<img>
insetDragImage
Javascript will just use the actual image bitmap data, ignoring other attributes likewidth
. Check the specs.However if you do something like this:
http://jsfiddle.net/cnAHv/9/
You will see the drag shadow now contains the bigger image. It occurs because when we use other visible HTML elements (that’s why I appended the DIV to the body), the browser will use its rendered view as a drag image.
This answer might be a bit late but you can also use a canvas to scale your original image.
http://jsfiddle.net/cnAHv/138/
The only downside to this is that the canvas may be tainted if the image doesn’t come from the same origin. More on tainted canvases here