Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to limit the behavior of the transformer and prevent it from going beyond the canvas #254

Open
lush-99 opened this issue Nov 28, 2024 · 4 comments

Comments

@lush-99
Copy link

lush-99 commented Nov 28, 2024

How to limit the behavior of the transformer and prevent it from going beyond the canvas

@lush-99
Copy link
Author

lush-99 commented Nov 28, 2024

@lavrton
Copy link
Member

lavrton commented Dec 2, 2024

Did you already find the solution? I see the demo has some working code.

@lush-99
Copy link
Author

lush-99 commented Dec 20, 2024

Did you already find the solution? I see the demo has some working code.

i find some code to it, but i find konva give me a error although i setting x=0
this is my solution:

    handleShapeTransform(e) {
      const currentSelectedRect = this.currentRects.find((rect) => rect.name === this.selectedRectName)
      const originX = e.target.x()
      const originY = e.target.y()
      const originScaleX = e.target.scaleX()
      const originScaleY = e.target.scaleY()
      const originWidth = e.target.width()
      const originHeight = e.target.height()
      if (originX < 0) {
        e.target.setAttrs({
          x: 0,
          scaleX: 1,
          width: currentSelectedRect.width,
        })
      }
      if (originY < 0) {
        e.target.setAttrs({
          y: 0,
          scaleY: 1,
          height: currentSelectedRect.height,
        })
      }
      if (originX + originScaleX * originWidth > this.stageConfig.width) {
        e.target.setAttrs({
          x: currentSelectedRect.x,
          scaleX: 1,
          width: this.stageConfig.width - currentSelectedRect.x,
        })
      }
      if (originY + originScaleY * originHeight > this.stageConfig.height) {
        e.target.setAttrs({
          y: currentSelectedRect.y,
          scaleY: 1,
          height: this.stageConfig.height - currentSelectedRect.y,
        })
      }
      this.currentRects = this.currentRects.map((item) => {
        if (item.name === this.selectedRectName) {
          return {
            ...item,
            x: e.target.x(),
            y: e.target.y(),
            scaleX: 1,
            scaleY: 1,
            width: e.target.width() * e.target.scaleX(),
            height: e.target.height() * e.target.scaleY(),
          }
        }
        return item
      })
    },

The problem I'm facing now is that even if I set the coordinates to 0, but e.target.x() < 0, konva gives me wrong coordinates, probably because of insufficient precision.

@lush-99
Copy link
Author

lush-99 commented Dec 20, 2024

sometimes konva give me a error x: -5.551115123125783e-17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants