Skip to content

Commit

Permalink
Merge xclud#71
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehutch committed Feb 28, 2023
1 parent 043e259 commit 416379a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 58 deletions.
1 change: 0 additions & 1 deletion lib/crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ library crop;
import 'dart:ui' as ui;
import 'dart:math';

import 'package:collision/collision.dart';
import 'package:crop/src/matrix_decomposition.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
Expand Down
91 changes: 35 additions & 56 deletions lib/src/crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class _CropState extends State<Crop> with TickerProviderStateMixin {
final _key = GlobalKey();
final _parent = GlobalKey();
final _repaintBoundaryKey = GlobalKey();
final _childKey = GlobalKey();

double _previousScale = 1;
Offset _previousOffset = Offset.zero;
Expand Down Expand Up @@ -150,66 +151,44 @@ class _CropState extends State<Crop> with TickerProviderStateMixin {
}

void _reCenterImage([bool animate = true]) {
//final totalSize = _parent.currentContext.size;

final sz = _key.currentContext!.size!;
final s = widget.controller._scale * widget.controller._getMinScale();
final w = sz.width;
final h = sz.height;
final offset = _toVector2(widget.controller._offset);
final canvas = Rectangle.fromLTWH(0, 0, w, h);
final obb = Obb2(
center: offset + canvas.center,
width: w * s,
height: h * s,
rotation: widget.controller._rotation,
);

final bakedObb = obb.bake();

_startOffset = widget.controller._offset;
_endOffset = widget.controller._offset;

final ctl = canvas.topLeft;
final ctr = canvas.topRight;
final cbr = canvas.bottomRight;
final cbl = canvas.bottomLeft;

final ll = Line(bakedObb.topLeft, bakedObb.bottomLeft);
final tt = Line(bakedObb.topRight, bakedObb.topLeft);
final rr = Line(bakedObb.bottomRight, bakedObb.topRight);
final bb = Line(bakedObb.bottomLeft, bakedObb.bottomRight);

final tl = ll.project(ctl);
final tr = tt.project(ctr);
final br = rr.project(cbr);
final bl = bb.project(cbl);

final dtl = ll.distanceToPoint(ctl);
final dtr = tt.distanceToPoint(ctr);
final dbr = rr.distanceToPoint(cbr);
final dbl = bb.distanceToPoint(cbl);

if (dtl > 0) {
final d = _toOffset(ctl - tl);
_endOffset += d;
}

if (dtr > 0) {
final d = _toOffset(ctr - tr);
_endOffset += d;
final childWidgetSize =
(_childKey.currentContext?.findRenderObject() as RenderBox).size;

double w, h;
final ratio = childWidgetSize.aspectRatio / sz.aspectRatio;
if (childWidgetSize.aspectRatio < 1.0) {
// Vertical image. Height needs to be rescaled according to ratio.
w = sz.width;
h = sz.height / ratio;
} else {
// Horizontal or square image. Width needs to be rescaled according to ratio (for square ratio is 1.0).
w = sz.width * ratio;
h = sz.height;
}

if (dbr > 0) {
final d = _toOffset(cbr - br);
_endOffset += d;
}
if (dbl > 0) {
final d = _toOffset(cbl - bl);
_endOffset += d;
}
final canvas = Rect.fromLTWH(0, 0, w, h);
final imageBoundaries = Rect.fromCenter(
center: widget.controller._offset + canvas.center,
width: w * s * cos(vm.radians(widget.controller._rotation)).abs() +
h * s * sin(vm.radians(widget.controller._rotation)).abs(),
height: w * s * sin(vm.radians(widget.controller._rotation)).abs() +
h * s * cos(vm.radians(widget.controller._rotation)).abs());

var clampBoundaries = Rect.fromCenter(
center: Offset.zero,
width: ((imageBoundaries.width - sz.width).abs().floorToDouble()),
height: (imageBoundaries.height - sz.height).abs().floorToDouble());

final clampedOffset = Offset(
widget.controller._offset.dx
.clamp(clampBoundaries.left, clampBoundaries.right),
widget.controller._offset.dy
.clamp(clampBoundaries.top, clampBoundaries.bottom));

widget.controller._offset = _endOffset;
_startOffset = widget.controller._offset;
widget.controller._offset = _endOffset = clampedOffset;

if (animate) {
if (_controller.isCompleted || _controller.isAnimating) {
Expand Down Expand Up @@ -296,7 +275,7 @@ class _CropState extends State<Crop> with TickerProviderStateMixin {
..scale(s, s, 1),
child: FittedBox(
fit: BoxFit.cover,
child: widget.child,
child: Container(key: _childKey, child: widget.child),
),
),
);
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ environment:
dependencies:
flutter:
sdk: flutter
collision: ^0.0.3
vector_math: ^2.1.2

dev_dependencies:
Expand Down

0 comments on commit 416379a

Please sign in to comment.