Drag and drop image on Canvas
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
import mx.managers.DragManager;
import mx.core.DragSource;
import mx.events.DragEvent;
import flash.events.MouseEvent;
[Embed(source='logo.jpg')]
public var globeImage:Class;
private function mouseMoveHandler(event:MouseEvent):void
{
var dragInitiator:Image=Image(event.currentTarget);
var ds:DragSource = new DragSource();
ds.addData(dragInitiator, "img");
DragManager.doDrag(dragInitiator, ds, event);
}
private function dragEnterHandler(event:DragEvent):void {
if (event.dragSource.hasFormat("img")){
DragManager.acceptDragDrop(Canvas(event.currentTarget));
}
}
private function dragDropHandler(event:DragEvent):void {
Image(event.dragInitiator).x = Canvas(event.currentTarget).mouseX;
Image(event.dragInitiator).y = Canvas(event.currentTarget).mouseY;
}
</mx:Script>
<mx:Canvas width="100"
height="100"
backgroundColor="#FF0000"
dragEnter="dragEnterHandler(event);"
dragDrop="dragDropHandler(event);">
<mx:Image id="myimg" source="@Embed(source='logo.jpg')" mouseMove="mouseMoveHandler(event);"/>
</mx:Canvas>
</mx:Application>
Related examples in the same category