2017-04-19 111 views
0

我有2個矩形。當小矩形移動到較大矩形的外部時,我希望邊界外的小矩形區域不可見。防止Fabric JS對象可見邊界

如果小矩形的一半在裏面,一半在外面。然後只有一半可見,這example這樣工作。

我該如何做到這一點?

My JSFiddle

(function() { 
    var canvas = this.__canvas = new fabric.Canvas('canvas'); 
    fabric.Object.prototype.transparentCorners = false; 

    // create a rectangle with a fill and a different color stroke 
    var rect = new fabric.Rect({ 
    left: 50, 
    top: 50, 
    width: 150, 
    height: 250, 
    fill: 'transparent', 
    stroke: 'rgba(34,177,76,1)', 
    strokeWidth: 1 
    }); 

    var rect2 = new fabric.Rect({ 
    left: 50, 
    top: 50, 
    width: 50, 
    height: 50, 
    fill: 'rgba(34,50,100,1)', 
    stroke: 'rgba(34,177,76,1)', 
    strokeWidth: 1 
    }); 
    canvas.add(rect); 
    canvas.add(rect2); 
})(); 

例這裏可與這樣:

+0

@ moáois不,我會用1.7.9實際上,我會更新jsfiddle – HOY

回答

1

這可能通過以下方式來實現,用一個額外的庫調用lodash

(function() { 
 
    var canvas = this.__canvas = new fabric.Canvas('canvas'); 
 
    fabric.Object.prototype.transparentCorners = false; 
 
    // create a rectangle with a fill and a different color stroke 
 
    var rect = new fabric.Rect({ 
 
     left: 50, 
 
     top: 50, 
 
     width: 150, 
 
     height: 250, 
 
     fill: 'transparent', 
 
     stroke: 'rgba(34,177,76,1)', 
 
     strokeWidth: 1 
 
    }); 
 
    var rect2 = new fabric.Rect({ 
 
     left: 50, 
 
     top: 50, 
 
     width: 50, 
 
     height: 50, 
 
     fill: 'rgba(34,50,100,1)', 
 
     stroke: 'rgba(34,177,76,1)', 
 
     strokeWidth: 1, 
 
     clipName: 'rect2', 
 
     clipTo: function(ctx) { 
 
      return _.bind(clip, rect2)(ctx) 
 
     } 
 
    }); 
 

 
    function d2R(deg) { 
 
     return deg * (Math.PI/180); 
 
    } 
 

 
    function clip_name(name) { 
 
     return _(canvas.getObjects()).where({ 
 
      clipFor: name 
 
     }).first() 
 
    } 
 

 
    function clip(ctx) { 
 
     this.setCoords(); 
 
     var clipObj = clip_name(this.clipName); 
 
     var scaleXTo1 = (1/this.scaleX); 
 
     var scaleYTo1 = (1/this.scaleY); 
 
     ctx.save(); 
 
     var ctxLeft = -(this.width/2) + clipObj.strokeWidth; 
 
     var ctxTop = -(this.height/2) + clipObj.strokeWidth; 
 
     var ctxWidth = clipObj.width - clipObj.strokeWidth; 
 
     var ctxHeight = clipObj.height - clipObj.strokeWidth; 
 
     ctx.translate(ctxLeft, ctxTop); 
 
     ctx.scale(scaleXTo1, scaleYTo1); 
 
     ctx.rotate(d2R(this.angle * -1)); 
 
     ctx.beginPath(); 
 
     ctx.rect(clipObj.left - this.oCoords.tl.x, clipObj.top - this.oCoords.tl.y, clipObj.width, clipObj.height); 
 
     ctx.closePath(); 
 
     ctx.restore(); 
 
    } 
 
    rect.set({ 
 
     clipFor: 'rect2' 
 
    }); 
 
    canvas.add(rect); 
 
    canvas.add(rect2); 
 
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.9/fabric.min.js"></script> 
 
<canvas id="canvas" width="800" height="600"></canvas>

+0

嗨moasis,非常感謝,我也找到了這個解決方案,但後來我意識到,當我將我的大矩形轉換爲透明時,它不起作用。我想要實現的是,我有一個T恤衫設計應用程序,我需要在T恤中間設置一個矩形,以便設計不會超出界限。 – HOY

+0

@HOY hm ..檢查更新的答案 –

+0

當你改變物體的寬度或高度時,這個失敗不幸。 – HOY