0

我在 canvas.clipTo 函数中使用 ctx.scale() 方法给出不同的画布形状。现在,当我在选择框中选择墙时,我想从画布中删除现有的剪切区域,我正在使用 fabric.js 有人知道如何解决这个问题吗?这是我的代码//html

 <canvas id="c" width="632" height="485"></canvas>
<span class="tt1">DECAL SHAPE</span>
<select name="Decal Shape" id="clipingShape" style="height:30px;">
<option> WALL </option>
<option> CIRCLE </option>
<option> OVAL </option>
<option> HORIZONTAL RECTANGLE  </option>
<option> VERTICAL RECTANGLE  </option>                                                
</select>

脚本//

var canvas = new fabric.Canvas('c');
$("#clipingShape").change(function(e){
 var w;
 var h;
 var ctx = canvas.getContext('2d');
 var shape =$('#clipingShape :selected').val();
    canvas.backgroundColor = 'red';
  if(shape=="WALL") 
  {
alert("how to remove cloping");
  }
canvas.clipTo = function(ctx) {
 if(shape=="OVAL")
 {
 w=canvas.width / 4;
 h=canvas.height / 2.4;
 ctx.save();
 ctx.scale(2, 1.2);
 ctx.arc(w, h, 155, 0, 2 * Math.PI, true);
 }
 if(shape=="CIRCLE")
 {
 w=canvas.width / 2;
 h=canvas.height / 2;
 ctx.save();
 ctx.arc(w, h, 230, 0, Math.PI * 2,true); //  Circle Shape
 }
 if(shape=="VERTICAL RECTANGLE")
 { 
 w=canvas.width - 217;
 h=canvas.height - 70;
 //alert(w);
 //alert(h);
 ctx.save();
 ctx.rect(110, 35, w, h); 
 }
  if(shape=="HORIZONTAL RECTANGLE")
 { 
 w=canvas.width - 100;
 h=canvas.height - 100;
 ctx.save();
 ctx.rect(50, 50, w, h); 
 }
  if(shape=="WALL")
 { 
 w=canvas.width - 100;
 h=canvas.height - 100;
 ctx.save();
 ctx.clear();
 }
 ctx.stroke();
 ctx.restore();

 };
 canvas.renderAll();

});
// canvas.renderAll();
var text;
text = new fabric.Text('Honey', {
  fontSize: 50,
  left: 100,
  top: 100,
  lineHeight: 1,
  originX: 'left',
  fontFamily: 'Helvetica',
  fontWeight: 'bold'
});
  canvas.add(text);

这是我的小提琴演示

4

1 回答 1

1

这个可能会有所帮助。http://jsfiddle.net/xadqg/13/

canvas.clipTo = null;
canvas.backgroundColor = 'white';
canvas.renderAll();
于 2013-09-29T14:45:18.033 回答