2016-05-20 4 views
0

私はFabric.jsを使用していますが、キャンバスオブジェクトを自動整列させる方法はわかりません。

キャンバスオブジェクトをこのような別のオブジェクトと自動整列させる例はありますか?

スナップ例の画像
auto snap example from other site

ここにリンク:私は同じことに取り組んでいた
auto snap example siteFabric.jsにキャンバスオブジェクトを自動スナップする方法はありますか

答えて

2

。これは非常に一般的な機能要求であるように思われたので、私は仕事を分かち合いたいと思った。それはいくらか洗練を使うことができる。たとえば、赤いボックスのサイズを変更すると、スケールが適用されず、右揃えされていないことがわかりました。しかし、私はそれが基本的なプリンシパルの井戸を実証し、あなたのニーズに合うようにそれを精緻化することができると思います。

(注:2017年8月1日:。すぐGitHubの上のより包括的なコードベースを配置する操作(https://github.com/JerrodV/FabricObjectAlignment)詳細を読む!)あなたはhere

Htmlのフィドルを表示することができます

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <canvas id="c" height="600" width="600" style="border:1px solid #c1c1c1;"></canvas> 
    </div> 
    </form> 
    <script src="Scripts/jquery-3.1.1.min.js"></script> 
    <script src="Scripts/fabric.min.js"></script> 
    <script src="Scripts/default.js"></script> 
</body> 
</html> 

Javascriptを:

Def = { 
    canvas: null,  
    rect: null, 
    lines: { 
     top: null, 
     left: null, 
     right: null, 
     bottom: null 
    }, 
    init: function() { 
     Def.canvas = new fabric.Canvas('c'); 

     Def.canvas.on('object:moving', Def.events.objectMoving); 

     Def.canvas.add(new fabric.Rect({ 
      height: 100, 
      width: 100, 
      top: 100, 
      left: 200, 
      fill: 'black', 
      selectable: false 
     })); 

     Def.canvas.add(new fabric.Rect({ 
      height: 100, 
      width: 100, 
      top: 300, 
      left: 100, 
      fill: 'black', 
      selectable: false 
     }));   

     Def.rect = new fabric.Rect({ 
      height: 100, 
      width: 100, 
      top: 200, 
      left: 250, 
      fill: 'red' 
     }); 

     Def.canvas.add(Def.rect); 
    }, 
    events: { 
     objectMoving: function (e) { 
      //Get the object being manipulated 
      var obj = e.target; 

      //Set up an object representing its current position 
      var curPos = { 
       top: parseInt(obj.get('top')), 
       left: parseInt(obj.get('left')), 
       right: parseInt(obj.get('left') + obj.get('width')), 
       bottom: parseInt(obj.get('top') + obj.get('height')) 
      }; 

      //Set up an object that will let us be able to keep track of newly created lines 
      var matches = { 
       top: false, 
       left: false, 
       right: false, 
       bottom: false 
      } 

      //Get the objects from the canvas 
      var objects = Def.canvas.getObjects(); 

      //For each object 
      for (var i in objects) { 

       //If the object we are looing at is a line or the object being manipulated, skip it 
       if (objects[i] === obj || objects[i].get('type') === 'line') { continue; } 

       //Set up an object representing the position of the canvas object 
       var objPos = { 
        top: parseInt(objects[i].get('top')), 
        left: parseInt(objects[i].get('left')), 
        right: parseInt(objects[i].get('left') + obj.get('width')), 
        bottom: parseInt(objects[i].get('top') + obj.get('height')) 
       } 

       //Look at all 4 sides of the object and see if the object being manipulated aligns with that side. 

       //Top//////////////////////////////////// 
       if (Def.inRange(objPos.top, curPos.top)) { 
        //We match. If we don't already have aline on that side, add one. 
        if (!Def.lines.top) { 
         Def.drawLine('top', objPos.top); 
         //Keep track of the fact we found a match so we don't remove the line prematurely. 
         matches.top = true; 
         //Snap the object to the line 
         obj.set('top', objPos.top).setCoords(); 
        } 
       }    
       //Left//////////////////////////////////// 
       if (Def.inRange(objPos.left, curPos.left)) { 
        if (!Def.lines.left) { 
         Def.drawLine('left', objPos.left);       
         matches.left = true; 
         obj.set('left', objPos.left).setCoords(); 
        } 
       }     
       //Right//////////////////////////////////// 
       if (Def.inRange(objPos.right, curPos.right)) { 
        if (!Def.lines.right) { 
         Def.drawLine('right', objPos.right);       
         matches.right = true;       
         obj.set('left', objPos.right - objects[i].get('width')).setCoords(); 
        } 
       }     
       //Bottom//////////////////////////////////// 
       if (Def.inRange(objPos.bottom, curPos.bottom)) { 
        if (!Def.lines.bottom) { 
         Def.drawLine('bottom', objPos.bottom);       
         matches.bottom = true; 
         obj.set('top', objPos.bottom - objects[i].get('height')).setCoords(); 
        } 
       } 

       //Look at the side we matched on. If we did not match, and we have a line, remove the line. 
       for (var j in matches) { 
        var m = matches[j]; 
        var line = Def.lines[j]; 
        if (!m && line) { 
         Def.canvas.remove(line); 
         Def.lines[j] = null; 
        } 

       } 

      } 
      Def.canvas.renderAll(); 
     } 
    }, 
    drawLine: function (side, pos) { 
     var ln = null 
     switch (side) { 
      case 'top': 
       ln = new fabric.Line([Def.canvas.get('width'), 0, 0, 0], { 
        left: 0, 
        top: pos, 
        stroke: 'rgb(178, 207, 255)' 
       }); 
       Def.lines.top = ln; 
       break; 
      case 'left': 
       ln = new fabric.Line([0, Def.canvas.get('height'), 0, 0], { 
        left: pos, 
        top: 0, 
        stroke: 'rgb(178, 207, 255)' 
       }); 
       Def.lines.left = ln; 
       break; 
      case 'right': 
       ln = new fabric.Line([0, Def.canvas.get('height'), 0, 0], { 
        left: pos, 
        top: 0, 
        stroke: 'rgb(178, 207, 255)' 
       }); 
       Def.lines.right = ln; 
       break; 
      case 'bottom': 
       ln = new fabric.Line([Def.canvas.get('width'), 0, 0, 0], { 
        left: 0, 
        top: pos, 
        stroke: 'rgb(178, 207, 255)' 
       }); 
       Def.lines.bottom = ln; 
       break; 
     } 
     Def.canvas.add(ln).renderAll(); 
    }, 
    alignTolerance : 6, 
    inRange: function (val1, val2) { 
     if ((Math.max(val1, val2) - Math.min(val1, val2)) <= Def.alignTolerance) { return true; }   
     else { return false; } 
    } 
}; 

$(Def.init); 

I Hこれは便利です。がんばろう!

関連する問題