konvajs

Adding custom icons to konvajs transformer anchors


Please have a look at www.jsbin.com/wigokojina/1/edit?html,css,js,output

I've added custom svg icons to the konva transformer, but the middle rotator icon is draggable even if i set draggable to false. The two other icons are fine and as expected, as far as dragging is concerned.

My questions are:

  1. How do i disable the dragging for the rotator anchor, so that the icon doesnt move?

  2. How do i disable all event handlers for an anchor, and add one click event? Ive tried shape.off('dragmove') etc. The only thing that helps is setting listening to false, but then im prevented from adding a new event listener. I want to disable all event handlers for the top right anchor, and add one onclick listener afterwards.

  3. Is it possible to add the icons to the shape itself using fillPatternImage? Instead of adding the icon as a new shape like im doing. If its possible, please provide an example using the jsbin.

Thanks very much :-)


Solution

  • At the current moment konva@4.0.16 doesn't support fully customized Konva.Transformer. But you are making a good attempt.

    How do i disable the dragging for the rotator anchor, so that the icon doesn't move at all?

    You can reset the position in transform event (almost as you do it). But at the current moment, for performance reasons, inside transform events all anchors has "old" positions. So you see dragging of the icon from rotater anchor. To fix the issue we can force update a transformer:

    circle.on('transform', function(){
      transformer.update();
      for (var button in buttons) {
        var selector = button.replace('_', '-');
        var shape = transformer.findOne('.' + selector);
        var icon = transformer.findOne('.' + selector + '-icon');
        icon.position(shape.position());
        icon.x(icon.x() - 5.25); icon.y(icon.y() - 5.25);
        layer.batchDraw();
      }
    });
    

    How do I disable all event handlers for an anchor, and add one-click event

    It can be something like this:

    shape.listening(false);
    icon.on('click', () => {
      alert('delete');
    });
    

    Demo: https://jsbin.com/galetahara/4/edit?js,output