最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - KonvaJS: event.preventDefault is not a function - Stack Overflow

programmeradmin6浏览0评论

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>
<html>

<head>
  <script src=".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>

<body>
  <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 = '.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> 

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>
<html>

<head>
  <script src="https://cdn.rawgit./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>

<body>
  <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> 
Share Improve this question edited Jan 26, 2021 at 14:47 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 4, 2015 at 12:51 Hitesh KumarHitesh Kumar 3,7089 gold badges48 silver badges73 bronze badges 1
  • See Konva docs for cancelBubble – Vanquished Wombat Commented Jan 26, 2021 at 15:40
Add a ment  | 

2 Answers 2

Reset to default 7

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

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.

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');
    });
发布评论

评论列表(0)

  1. 暂无评论