Canvas Drag and Drop : Drag Drop « Development « Flex






Canvas Drag and Drop

Canvas Drag and Drop
          

<!--
Code from Flex 4 Documentation "Using Adobe Flex 4".

This user guide is licensed for use under the terms of the Creative Commons Attribution 
Non-Commercial 3.0 License. 

This License allows users to copy, distribute, and transmit the user guide for noncommercial 
purposes only so long as 
  (1) proper attribution to Adobe is given as the owner of the user guide; and 
  (2) any reuse or distribution of the user guide contains a notice that use of the user guide is governed by these terms. 
The best way to provide notice is to include the following link. 
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/

-->



    <!-- dragdrop\DandDCanvas.mxml -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"
    backgroundColor="white">
    <s:layout>
        <s:VerticalLayout />
    </s:layout>
    <fx:Script> 
         
        import mx.core.DragSource; 
        import mx.managers.DragManager; 
        import mx.events.*; 
        import mx.containers.Canvas; 
        // Initializes the drag and drop operation. 
        private function mouseMoveHandler(event:MouseEvent):void { 
            // Get the drag initiator component from the event object. 
            var dragInitiator:Canvas=Canvas(event.currentTarget); 
            // Get the color of the drag initiator component. 
            var dragColor:int = dragInitiator.getStyle('backgroundColor'); 
            // Create a DragSource object. 
            var ds:DragSource = new DragSource(); 
            // Add the data to the object. 
            ds.addData(dragColor, 'color'); 
            // Call the DragManager doDrag() method to start the drag. 
            DragManager.doDrag(dragInitiator, ds, event); 
        } 
        // Called when the user moves the drag indicator onto the drop target. 
        private function dragEnterHandler(event:DragEvent):void { 
            // Accept the drag only if the user is dragging data 
            // identified by the 'color' format value. 
            if (event.dragSource.hasFormat('color')) { 
                // Get the drop target component from the event object. 
                var dropTarget:Canvas=Canvas(event.currentTarget); 
                // Accept the drop. 
                DragManager.acceptDragDrop(dropTarget); 
            } 
        } 
        // Called if the target accepts the dragged object and the user 
        // releases the mouse button while over the Canvas container. 
        private function dragDropHandler(event:DragEvent):void { 
            // Get the data identified by the color format 
            // from the drag source. 
            var data:Object = event.dragSource.dataForFormat('color'); 
            // Set the canvas color. 
            myCanvas.setStyle("backgroundColor", data); 
        } 
      
    </fx:Script>
    <!--
        A horizontal box with red and green canvases that the user can drag.
    -->
    <mx:HBox>
        <mx:Canvas width="30" height="30" backgroundColor="red"
            borderStyle="solid" mouseMove="mouseMoveHandler(event);" />
        <mx:Canvas width="30" height="30" backgroundColor="green"
            borderStyle="solid" mouseMove="mouseMoveHandler(event);" />
    </mx:HBox>
    <mx:Label text="Drag a color onto the Canvas container." />
    <!-- Handles dragEnter and dragDrop events to allow dropping. -->
    <mx:Canvas id="myCanvas" width="100" height="100"
        backgroundColor="#FFFFFF" borderStyle="solid" dragEnter="dragEnterHandler(event);"
        dragDrop="dragDropHandler(event);" />
    <mx:Button id="b1" label="Clear Canvas"
        click="myCanvas.setStyle('backgroundColor', '0xFFFFFF');" />
</s:Application>

   
    
    
    
    
    
    
    
    
    
  








Related examples in the same category

1.Setting the dragMoveEnabled property to move instead of copySetting the dragMoveEnabled property to move instead of copy
2.Dragging and dropping within a component, to allow orderingDragging and dropping within a component, to allow ordering
3.Create DragSource object and add data to itCreate DragSource object and add data to it
4.Performing a two-way drag and dropPerforming a two-way drag and drop
5.Dragging and dropping in the same controlDragging and dropping in the same control
6.The drag initiators, and drop targetThe drag initiators, and drop target
7.Get Drag source data formatGet Drag source data format
8.Press control key as drag and dropPress control key as drag and drop
9.Specifying the drag indicator by using the DragSpecifying the drag indicator by using the Drag
10.Specify a Drag ProxySpecify a Drag Proxy
11.Enable and Disable Drag OperationsEnable and Disable Drag Operations
12.Drag Drop To ComponentDrag Drop To Component
13.Set liveDragging to true to dispatch change event continuously as moving the thumbSet liveDragging to true to dispatch change event continuously as moving the thumb
14.Get drag source from DragEventGet drag source from DragEvent
15.Canvas drag enter eventCanvas drag enter event
16.Explpicitly handle the dragOver eventExplpicitly handle the dragOver event