kekland / croppy

A fully customizable image cropper for Flutter, in Flutter
MIT License
105 stars 30 forks source link

Using crop shapes that don't fit in the crop rect #6

Open Ahmed-gamal-elmahdy opened 1 year ago

Ahmed-gamal-elmahdy commented 1 year ago

when using a shape with negative values in cubic, it breaks the crop this is the path used

CropShape myCustomShapFn(vg.PathBuilder builder, Size size) { final path = builder .moveTo(size.width*0.4967222,size.height*0.5193478) .cubicTo(size.width*0.6900278,size.height*0.5193478,size.width*0.9666944,size.height*-0.1071594,size.width*0.9666944,size.height*0.0549420) .cubicTo(size.width*0.9666944,size.height*0.1450290,size.width*0.8323889,size.height*0.9920290,size.width*0.4940556,size.height*0.9920290) .cubicTo(size.width*0.3007500,size.height*0.9920290,size.width*0.0214722,size.height*0.1888551,size.width*0.0214722,size.height*0.0312174) .cubicTo(size.width*0.0214722,size.height*-0.0588406,size.width*0.1584444,size.height*0.5193478,size.width*0.4967222,size.height*0.5193478) .close() .toPath(); return CropShape.custom(path); }

this is the shape as drawn by painter image

kekland commented 1 year ago

Hi! Thanks for the report. Will investigate soon

kekland commented 1 year ago

This seems to occur when the AABB of the convex hull of the samples of the custom path does not equal the cropping rectangle. There are two solutions here:

  1. Change the custom crop shape so that it guarantees that the AABB will be equal to the crop rect. This behavior can be enforced with assertions.
  2. Tweak the rendering code to make sure that it can work with crop shapes that do not fill the entire AABB.

I'll try option 2 and post my findings here.

kekland commented 1 year ago

Unfortunately, after a long day of trying to fix this behavior, I came to the conclusion that it would probably require me to rewrite the FitPolygonInQuad solver completely. This solver assumes that the polygon passed has an AABB that is equal to the crop rect AABB, and if it's not, then it will return a smaller AABB, which is what I observed when playing around with invalid crop shapes. Therefore, I would suggest tweaking the crop shape so that it always fits in the crop rect.

I'll add this in the documentation and enable asserts so that this can't happen.