【问题标题】:move multiple images from one canvas to other canvas将多个图像从一个画布移动到另一个画布
【发布时间】:2013-06-23 03:38:13
【问题描述】:

http://www.rgraph.net/blog/2013/january/an-example-of-html5-canvas-drag-n-drop.html有一个例子

我无法将多个图像添加到画布 1 并将这些添加的图像移动到画布 2。此外,我应该能够在第二个画布中拖动(在画布 2 中移动添加的图像)这些添加的图像。

【问题讨论】:

    标签: html drag-and-drop html5-canvas


    【解决方案1】:
    function dragDrop(e, ui) {
    // get the drop point (be sure to adjust for border)
    var x = parseInt(ui.offset.left - offsetX) - 1;
    var y = parseInt(ui.offset.top - offsetY);
    
    // get the drop payload (here the payload is the $tools index)
    var theIndex = ui.draggable.data("toolsIndex");
    // drawImage at the drop point using the dropped image 
    ctx.drawImage($tools[theIndex], x, y, 32, 32);
    }
    

    http://jsfiddle.net/cyur7/

    这个 jsfiddle 链接工作正常。

    【讨论】:

    • 如何在其他画布中也启用拖放功能。而且它在我的 Chrome 浏览器中不起作用。
    • 我用的是火狐,没问题,试试其他浏览器不要屏蔽。要启用拖放,您只需添加 TheOtherCanvasName.droppable({...});
    【解决方案2】:

    此解决方案使用 KineticJS 库来满足您的需求。

    这是代码和小提琴:http://jsfiddle.net/m1erickson/bSpBF/

    <!DOCTYPE html>
    <html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
        <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.1.min.js"></script>
    <style>
        body{ background-color: ivory; padding:10px;}
        #container1,#container2{
          border:solid 1px #ccc;
          margin-top: 10px;
          width:300px;
          height:100px;
        }
        #container2{
          height:300px;
        }
    </style>        
    <script>
    $(function(){
    
        var highlightWidth=8;
    
        var stage = new Kinetic.Stage({
            container: 'container1',
            width: 300,
            height: 100
        });
        var layer = new Kinetic.Layer();
        stage.add(layer);
    
    
        var dropzone = new Kinetic.Stage({
            container: 'container2',
            width: 300,
            height: 300
        });
        var dropLayer = new Kinetic.Layer();
        dropzone.add(dropLayer);
    
    
        // these must go after the creation of stages & layers
        addBackground(stage,layer,dropLayer);
        layer.draw();
        addBackground(dropzone,dropLayer,layer);
        dropLayer.draw();
    
    
        // get images & then trigger start()
        var images={};
        var URLs = {
          house1: 'https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-3.jpg',
          house2: 'https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-4.jpg',
          house3: 'https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-1.jpg'
        };
        loadImages(URLs,start);
    
    
        function start(){
            var house1=kImage(images.house1,10,10,50,50,layer);
            var house2=kImage(images.house2,75,10,50,50,layer);
            var house3=kImage(images.house3,140,10,50,50,layer);
            layer.draw();
        }
    
    
        function swapStagesIfSelected(sourceLayer,destinationLayer,startX,startY){
    
            // get all elements on the source layer
            var elements=sourceLayer.get("Image");
    
            // don't let dropped elements fall off the stage
            var totalWidth=0;
            var maxHeight=-999;
            var layerWidth=destinationLayer.getStage().getWidth();
            var layerHeight=destinationLayer.getStage().getHeight();
            for(var i=0;i<elements.length;i++){
                if(elements[i].isSelected){
                    totalWidth+=elements[i].getWidth();
                    maxHeight=Math.max(elements[i].getHeight(),maxHeight);
                }
            }
            if(startX+totalWidth>layerWidth){
                startX=layerWidth-totalWidth-15; 
            }
            if(startY+maxHeight>layerHeight){
                startY=layerHeight-maxHeight-15; 
            }
    
            // move all selected images 
            // to the clicked x/y of the destination layer
            for(var i=0;i<elements.length;i++){
                var element=elements[i];
                if(element.isSelected){
                    var img=element.getImage();
                    kImage(img,startX,startY,element.getWidth(),element.getHeight(),destinationLayer);
                    startX+=element.getWidth()+10;
                    element.remove();
                }
            }
            sourceLayer.draw();
            destinationLayer.draw();
        }
    
    
        // build the specified KineticJS Image and add it to the specified layer
        function kImage(image,x,y,width,height,theLayer){
            var image=new Kinetic.Image({
                image:image,
                x:x,
                y:y,
                width:width,
                height:height,
                strokeWidth:0.1,
                stroke:"green",
                draggable:true
            });
            image.myLayer=theLayer;
            image.isSelected=false;
            image.on("click",function(){
                highlight(this);
                this.myLayer.draw();
            });
            image.myLayer.add(image);
            return(image);
        }
    
    
        // build a background image and add it to the specified stage
        function addBackground(theStage,theLayer,otherLayer){
    
            var background = new Kinetic.Rect({
              x: 0,
              y: 0,
              width: theStage.getWidth(),
              height: theStage.getHeight(),
              fill: "white",
              stroke: "green",
              strokeWidth: 1
            });
            background.on("click",function(){
                var pos=theStage.getMousePosition();
                var mouseX=parseInt(pos.x);
                var mouseY=parseInt(pos.y);
                swapStagesIfSelected(otherLayer,theLayer,mouseX,mouseY);
            });
            theLayer.add(background);
        }
    
    
        /////////////  Image loader
    
              function loadImages(URLs, callback) {
                var loaded = 0;
                var needed = 0;
                for(var url in URLs) { needed++; console.log(url); }
                for(var url in URLs) {
                  images[url] = new Image();
                  images[url].onload = function() {
                    if(++loaded >= needed) {
                      callback(images);
                    }
                  };
                  images[url].src = URLs[url];
                }
              }
    
        /////////////  Toggle Highlighting
    
        function highlight(element,setStrokeWidth){
            if(setStrokeWidth){
                    element.setStrokeWidth(setStrokeWidth);
            }else{
                if(element.getStrokeWidth()>5){
                    element.setStrokeWidth(0.1);
                    element.isSelected=false;
                }else{
                    element.setStrokeWidth(highlightWidth);
                    element.isSelected=true;
                }
            }
        }
    
    
    }); // end $(function(){});
    
    </script>       
    </head>
    
    <body>
        <p>Click on image(s) to toggle selection</p>
        <p>Then click in the other canvas to drop</p>
        <div id="container1"></div>
        <div id="container2"></div>
        <button id="clear">Clear Hightlights</button>
        <button id="swap">Swap Selected</button>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      • 2012-05-23
      • 2015-08-12
      • 2012-05-20
      相关资源
      最近更新 更多