0

我设计了一个页面,可以在画布中使用鼠标绘制线条并拖放项目。

我的主要问题是我只能删除一项。第二个问题是我无法在拖动项目时克隆,所以我在开始时将它们复制到 for 循环中。

这是完整的页面:http ://www.trustweb.it/Html5draw/

这是与拖放项目相关的javascript:

/***DRAG&DROP ITEMS ***/
        var overitem=0;
        var start = null;

        function bindGroupEvents(group, rect, trash) {
            group.on('mouseover', function() {

              overitem=1;
              if(group.getStage()) {
                trash.moveTo(group);
                trash.show();
                trash.getLayer().draw();
                document.body.style.cursor = 'pointer';
              }
            });

            group.on('mouseout', function() {
              trash.hide();
              trash.getLayer().draw();
              document.body.style.cursor = 'default';
              overitem=0;
            });

            group.on('dragmove', function() {
              if(start) {
                var rect = group.get('.rect')[0];
                rect.setWidth(start.rectWidth + group.getX() - start.groupX);
                rect.setHeight(start.rectHeight + start.groupY - group.getY());
                group.setX(start.groupX);
                trash.setX(rect.getWidth() - 24);
                // draw shapes layer and handle layer
                trash.getStage().draw();
              }
            });

            group.on('dragstart', function() {
              /*
              if(group.getId()=='dryer' || group.getId()=='washing' )
              {
                alert('to be cloned');
                }
            */
              group.moveToTop();
            });
        }


        function bindTrashEvents(trash) {
            trash.on('mouseover', function() {

              console.log('over');

              var group = trash.getParent();
              var rect = group.get('.rect')[0];
              start = {
                groupX: group.getX(),
                groupY: group.getY(),
                rectWidth: rect.getWidth(),
                rectHeight: rect.getHeight()
              };
            });

            trash.on('mouseout', function(){
              start = null;
            });

            trash.on('click', function() {
                alert('click');

                trash.getParent().transitionTo({
                    opacity: 0,
                    duration: 0.2,
                    callback: function() {
                        var rect = trash.getParent().remove();
                    }
                })
            });
        }
        /*** END DRAG&DROP ***/

        window.onload = function() {
            var layer = new Kinetic.Layer();
            var stage = new Kinetic.Stage({
                container: "container",
                width: 978,
                height: 598
            });


            /** DRAG&DROP UITEM **/
            var shapesLayer = new Kinetic.Layer();
            var editLayer = new Kinetic.Layer();

            // build trash
            trash = new Kinetic.Image({
              visible: false,
              opacity: 0.7,
              x: 3,
              y: 3
            });

            // load trash image
            var trashImg = new Image();
            trashImg.onload = function() {
              trash.setImage(trashImg);
              editLayer.add(trash);
            }; 
            trashImg.src = 'x.png';

            bindTrashEvents(trash);

            // build rectangles
            var colors = ['red', 'orange'];

            /* washing machine */
            for(i=0;i<30;i++)
            {
            var group = new Kinetic.Group({
                x: 1,
                y: 1,
                id:'washing',
                draggable: true
            });
            var rect = new Kinetic.Rect({
                width: 50,
                height:50,
                fill: colors[0],
                stroke: 'black',
                opacity: 0.85,
                name: 'rect'
            });

            bindGroupEvents(group, rect, trash);
            group.add(rect);
            shapesLayer.add(group);
            }

            /* dryer */
            for(i=0;i<30;i++)
            {
            var group = new Kinetic.Group({
                x: 100,
                y: 1,
                draggable: true
            });
            var rect = new Kinetic.Rect({
                width: 50,
                height:50,
                fill: colors[1],
                stroke: 'black',
                opacity: 0.85,
                name: 'rect'
            });
            bindGroupEvents(group, rect, trash);
            group.add(rect);
            shapesLayer.add(group);
            }
            stage.add(shapesLayer);
            stage.add(editLayer);

        }
4

1 回答 1

0

最新的 KineticJS 4.3 有一个可以使用的 .clone() 方法。

 group.on('click', function(){ 
    var newRect = rect.clone();
    shapesLayer.add(newRect);
    newRect.simulate('mousedown');
    newRect.simulate('dragstart');
 }

见:http: //jsfiddle.net/CPrEe/38/

如果要删除图层中的所有项目,您可以这样做:

 layer.removeChildren();

或者干脆做:

 layer.hide(); 

这样你就看不到整个图层。

更新 http://jsfiddle.net/CPrEe/77/

group.on('click', function(){ 
    var newRect = rect.clone();
    shapesLayer.add(newRect);
    newRect.fire('mousedown');
    newRect.fire('dragstart');
}
于 2013-01-09T22:01:09.883 回答