JSFiddle - React, Tailwind, and code Playground

by confile

HTML

<div id="container"></div>
    <div id="buttons">
      <input type="button" id="punch" value="Punch">
    </div>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.1.0.min.js"></script>
        
        
Image <br>
<div id="mydiv" ></div>

CSS

body {
        margin: 0px;
        padding: 0px;
      }
      #container {
        background-color: #222;
        display: inline-block;
        width: 580px;
        height: 202px;
      }
      #buttons {
        position: absolute;
        top: 5px;
        left: 10px;
      }
      #buttons > input {
        padding: 10px;
        display: block;
        margin-top: 5px;
      }

JavaScript

var stage = new Kinetic.Stage({
        container: 'container',
        width: 578,
        height: 200
      });
      var layer = new Kinetic.Layer();

      var imageObj = new Image();
      imageObj.onload = function() {
        var blob = new Kinetic.Sprite({
          x: 250,
          y: 40,
          image: imageObj,
          animation: 'idle',
          animations: {
            idle: [
              // x, y, width, height (4 frames)
              2,2,70,119,
              71,2,74,119,
              146,2,81,119,
              226,2,76,119
            ],
            punch: [
              // x, y, width, height (3 frames)
              2,138,74,122,
              76,138,84,122,
              346,138,120,122
            ]
          },
          frameRate: 7,
          frameIndex: 0
        });

        // add the shape to the layer
        layer.add(blob);

        // add the layer to the stage
        stage.add(layer);

        // start sprite animation
        blob.start();

        var frameCount = 0;
        
        blob.on('frameIndexChange', function(evt) {
          if (blob.animation() === 'punch' && ++frameCount > 3) {
            blob.animation('idle');
            frameCount = 0;
          }
        });
          
        document.getElementById('punch').addEventListener('click', function() {
            
            alert(blob.width());
          
          blob.animation('punch');
          
          var animationKey = blob.animation();  
          var startIndex = blob.frameIndex();
          var endIndex = startIndex + 4;  
          var anArrayObj = blob.animations();  
            console.log(anArrayObj);
            console.log("index: "+startIndex+" animationKey: "+animationKey);  
          
          
          var tmpCoordinates = anArrayObj[animationKey]; 
          console.log(tmpCoordinates);  
          var finalCoords = new Array();  
            for (var i=startIndex; i < endIndex; i++) {
               ...