简体   繁体   English

KineticJS的形状相交

[英]shape intersection with KineticJS

I want to show only intersection of two shape by using KineticJS . 我想通过使用KineticJS显示两个形状的唯一交集 How can I do this? 我怎样才能做到这一点? I tried to do it like following link. 我尝试按照以下链接进行操作。 HTML5 Canvas Clipping Region . HTML5画布剪切区域 Is have any other way? 还有其他办法吗?

You can use the globalCompositeOperation to do this: http://jsfiddle.net/wbzwX/ 您可以使用globalCompositeOperation来做到这一点: http : //jsfiddle.net/wbzwX/

ctx.fillStyle="#000";
ctx.fillRect(50,50,100,100);

ctx.globalCompositeOperation = "source-in"; 
// this will use the fillstyle of the next drawn object. 
// "destination-in" will use the previous fillstyle.

ctx.beginPath();
ctx.arc(100,50,30,0,Math.PI*2,false);
ctx.closePath();
ctx.fillStyle="#f00";
ctx.fill();​

I have created the solution based on Shmi. 我创建了基于Shmi的解决方案。

   function makeShapeComposite(shape, operation) {
        shape.attrs._drawFunc = shape.attrs.drawFunc;
        shape.attrs.drawFunc = function (context) {
            context.save();
            context.globalCompositeOperation = operation;
            this.attrs._drawFunc.call(this, context);
            context.restore();
        };
        return shape;
    };   

   var pol=  makeShapeComposite(new Kinetic.RegularPolygon({
           x: 250,
            y: 100,
            sides: 4,
            radius: 70,
            fill: '#00D2FF',
            stroke: 'black',
            strokeWidth: 2
        }), "destination-in");

Fiddle: http://jsfiddle.net/sara9/2v7W2/5/ 小提琴: http : //jsfiddle.net/sara9/2v7W2/5/

Refer this tutorial. 请参考本教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM