Move object within canvas boundary limit

前端 未结 6 1634
野性不改
野性不改 2020-12-08 00:58

I am trying to limit the moving object within the canvas but i am getting some difficulty to moving object with limit area on top and left side and when i scale the object w

6条回答
  •  猫巷女王i
    2020-12-08 01:28

    Just add the below code in your js file and change the value of scale X(left) and Y(top) according to your canvas height and width.

    // canvas moving limit 
    
    canvas.observe("object:moving", function(e){
          var obj = e.target;
             // if object is too big ignore
    
            var halfw = obj.currentWidth/2;
            var halfh = obj.currentHeight/2;
            var bounds = {tl: {x: halfw, y:halfh},
                br: {x: obj.canvas.width , y: obj.canvas.height }
            };
    
            // top-left  corner
    
    
    
                // alert("text");
            if(obj.top < bounds.tl.y || obj.left < bounds.tl.x){
                obj.top = Math.max(obj.top, '10'  );
                obj.left = Math.max(obj.left , '50' ) 
            }
    
    
            // bot-right corner
            if(obj.top > bounds.br.y || obj.left > bounds.br.x ){
                obj.top = Math.min(obj.top, '360'  );  
                obj.left = Math.min(obj.left, '470' )  
            }
    
    });
    // end canvas moving limit
    

提交回复
热议问题