Rotated RND resizing problem

See original GitHub issue

QUESTION I am finding it difficult to resize a RND that is rotated by xdeg around the Z axis. Is resizeHandlerStyles prop a solution? Or is it not supported by the component altogether?

Thanks

I’m using react-rnd 5.0.8

Steps to Reproduce

  1. Rotate an RND by 90deg transform: rotateZ(90deg)
  2. Resize using the right handler

Expected behavior

Should increase/decrease the width of the RND along X direction

Actual behavior

Scales the image / does some unexpected resize Handler style also looks wrong

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
pionlcommented, Sep 11, 2017

@nandukalidindi The library will not support rotation by it self. As discussed here (https://github.com/bokuweb/re-resizable/pull/129). You must use latest version and implement your logic.

Here is my implementation in ES6. You must know how big the div is. Set the handleWrapperStyle prop. Feel free to use it. Don’t know If it should be implemented by the library. @bokuweb ?

The rotation is not set on the resize div, but in children div (your div). The calculation will just fix the “handlers” to match the children rotation.


/**
 * Calculates new rotation for only left corner, will match the parents rotation, only
 * for 0 0 origin rotation
 *
 * @param {number} rotation
 * @return {number}
 */
export function calculateRotationForLeftCorner (rotation) {
    // Convert the range from 0 - 360 to 4 parts from 0 - 90 where we can detect
    // where the edges should rotated. From 0 - 45 it will rotate item down, from
    // 45 - 90 it will rotate item up
    const menuRotation = rotation - (Math.floor(rotation / 90) * 90)

    // If the rotation is 45 deg, we need to rotate the menu up
    if (menuRotation > 45) {
        return -90 + menuRotation
    }

    return menuRotation
}

export function createHandlersStyle (rotation, width, height) {
    // This will ensure that resizing will occur from correct orientation of resize handlers, not the view
    const rotationByLeftCorner = calculateRotationForLeftCorner(rotation)
    const wrapperStyle = {
        transform: `rotate(${rotationByLeftCorner}deg)`
    }

    // Detect if the item is rotated in way, where height is width, we need to adjust the resize container
    const rotationStep = Math.abs(rotation / 45)
    if ((rotationStep >= 1 && rotationStep < 3) || (rotationStep >= 5 && rotationStep < 7)) {
        wrapperStyle.width = height
        wrapperStyle.height = width
        // We need to rotate item around original center point, lets move new
        // center point into the original point
        wrapperStyle.marginTop = -width / 2
        wrapperStyle.marginLeft = -height / 2
        wrapperStyle.left = width / 2
        wrapperStyle.top = height / 2
    }
   return    wrapperStyle
}

3reactions
nandukalidindicommented, Sep 15, 2017

Sorry, I haven’t gotten a chance to try out the approach suggested by @pionl which could work for my use case. Now that handleWrapperStyle is implemented in the library itself, will give it a shot. Thanks @pionl and @bokuweb. Closing this issue for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-rnd-rotate - npm Package Health Analysis
A resizable and draggable and rotate component for React For more information about how to use this package see README.
Read more >
Resizing Handles on a Rotated Element - javascript
EDIT: I've got drag start, drag move, and drag end callbacks on each handle. My current code simply gets the new point (in...
Read more >
Why and How to Implement Random Rotate Data ...
One common data augmentation technique is random rotation. A source image is random rotated clockwise or counterclockwise by some number of ...
Read more >
Introducing A New Web Component To Drag, Resize And Rotate
With it you can add drag, resize and rotate features to your applications or websites, regardless of your web framework. Cherry on the...
Read more >
react-rnd
My question: is there any way to add css transform of RND component using style ... be) the possibility to rotate the elements...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found