Custom drawfunc in KineticJS

戏子无情 提交于 2019-12-24 10:44:30

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/20855410/custom-drawfunc-in-kineticjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!