Search code examples
javascriptimagehtmlhtml5-canvaskineticjs

Resizing the images while keeping proportion using kinetic js canvas


I'm using this in my app:

http://www.html5canvastutorials.com/labs/html5-canvas-drag-and-drop-resize-and-invert-images/

have use anchors for resizing the image ..

What I want is to resize the image and keep its ratio maintained. I don't want it to stretch I'm able to achieve that using this peace of code.:..

  if(width) {
        image.setSize(width);
      }

But this is screwing up the anchors .. Anyone ever tried something like this..


Solution

  • I know this is a horribly old post but I needed to implement this exact thing and found the accepted answer is quite flawed - the left two drag handles move the entire image and act very much unlike an image editing program would, for instance. I am posting my solution in case someone stumbles upon this from a Google search like I did.

    // Update the positions of handles during drag.
    // This needs to happen so the dimension calculation can use the
    // handle positions to determine the new width/height.
    switch (activeHandleName) {
        case "topLeft":
            topRight.setY(activeHandle.getY());
            bottomLeft.setX(activeHandle.getX());
            break;
        case "topRight":
            topLeft.setY(activeHandle.getY());
            bottomRight.setX(activeHandle.getX());
            break;
        case "bottomRight":
            bottomLeft.setY(activeHandle.getY());
            topRight.setX(activeHandle.getX());
            break;
        case "bottomLeft":
            bottomRight.setY(activeHandle.getY());
            topLeft.setX(activeHandle.getX());
            break;
    }
    
    // Calculate new dimensions. Height is simply the dy of the handles.
    // Width is increased/decreased by a factor of how much the height changed.
    newHeight = bottomLeft.getY() - topLeft.getY();
    newWidth = image.getWidth() * newHeight / image.getHeight();
    
    // Move the image to adjust for the new dimensions.
    // The position calculation changes depending on where it is anchored.
    // ie. When dragging on the right, it is anchored to the top left,
    //     when dragging on the left, it is anchored to the top right.
    if(activeHandleName === "topRight" || activeHandleName === "bottomRight") {
        image.setPosition(topLeft.getX(), topLeft.getY());
    } else if(activeHandleName === "topLeft" || activeHandleName === "bottomLeft") {
        image.setPosition(topRight.getX() - newWidth, topRight.getY());
    }
    
    imageX = image.getX();
    imageY = image.getY();
    
    // Update handle positions to reflect new image dimensions
    topLeft.setPosition(imageX, imageY);
    topRight.setPosition(imageX + newWidth, imageY);
    bottomRight.setPosition(imageX + newWidth, imageY + newHeight);
    bottomLeft.setPosition(imageX, imageY + newHeight);
    
    // Set the image's size to the newly calculated dimensions
    if(newWidth && newHeight) {
         image.setSize(newWidth, newHeight);
    }
    

    Modified KineticJS example in JSBin: http://jsbin.com/iyimuy/1/edit