【问题标题】:Make transparent eraser in kineticjs [closed]在kineticjs中制作透明橡皮擦[关闭]
【发布时间】:2014-08-14 00:40:21
【问题描述】:

您好,如何使用 kineticjs 或仅使用画布制作透明橡皮擦。

我需要擦除某些图像上的部分多边形,只使多边形的一部分透明并且图像应该保持不变。

【问题讨论】:

  • 到目前为止您尝试过什么?您能否展示您迄今为止的努力或更具体的内容?
  • 检查markE的答案。

标签: javascript html canvas transparency kineticjs


【解决方案1】:

您需要访问上下文的globalCompositeOperation="destination-out",它充当“橡皮擦”。 KineticJS 上下文不会为您提供 globalCompositeOperation,因此您必须挖掘才能获得“真正的” html 画布上下文:

  • 把你的图片放在底层
  • 将多边形放在顶层
  • 在顶层放置自定义形状
  • 使用自定义形状获取 html 画布上下文 [context=layer.getContext()._context]
  • 使用context.globalCompositeOperation="destination-out"“擦除”多边形并让下面的图像显示出来。

这是一个“擦除”到图像的形状对象的示例:

http://jsfiddle.net/m1erickson/yv9qn/

var myShape = new Kinetic.Shape({
  x: 0,
  y: 0,
  fill:"blue",
  drawFunc: function(context) {
    var ctx=layer.getContext()._context;
    ctx.save();
    ctx.globalCompositeOperation="destination-out";
    for(var i=0;i<this.cutouts.length;i++){
        var cut=this.cutouts[i];
        ctx.fillRect(cut.x,cut.y,cut.width,cut.height);
    }
    ctx.restore();  
  }
});
myShape.cutouts=[];
layer.add(myShape);

myShape.cutouts.push({x:75,y:75,width:30,height:30});
myShape.cutouts.push({x:225,y:75,width:30,height:30});
myShape.cutouts.push({x:150,y:125,width:30,height:30});

【讨论】:

  • 关于问题,如果我用多边形替换透明的矩形,点的顺序很重要。
  • {x,y,width,height} 对象的顺序并不重要。
  • 我认为如果用 {x, y, width, height} 定义的这个矩形替换为用点数组定义的多边形 {x, y} 例如,多边形中点的顺序重要吗?
  • 我不明白,但这里有一个使用多边形点而不是矩形的示例:jsfiddle.net/m1erickson/kr545
  • 酷,谢谢,我使用了其他一些算法,它取决于多边形中点的顺序。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-18
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
  • 2011-04-16
  • 2012-07-15
  • 2012-09-30
相关资源
最近更新 更多