如何将不同的画布绘图视为一个,如何在其中跟踪序列?

时间:2015-06-17 09:19:13

标签: javascript canvas

我有一个场景,我正在使用画布,我从画布的左侧拖动一些绘图并将其放在它的右侧,如果我将一个盒子放到另一个盒子附近它也会杵一起。现在我还需要做两件事 -

  1. 首先,一旦我与另一个人吵架,我想给这套不止一个盒子,一个盒子的治疗。意思是如果我拖动任何一个杵在一起的盒子,整个盒子应该被拖到一起。

  2. 第二,当我放弃框时,我想根据已删除的框生成一些纯文本,例如,如果我删除"第一个框",纯文本应该是像#34;你先丢掉第一个盒子"。我需要对序列做这件事,这意味着如果第二个框首先出现,那么根据该框出现的文本应首先出现。

  3. 以下是我已完成的代码

        <script type="text/javascript">
                window.onload = function(){
                    draw();
                }
            </script>
        <body style="margin: 0;padding:0;clear:both; cursor: pointer">
            <canvas id="canvas" tabindex="1" style="float:left" ></canvas>
            <div id="plainEnglish" tabindex="2" style="float: left;"></div>
        </body>
    
    <script>
    var c = document.getElementById("canvas");
    var ctx = c.getContext("2d");
    
    c.width = 600;
    c.height = 300;
    
    //My mouse coordinates
    var x,y;
    c.addEventListener("mousedown",down);
    c.addEventListener("mousemove",move);
    c.addEventListener("mouseup",up);
    
    var r = 0;
    
    function counter() {
     r++;
     console.log(r);
    }
    
    //I'll save my boxes in this array
    var myBoxes = new Array();
    
    // Those boxes which I have moved to droppable area of the canvas.
    var myDroppedBoxes = new Array();
    
    //This function describes what a box is.
    //Each created box gets its own values
    function box(x,y,w,h,rgb,text) {
        this.x = x,
        this.y = y;
        this.xS = x; //saving x
        this.yS = y; //saving y
        this.w = w;
        this.h = h;
        this.rgb = rgb;
        this.text = text;
    
        //to determine if the box is being draged
        this.draging = false;
        this.isBeingDragged = false;
    }
    
    //Let's make some boxes!!
    myBoxes[0] = new box(20,20,75,20,"#6AA121","First");
    myBoxes[1] = new box(20,50,75,20,"#6AA121", "Second");
    myBoxes[2] = new box(20,80,75,20,"#6AA121","third");
    
    //here we draw everything
    function draw() {
        ctx.clearRect(0,0,c.width,c.height);
        //Dropable area
        ctx.fillStyle="red";
        ctx.fillRect(c.width/2,0,c.width,c.height);
    
        //Boxes!
        for (var i = 0; i<myBoxes.length; i++) {
            var b = myBoxes[i];
            if (b.draging) { //box on the move
                //Also draw it on the original spot
                ctx.fillStyle=b.rgb; 
                ctx.fillRect(b.xS,b.yS,b.w,b.h);
                ctx.strokeRect(b.xS,b.yS,b.w,b.h);
                ctx.font = "14px Arial";
                ctx.strokeText(b.text, b.xS + 5 , b.yS + 15);
            }
    
            ctx.fillStyle=b.rgb;
            ctx.fillRect(b.x,b.y,b.w,b.h);
            ctx.strokeRect(b.x,b.y,b.w,b.h);
            ctx.font = "14px Arial";
            ctx.strokeText(b.text, b.x + 5 , b.y + 15);
        }
    
        for(var i = 0; i< myDroppedBoxes.length; i++) {
            var b = myDroppedBoxes[i];
            ctx.fillStyle=b.rgb;
            ctx.fillRect(b.x,b.y,b.w,b.h);
            ctx.strokeRect(b.x,b.y,b.w,b.h);
            ctx.font = "14px Arial";
            ctx.strokeText(b.text, b.x + 5 , b.y + 15);
        }
    
    
    }
    
    
    function down(event) {
        event = event || window.event;
        x = event.pageX - c.offsetLeft,
        y = event.pageY - c.offsetTop;
    
        for (var i = 0; i<myBoxes.length; i++) {
            var b = myBoxes[i];
            if (x>b.xS && x<b.xS+b.w && y>b.yS && y<b.yS+b.h) {
                myBoxes[i].draging = true;
                myBoxes[i].isBeingDragged = true;
            }
    
        }
    
        for (var i = 0; i<myDroppedBoxes.length; i++) {
            var b = myDroppedBoxes[i];
            if (x>b.x && x<b.x + b.w && y>b.y && y<b.y + b.h) {
                b.draging = true;
                b.isBeingDragged = true;
            }
    
        }
        draw();
    }
    
    function move(event) {
        event = event || window.event;
        x = event.pageX - c.offsetLeft,
        y = event.pageY - c.offsetTop;
    
        for (var i = 0; i<myBoxes.length; i++) {
            var b = myBoxes[i];
            if (b.draging && b.isBeingDragged) {
                myBoxes[i].x = x;
                myBoxes[i].y = y;
    
                if (b.x>c.width/2) {
                var length = myDroppedBoxes.length ;
                    myDroppedBoxes[length] = new box(x,y,b.w,b.h,b.rgb,b.text);
                    myDroppedBoxes[length].draging = true;
                    myDroppedBoxes[length].isBeingDragged = true;
                    b.x = b.xS;
                    b.y = b.yS;
                    b.isBeingDragged = false;
                }
            }
        }
    
        for (var i = 0; i<myDroppedBoxes.length; i++) {
            var b = myDroppedBoxes[i];
            if (b.draging && b.isBeingDragged) {
                b.x = x;
                b.y = y;
            }
        }
        draw();
    }
    function up(event) {
        event = event || window.event;
        x = event.pageX - c.offsetLeft,
        y = event.pageY - c.offsetTop;
    
        for (var i = 0; i< myBoxes.length; i++) {
            var b = myBoxes[i];
            if (b.draging && b.isBeingDragged) {
                //Let's see if the rectangle is inside the dropable area
                if (b.x < c.width/2) {
                    myBoxes[i].x = b.xS;
                    myBoxes[i].y = b.yS;
                    myBoxes[i].draging = false;
                    b.isBeingDragged = false;
                }
    
            }
        }
    
        for (var i = 0; i< myDroppedBoxes.length; i++) {
            var b = myDroppedBoxes[i];
            if ( b.isBeingDragged) {
                //Let's see if the rectangle is inside the dropable area
                if (b.x>c.width/2) {
                    b.x = x;
                    b.y = y;
                    clubLegos(b);
                    plainTextMaker();
                    b.isBeingDragged = false;
                }
                else {
                    //No it's not, sending it back to its original spot   
                   myDroppedBoxes.splice(i,1);
                }
    
            }
    
        }
        draw();
    }
    
    function clubLegos(b) {
        // this loop is for checking that the box is lying near to which other box.
        for(var j = 0; j < myDroppedBoxes.length; j++) {  
            var z =  myDroppedBoxes[j];
            if(!z.isBeingDragged) {
                    if(((x > z.x) && (x < (z.x + z.w))) && ((y > (z.y - 15)) && (y < (z.y + z.h + 10)))) {
                        b.x = z.x;
                        if( (y - z.y) >= 0) {
                            b.y = (z.y + z.h);
                            console.log("inside if " + y + " " + z.y);
                        }
                        else {
                        console.log("inside else " +  y + " " + z.y);
                            b.y = (z.y - z.h);
                        }
    
    
                    }
            }
        }
    }
    
    function plainTextMaker() {
        plainEnglishDiv =  document.getElementById("plainEnglish");
        plainEnglishDiv.innerHTML = "<h3>Here I am generating some plain text based on each drag and drop</h3>";
    }
    
    </script>
    </html>
    

    这是同样的JS小提琴 - http://jsfiddle.net/akki166786/wa52f9pm/

    非常感谢任何帮助。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

为每个盒子对象提供与Html class类似的内容。

这样你可以用相同的&#34;类&#34;拖动每个方框。同时进行。

为每个框添加club:属性。

// at top of app set a counter to make new clubs
var nextClub=0;

// each new box gets a unique "club"
function box(x,y,w,h,rgb,text) {
    ...
    club:(nextClub++),
    ...

clubLegos(b)中,当您将拖动的框b附加到另一个框z时,也会将已删除的框(b)&amp; (z)组的任何成员都是一个全新的俱乐部ID:

// give the dropped box (b) & any member of (z)'s group a brand new club id
...if attaching b to z
b.club=nextClub;
for(var i=0;i<myBoxes.length;i++){
    var bb=myBoxes[i];
    if(bb.club==z.club){
        bb.club=nextClub;
    }
}
nextClub++;

这样,例如,如果mousedown让用户开始使用club==2拖动一个框,那么您可以将每个myBoxes添加club==2到您的拖动数组和每个框中同时拖动club==2