0

i have a requirement to have rectangle shape of fixed size and must be draggable to container, so that i can connect them.

i have searched alot but did not find any solution.

Question: single rectangle shape(fixed size) should be dragged to container

For full view Code pen:https://codepen.io/eabangalore/pen/LvzXxX

here is how:

enter image description here

Code Demo:

<!--
  $Id: helloworld.html,v 1.6 2013/10/28 08:44:54 gaudenz Exp $
  Copyright (c) 2006-2013, JGraph Ltd
  
  Hello, World! example for mxGraph. This example demonstrates using
  a DOM node to create a graph and adding vertices and edges.
-->
<html>
<head>
 <title>Hello, World! example for mxGraph</title>

 <!-- Sets the basepath for the library if not in same directory -->
 <script type="text/javascript">
  mxBasePath = 'https://jgraph.github.io/mxgraph/javascript/src';
 </script>

 <!-- Loads and initializes the library -->
 <script type="text/javascript" src="https://jgraph.github.io/mxgraph/javascript/src/js/mxClient.js"></script>

 <!-- Example code -->
 <script type="text/javascript">
  // Program starts here. Creates a sample graph in the
  // DOM node with the specified ID. This function is invoked
  // from the onLoad event handler of the document (see below).
  function main(container)
  {
   // Checks if the browser is supported
   if (!mxClient.isBrowserSupported())
   {
    // Displays an error message if the browser is not supported.
    mxUtils.error('Browser is not supported!', 200, false);
   }
   else
   {
    // Disables the built-in context menu
    mxEvent.disableContextMenu(container);
    
    // Creates the graph inside the given container
    var graph = new mxGraph(container);

    // Enables rubberband selection
    new mxRubberband(graph);
    
    // Gets the default parent for inserting new cells. This
    // is normally the first child of the root (ie. layer 0).
    var parent = graph.getDefaultParent();
        
    // Adds cells to the model in a single step
    graph.getModel().beginUpdate();
    try
    {
     var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30);
     var v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
     var e1 = graph.insertEdge(parent, null, '', v1, v2);
    }
    finally
    {
     // Updates the display
     graph.getModel().endUpdate();
    }
   }
  };
 </script>
</head>

<!-- Page passes the container for the graph to the program -->
<body onload="main(document.getElementById('graphContainer'))">

 <!-- Creates a container for the graph with a grid wallpaper -->
 <div id="graphContainer"
  style="position:relative;overflow:scroll;width:321px;height:241px;background:url('https://jgraph.github.io/mxgraph/javascript/examples/editors/images/grid.gif');cursor:default;">
 </div>
</body>
</html>

Please help me thanks in advance!!!!!

EaBengaluru
  • 131
  • 2
  • 17
  • 59

2 Answers2

0

You can try something like

$(".rectagle").draggable({helper: 'clone'});

$("#canvas").droppable({
    accept: ".rectagle",
    drop: function(ev,ui){
        $(ui.draggable).clone().appendTo(this);
    }
});
#canvas {
    height: 100px;
    border: 1px solid lightgrey;
}
.rectagle {
    display: inline-block;
    height: 50px;
    width: 50px;
    border: 1px solid lightblue;
    text-align: center;
    background-color: lightgreen;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<div class="rectagle"></div>
<div id="canvas"></div>
NPE
  • 429
  • 2
  • 16
0
function addToolbarItem(graph, toolbar, prototype, image)
     {
        // Function that is executed when the image is dropped on
        // the graph. The cell argument points to the cell under
        // the mousepointer if there is one.
        var funct = function(graph, evt, cell)
        {
            graph.stopEditing(false);
            var pt = graph.getPointForEvent(evt);
            var vertex = graph.getModel().cloneCell(prototype);
            vertex.geometry.x = pt.x;
            vertex.geometry.y = pt.y;

            graph.setSelectionCells(graph.importCells([vertex], 0, 0, cell));
        }
        // Creates the image which is used as the drag icon (preview)
        var img = toolbar.addMode(null, image, funct);
        mxUtils.makeDraggable(img, graph, funct);
    }

try this example