Search code examples
transformkineticjsfabricjs

Ability to freely transform KineticJS objects like FabricJS


I love KineticJS, its speed, marriage with GSAP, but what is making my head spin is there a way to freely transform KineticJS objects like the way in FabricJS? Here is the link reference to what I am trying to say: http://fabricjs.com/customization/ I don't want to use FabricJs as its really slow, and its low performance evident from various unit tests.

I am really looking forward to finding a way to be able to freely transform object in KineticJS as it would make life so much easier.

Is there a way to do it?

Thanks for your help, Praney


Solution

  • Like markE said, this tutorial on Eric's (creator of KineticJS) tutorial site is the basis for all free transforming within KineticJS.

    I'll go into detail about the actual free transform logic, there are 2 main functions:

    function addAnchor(group, x, y, name) {
      var stage = group.getStage();
      var layer = group.getLayer();
    
      //Create the anchor shape
      var anchor = new Kinetic.Circle({
        x: x,
        y: y,
        stroke: '#666',
        fill: '#ddd',
        strokeWidth: 2,
        radius: 8,
        name: name,
        draggable: true,
        dragOnTop: false
      });
    
      //Calls the update function which handles the transform logic
      anchor.on('dragmove', function() {
        update(this);
        layer.draw();
      });
      //When the anchor is selected, we want to turn dragging off for the group
      //This is so that only the anchor is draggable, and we can transform instead of drag
      anchor.on('mousedown touchstart', function() {
        group.setDraggable(false);
        this.moveToTop();
      });
      //Turn back on draggable for the group
      anchor.on('dragend', function() {
        group.setDraggable(true);
        layer.draw();
      });
      // add hover styling
      anchor.on('mouseover', function() {
        var layer = this.getLayer();
        document.body.style.cursor = 'pointer';
        this.setStrokeWidth(4);
        layer.draw();
      });
      anchor.on('mouseout', function() {
        var layer = this.getLayer();
        document.body.style.cursor = 'default';
        this.setStrokeWidth(2);
        layer.draw();
      });
    
      group.add(anchor);
    }
    

    The addAnchor function, like the name says, adds a single anchor (or free transform handle) to a Kinetic.Group. By default it uses a Kinetic.Circle but really you can use any shape you want.

    • group - the group to add the anchor to
    • x - the x position of the anchor
    • y - the y position of the anchor
    • name - name of the anchor (usually describe which position the anchor represents, like topLeft or bottomRight

    You'll notice a bunch of events attached to the newly created anchor, most of these are pretty straight-forward but the one you want to pay attention to is the dragmove event - this event is the one that calls the update function which handles all the logic for transforming the group/node. It's useful to note that the update function is called for every pixel that you are dragging an anchor.

    The tutorial uses 4 corner anchors (thus calling addAnchor 4 times for each group/node), but if you wanted 8 anchors (4 corners - 4 sides) then you just have to adjust the logic to position the anchors correctly and to move the anchors properly when transforming.

    By the way, the reason we're adding Anchors to a Group, is because we need them to group with the node in question, and stick with each node through dragging and transforming.

    The second method is the update function:

    function update(activeAnchor) {
      var group = activeAnchor.getParent();
    
      //Get each anchor inside the group, by name. Keep a standard set of names for every anchor you use and note they have to be names not ids because there will be multiple anchors named .topLeft in your app
      var topLeft = group.get('.topLeft')[0];
      var topRight = group.get('.topRight')[0];
      var bottomRight = group.get('.bottomRight')[0];
      var bottomLeft = group.get('.bottomLeft')[0];
      var image = group.get('.image')[0];
    
      var anchorX = activeAnchor.getX();
      var anchorY = activeAnchor.getY();
    
      // update anchor positions
      switch (activeAnchor.getName()) {
        case 'topLeft':
          //When topLeft is being dragged, topRight has to update in the Y-axis
          //And bottomLeft has to update in the X-axis
          topRight.setY(anchorY);
          bottomLeft.setX(anchorX);
          break;
        case 'topRight':
          topLeft.setY(anchorY);
          bottomRight.setX(anchorX);
          break;
        case 'bottomRight':
          bottomLeft.setY(anchorY);
          topRight.setX(anchorX); 
          break;
        case 'bottomLeft':
          bottomRight.setY(anchorY);
          topLeft.setX(anchorX); 
          break;
      }
    
      image.setPosition(topLeft.getPosition());
    
      //New height and width are calculated with a little math
      //by calculating the distance between the update anchor positions.
      var width = topRight.getX() - topLeft.getX();
      var height = bottomLeft.getY() - topLeft.getY();
      if(width && height) {
        image.setSize(width, height);
      }
    }
    

    The update function only takes one argument: the activeAnchor which is the anchor being dragged.

    After that, it selects the other anchors within the group (using static names that you need to give each node and keep consistent throughout your app) so that we can translate their positions while the activeAnchor is being dragged.

    The switch statement can get pretty large if you use 8 anchors instead of 4. This is because you need to consider translating almost all the other anchors while dragging one of them.

    For an 8 anchor example: If you drag the topLeft anchor, you need to update the y position of the topRight anchor, the x position of the bottomLeft anchor, and for the topMid and leftMid anchors you need to adjust both the x,y values to stay in between the other anchors.

    After updating the anchor position, the function handles the logic to resize the shape. Notice that the shape is selected by var image = group.get('.image')[0]; But, what you can do is use the get function to select by type and do something like:

    var shape = group.get('Shape')[0];

    Obviously this will work best if you just have 1 shape per group (to transform) + 4 or 8 anchors.

    Let me know if you have any other questions or comments! Good luck!