Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Nowadays I am playing with KonvaJS . I have set a click event listener on a shape . When I click on shape , the listener is invoked. In my listener I am invoking event.preventDefault() method to stop triggering of other events. But when I do so it says that:

Uncaught TypeError: event.preventDefault is not a function

I don't know what I am missing. Here is my code :

<!DOCTYPE html>
  <script src="https://cdn.rawgit.com/konvajs/konva/0.9.5/konva.min.js"></script>
  <meta charset="utf-8">
  <title>Konva Scale Animation Demo</title>
  <style>
    body {
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: #F0F0F0;
  </style>
</head>
  <div id="container"></div>
  <script>
    var width = window.innerWidth;
    var height = window.innerHeight;
    var stage = new Konva.Stage({
      container: 'container',
      width: width,
      height: height
    var layer = new Konva.Layer();
    stage.add(layer);
    var gArrow2 = new Image();
     var circle2 = new Konva.Image({
        x: 60, //on canvas
        y: 60,
        image: gArrow2,
        name: 'abc',
        id: 'xyz',
        draggable:true
      circle2.on('click',function(event){
        event.preventDefault()
        console.log(e.type);
      circle2.on('mouseup',function(event){
         event.preventDefault()
        console.log(e.type);
    gArrow2.onload = function() {
      layer.add(circle2);
      layer.draw();
    gArrow2.src = 'http://konvajs.github.io/assets/snake.png';
    var period = 1500;
    var amplitude = 10;
    var centerX = stage.getWidth() / 2;
    var centerY = stage.getHeight() / 2;
    var anim = new Konva.Animation(function(frame) {
      circle2.setY(amplitude * Math.sin(frame.time * 2 * Math.PI / period) + centerY);
    }, layer);
    anim.start();
  </script>
</body>
</html> 

It seems that KonvaJS's on handler does not give you directly the browser events but an object that looks like: enter image description here.

The browser's original event is stored in the evt property.

In your code, try to replace event.preventDefault() by event.evt.preventDefault(): http://plnkr.co/edit/tCESd42r67TzeuLiISxU.

You nailed it brother. thanks. Can I stop triggering mouseup event when user clicks on shape? – Hitesh Kumar Sep 4, 2015 at 13:14 If you want to remove the default operations on click, preventing on the click event is sufficient. No need to call preventDefault on mouseup too. – Quentin Roy Sep 4, 2015 at 13:16

If you want other events not to be triggered then KonvaJS has a property called cancelBubble for event which you can set to true. By using this need not to look for event.preventDefault() function. Here's the plunkr to demonstrate.

triangle.on('mouseup', function(e) {
      e.cancelBubble = true;
      console.log('Triangle mouse up');
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.