Custom drawfunc in KineticJS

2019-08-03 14:01发布

I have gone through few articles for detecting hit only on the perimeter of circle or rectangle. Some workarounds for implementing it is done by doing maths on click event of the shape or creating groups. That's fine. But is there any way we can implement this by creating a custom draw function that just draws the perimeter of a rectangle, circle (stroke only).

Any help would be much appreciated.

标签: kineticjs
1条回答
小情绪 Triste *
2楼-- · 2019-08-03 14:29

This is really simple if you set your hit function. Here's a jsfiddle:

http://jsfiddle.net/L3EST/

The key is to use the strokeShape() method in your custom hit function instead of fillStrokeShape(), which is what the default hit function uses.

  var rect = new Kinetic.Rect({
    x: 10,
    y: 10,
    width: 100,
    height: 50,
    fill: 'red',
    stroke: 'black',
    strokeWidth: 10,
    drawHitFunc: function(context) {
      context.beginPath();
      context.rect(0, 0, this.getWidth(), this.getHeight());
      context.closePath();
      context.strokeShape(this);
    }
  });

After attaching events as is done in the jsfiddle, when you mouseover or mouseout of the perimeter of the shape, the events are fired.

查看更多
登录 后发表回答