【问题标题】:How to remove existing clipping region from canvas如何从画布中删除现有的剪辑区域
【发布时间】:2013-09-29 22:45:18
【问题描述】:

我在 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);

这是我的Fiddle Demo

【问题讨论】:

    标签: javascript jquery html canvas fabricjs


    【解决方案1】:

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

    canvas.clipTo = null;
    canvas.backgroundColor = 'white';
    canvas.renderAll();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      • 2013-09-09
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      相关资源
      最近更新 更多