Normalize translation in image editor.

pull/2/head
Matthew Chen 6 years ago
parent f01fe8e563
commit 0d26caced7

@ -92,6 +92,8 @@ public class ImageEditorTransform: NSObject {
return transform return transform
} }
// This method normalizes a "proposed" transform (self) into
// one that is guaranteed to be valid.
public func normalize(srcImageSizePixels: CGSize) -> ImageEditorTransform { public func normalize(srcImageSizePixels: CGSize) -> ImageEditorTransform {
// Normalize scaling. // Normalize scaling.
// The "src/background" image is rendered at a size that will fill // The "src/background" image is rendered at a size that will fill
@ -127,7 +129,7 @@ public class ImageEditorTransform: NSObject {
var naiveViewportMinCanvas = CGPoint.zero var naiveViewportMinCanvas = CGPoint.zero
var naiveViewportMaxCanvas = CGPoint.zero var naiveViewportMaxCanvas = CGPoint.zero
// Find the "naive" bounding box of the viewport on the canvas // Find the "naive" bounding box of the viewport on the canvas
// by projects its corners from view coordinates to canvas // by projecting its corners from view coordinates to canvas
// coordinates. // coordinates.
// //
// Due to symmetry, it should be sufficient to project 2 corners // Due to symmetry, it should be sufficient to project 2 corners
@ -163,13 +165,14 @@ public class ImageEditorTransform: NSObject {
// from the last step. // from the last step.
// //
// This is subtle. We want to clamp in canvas coordinates // This is subtle. We want to clamp in canvas coordinates
// since the translation is specified in "unit canvas" // since the min/max translation is specified by a bounding
// coordinates. However, because the translation is // box in "unit canvas" coordinates. However, because the
// applied in SRT order (scale-rotate-transform), it // translation is applied in SRT order (scale-rotate-transform),
// effectively operates in view coordinates since it is // it effectively operates in view coordinates since it is
// applied last. So we project it from view coordinates // applied last. So we project it from view coordinates
// to canvas coordinates, clamp it, then project it back // to canvas coordinates, clamp it, then project it back
// into unit view coordinates. // into unit view coordinates using the "naive" (no translation)
// transform.
let translationInView = self.unitTranslation.fromUnitCoordinates(viewBounds: viewBounds) let translationInView = self.unitTranslation.fromUnitCoordinates(viewBounds: viewBounds)
let translationInCanvas = translationInView.applyingInverse(naiveAffineTransform) let translationInCanvas = translationInView.applyingInverse(naiveAffineTransform)
// Clamp the translation to +/- maxTranslationCanvasUnit. // Clamp the translation to +/- maxTranslationCanvasUnit.

Loading…
Cancel
Save