1

I want to use Angularjs with Kineticjs. The problem is that, it seems angular is overriding mouse events on my stage object.

The following html works fine as stand alone file, you can test it. But if you put this as an Angularjs template, the mouse events stop working.

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
      #container {
        background-color: black;
        background-image: url("http://www.html5canvastutorials.com/demos/assets/line-building.png");
        background-position: 1px 0px;
        background-repeat: no-repeat;
        width: 580px;
        height: 327px;
      }
    </style>
   </head>
  <body>
    <div id="container"></div>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.1.min.js"></script>
    <script defer="defer">
  function getData() {
    return {
      '1st Floor': {
        color: 'blue',
        points: [366, 298, 500, 284, 499, 204, 352, 183, 72, 228, 74, 274]
      },
      '2nd Floor': {
        color: 'red',
        points: [72, 228, 73, 193, 340, 96, 498, 154, 498, 191, 341, 171]
      },
      '3rd Floor': {
        color: 'yellow',
        points: [73, 192, 73, 160, 340, 23, 500, 109, 499, 139, 342, 93]
      },
      'Gym': {
        color: 'green',
        points: [498, 283, 503, 146, 560, 136, 576, 144, 576, 278, 500, 283]
      }
    }
  }
  function updateTooltip(tooltip, x, y, text) {
    tooltip.getText().setText(text);
    tooltip.setPosition(x, y);
    tooltip.show();
  }
  var stage = new Kinetic.Stage({
    container: 'container',
    width: 578,
    height: 325
  });
  var shapesLayer = new Kinetic.Layer();
  var tooltipLayer = new Kinetic.Layer();

  var tooltip = new Kinetic.Label({
    opacity: 0.75,
    visible: false,
    listening: false,
    text: {
      text: '',
      fontFamily: 'Calibri',
      fontSize: 18,
      padding: 5,
      fill: 'white'
    },
    rect: {
      fill: 'black',
      pointerDirection: 'down',
      pointerWidth: 10,
      pointerHeight: 10,
      lineJoin: 'round',
      shadowColor: 'black',
      shadowBlur: 10,
      shadowOffset: 10,
      shadowOpacity: 0.5
    }
  });

  tooltipLayer.add(tooltip);

  // get areas data
  var areas = getData();

  // draw areas
  for(var key in areas) {

    var area = areas[key];
    var points = area.points;

    var shape = new Kinetic.Polygon({
      points: points,
      fill: area.color,
      opacity: 0,
      // custom attr
      key: key
    });

    shapesLayer.add(shape);

  }

  stage.add(shapesLayer);
  stage.add(tooltipLayer);

  stage.on('mouseover', function(evt) {
    var shape = evt.targetNode;
    shape.setOpacity(0.5);
    shapesLayer.draw();
  });
  stage.on('mouseout', function(evt) {
    var shape = evt.targetNode;
    shape.setOpacity(0);
    shapesLayer.draw();
    tooltip.hide();
    tooltipLayer.draw();
  });
  stage.on('mousemove', function(evt) {
    var shape = evt.targetNode;
    var mousePos = stage.getMousePosition();
    var x = mousePos.x;
    var y = mousePos.y - 5;
    updateTooltip(tooltip, x, y, shape.attrs.key);
    tooltipLayer.batchDraw();
  }); 

</script>

How does event handling work on angular? And how could I prevent from overriding events like:

stage.on('mouseover', function(evt) {
    var shape = evt.targetNode;
    shape.setOpacity(0.5);
    shapesLayer.draw();
  });

Thanks!

1 Answer 1

2

Ok, it seems Angular was capturing all DOM events.

The solution is to put Kineticjs tag before angular.js(and probably before jQuery too), and be sure you don't declare the tag again elsewhere in any view.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.