Code Monkey home page Code Monkey logo

Comments (6)

Ericxgao avatar Ericxgao commented on September 26, 2024

I am also seeing this issue.

from react-image-crop.

sekoyo avatar sekoyo commented on September 26, 2024

I used to have a zoom prop, perhaps something like that can fix it 🤔

/** A non-visual prop to keep pointer coords accurate when a parent element is scaled.
Not to be confused with the `scale` prop which scales the image itself. Defaults to 1. */
zoom?: number

https://github.com/sekoyo/react-image-crop/blob/9.1.1/src/ReactCrop.tsx#L358

https://github.com/sekoyo/react-image-crop/blob/9.1.1/src/ReactCrop.tsx#L436

Are you able to make an example of the issue on React Flow (haven't used or heard of it before)?

from react-image-crop.

c0d3ster avatar c0d3ster commented on September 26, 2024

React Flow is a library used for node based workflows. The way the zoom works under the hood is utilizing transform: scale with CSS, so you can just wrap a ReactCrop element in a scaled div to see the same effect. Here is the result of doing that with the CodeSandbox example:
https://codesandbox.io/p/sandbox/react-image-crop-demo-with-react-hooks-forked-with-zoomed-out-container-gqjwpg

It would be possible to make a CodeSandbox with React Flow in particular, but that would require more boilerplate since you need to define all your node types. In my current project, the behavior is slightly different than the example above as the cropped output is accurate to the crop area visually. It just won't let you set the crop area to larger than the zoom amount. In other words, the screenshot I attached above is the maximum selectable area.
I believe bringing that zoom prop back would do the trick in both cases though.

from react-image-crop.

sekoyo avatar sekoyo commented on September 26, 2024

Sorry I misread - looks like it's only an issue with the crop preview.

So you can modify it like:

async function canvasPreview(
  image: HTMLImageElement,
  canvas: HTMLCanvasElement,
  crop: PixelCrop,
  scale = 1,
  rotate = 0,
  appScalePc = 100,
) {
  const ctx = canvas.getContext('2d')

  if (!ctx) {
    throw new Error('No 2d context')
  }

  const scaleX = (image.naturalWidth / image.width) * (100 / appScalePc)
  const scaleY = (image.naturalHeight / image.height) * (100 / appScalePc)
  
  // ....
}

And if your transform: 'scale(50%)' is 50%, then pass 50 to appScalePc

from react-image-crop.

Ericxgao avatar Ericxgao commented on September 26, 2024

Would it still be possible to expose that zoom prop?

from react-image-crop.

Ericxgao avatar Ericxgao commented on September 26, 2024

I fixed this with the following subclass - would it be possible to expose a scale or zoom parameter as it seems like it does this?

interface ScaleCompatibleReactCropProps {
  scale?: number
}

class ScaleCompatibleReactCrop extends ReactCrop {
  props: ReactCropProps & ScaleCompatibleReactCropProps

  getBox() {
    const box = super.getBox()
    const { scale } = this.props

    if (scale !== undefined) {
      box.width /= scale
      box.height /= scale
    }

    return box
  }
}

from react-image-crop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.