Transform (Move/Scale/Rotate) shapes with KineticJS
Asked Answered
G

3

6

I'm trying to build a transform manager for KineticJS that would build a bounding box and allow users to scale, move, and rotate an image on their canvas. I'm getting tripped up with the logic for the anchor points.

http://jsfiddle.net/mharrisn/whK2M/

I just want to allow a user to scale their image proportionally from any corner, and also rotate as the hold-drag an anchor point.

Can anyone help point me in the right direction?

Thank you!

Greenes answered 6/9, 2012 at 22:24 Comment(4)
If you need tutorials go to here: html5canvastutorials.com/kineticjs ; do you really want to rotate the image when the user drags an anchor (one of the blue squares on each corner, I guess)? looks confusing, maybe you should allow the user to choose between transform / scale / rotate contexts (do you know blender?)Carbolated
Looking at this fiddle, you could use a fifth anchor which would control the rotation and the four other anchors would control scaling.Trace
Rotation with 'dragstart' and 'dragend' and 'dragmove' would be calculated by getting the starting points and ending/current points of the mouse position to calculate an angle given the center of the image. Which would be similar to throwing your picture into microsoft word or powerpoint.Trace
have you updated the fiddle at all?Trace
W
9

Here is a proof of concept of a rotational control I've made: http://codepen.io/ArtemGr/pen/ociAD

While the control is dragged around, the dragBoundFunc is used to rotate the content alongside it:

controlGroup.setDragBoundFunc (function (pos) {
  var groupPos = group.getPosition()
  var rotation = degrees (angle (groupPos.x, groupPos.y, pos.x, pos.y))
  status.setText ('x: ' + pos.x + '; y: ' + pos.y + '; rotation: ' + rotation); layer.draw()
  group.setRotationDeg (rotation); layer.draw()
  return pos
})
Windage answered 22/2, 2013 at 22:59 Comment(0)
B
0

I am doing the same thing, and I've posted a question which is allmoast the same, but I found a link where you have the resize and move tool ready developed. So I have used the same. It does not contain the rotate tool however, but this can be a good start for you too, it is very simple and logical. Here is the link: http://www.html5canvastutorials.com/labs/html5-canvas-drag-and-drop-resize-and-invert-images/

I will come back with the rotation tool as well if I manage to get it working perfectly.

Belligerence answered 26/11, 2013 at 10:12 Comment(0)
D
0

I hope I am not late yet for posting this code snippet that I made. I had the same problem with you guys dealing with this kind of task. Its been 3 days since I tried so many workarounds to mimic the fabricjs framework capability when dealing with images and objects. I could use Fabricjs though but it seems that Kineticjs is more faster/consistent to deal with html5.

Luckily, we already have existing plugin/tool that we could easily implement together with kineticjs and this is jQuery Transform tool. SUPER THANKS TO THE AUTHOR OF THIS! Just search this on google and download it.

I hope the code below that I created would help lots of developers out there who is pulling their hair off to solve this kind of assignment.

$(function() {

    //Declare components STAGE, LAYER and TEXT
    var _stage = null;
    var _layer = null;
    var simpleText = null;
    _stage = new Kinetic.Stage({
        container: 'canvas',
        width: 640,
        height: 480
    });
    _layer = new Kinetic.Layer();

    simpleText = new Kinetic.Text({
            x: 60,
            y: 55,
            text: 'Simple Text',
            fontSize: 30,
            fontFamily: 'Calbiri',
            draggable: false,
            name:'objectInCanvas',
            id:'objectCanvas',
            fill: 'green' 
          });
     //ADD LAYER AND TEXT ON STAGE     
    _layer.add(simpleText);
    _stage.add(_layer);
    _stage.draw();

    //Add onclick event listener to the Stage to remove and add transform tool to the object 
    _stage.on('click', function(evt) {

        //Remove all objects' transform tool inside the stage 
        removeTransformToolSelection(); 

        // get the shape that was clicked on
        ishape = evt.targetNode;

        //Add and show again the transform tool to the selected object and update the stage layer
        $(ishape).transformTool('show');
        ishape.getParent().moveToTop();
        _layer.draw();

    });

    function removeTransformToolSelection(){
        //Search all objects inside the stage or layer who has the name of "objectInCanvas" using jQuery iterator and hide the transform tool. 
        $.each(_stage.find('.objectInCanvas'), function( i, child ) {
                $(child).transformTool('hide');
        });
    }

    //Event listener/Callback when selecting image using file upload element
    function handleFileSelect(evt) {

           //Remove all objects' transform tool inside the stage 
           removeTransformToolSelection(); 

           //Create image object for selected file
           var imageObj = new Image();
           imageObj.onload = function() {
                    var myImage = new Kinetic.Image({
                    x: 0,
                    y: 0,
                    image: imageObj,
                    name:'objectInCanvas',
                    draggable:false,
                    id:'id_'
             });
             //Add to layer and add transform tool
             _layer.add(myImage);
             $(myImage).transformTool();
             _layer.draw();

            }  

          //Adding source to Image object.
          var f = document.getElementById('files').files[0];
          var name = f.name;
          var url = window.URL;
          var src = url.createObjectURL(f);
          imageObj.src = src;
    }
    //Attach event listener to FILE element
    document.getElementById('files').addEventListener('change', handleFileSelect, false);

});
Drunken answered 6/2, 2014 at 8:15 Comment(2)
ooops I forgot the HTML element. <div id="canvas"></div> <input type="file" id="files" name="files[]" multiple />Gabie
what existing plugin?Torus

© 2022 - 2024 — McMap. All rights reserved.