当前位置: 首页 > 工具软件 > KineticJS > 使用案例 >

HTML5画布形状与KineticJS事件

施弘壮
2023-12-01
检测形状与KineticJS事件,我们可以使用on()方法将事件处理程序绑定到一个节点。在()方法需要一个事件类型和事件发生时执行的函数。KineticJS支持鼠标悬停、mouseoutmouseenter、mouseleavemousemove,mousedown,mouseup,click,dblclick,dragstart,拖动结束桌面活动。

说明:Mouseover和mouseout三角形,鼠标悬停,mouseout,mousedown和mouseup循环。

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.0.0.min.js"></script>
    <script defer="defer">
      function writeMessage(message) {
        text.setText(message);
        layer.draw();
      }
      var stage = new Kinetic.Stage({
        container: 'container',
        width: 578,
        height: 200
      });
      var layer = new Kinetic.Layer();
      
      var triangle = new Kinetic.RegularPolygon({
        x: 190,
        y: 120,
        sides: 3,
        radius: 80,
        fill: '#00D2FF',
        stroke: 'black',
        strokeWidth: 4
      });
      
      var text = new Kinetic.Text({
        x: 10,
        y: 10,
        fontFamily: 'Calibri',
        fontSize: 24,
        text: '',
        fill: 'black'
      });
      
      triangle.on('mouseout', function() {
        writeMessage('Mouseout triangle');
      });

      triangle.on('mousemove', function() {
        var mousePos = stage.getPointerPosition();
        var x = mousePos.x - 190;
        var y = mousePos.y - 40;
        writeMessage('x: ' + x + ', y: ' + y);
      });

      var circle = new Kinetic.Circle({
        x: 380,
        y: stage.height()/2,
        radius: 70,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 4
      });

      circle.on('mouseover', function() {
        writeMessage('Mouseover circle');
      });
      circle.on('mouseout', function() {
        writeMessage('Mouseout circle');
      });
      circle.on('mousedown', function() {
        writeMessage('Mousedown circle');
      });
      circle.on('mouseup', function() {
        writeMessage('Mouseup circle');
      });

      layer.add(triangle);
      layer.add(circle);
      layer.add(text);
      stage.add(layer);
      
    </script>
  </body>
</html>


 类似资料: