Skip to content

Commit

Permalink
Fix transform again to work again correctly, references #518
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Oct 30, 2024
1 parent 5e09542 commit 3f7bfb2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
21 changes: 14 additions & 7 deletions app/lib/renderers/renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,17 @@ abstract class Renderer<T> {
rotation ??= relative ? 0 : this.rotation;
final double nextRotation =
(relative ? rotation + this.rotation : rotation) % 360;
position ??= relative ? rect.topLeft : Offset.zero;
position ??= relative ? Offset.zero : rect.topLeft;
final expandedRect = this.expandedRect ?? rect;
var nextPosition = relative ? position + rect.topLeft : position;

final radians = this.rotation * (pi / 180);
if (rotatePosition) {
var relativePosition = nextPosition - rect.topLeft;
relativePosition = relativePosition.rotate(rect.center, rotation);
relativePosition = relativePosition.rotate(Offset.zero, radians);
nextPosition = relativePosition + rect.topLeft;
}

final radians = this.rotation * (pi / 180);

if (radians != 0) {
final rotationCos = cos(radians);
final rotationSin = sin(radians);
Expand All @@ -257,9 +257,16 @@ abstract class Renderer<T> {

scaleX = (oldScaleX * rotationCos - oldScaleY * rotationSin).abs();
scaleY = (oldScaleX * rotationSin + oldScaleY * rotationCos).abs();
final offsetX = (oldScaleX - scaleX) * rect.width / 2;
final offsetY = (oldScaleY - scaleY) * rect.height / 2;
nextPosition += Offset(offsetX, offsetY);
// Calculate offset, please use Offset#rotate(pivot, radians)
// to rotate the offset around the pivot point
// Don't forget to use the radians and the scales to calculate the offset to keep the rectangle on the same position as if they are unrotated
final rotationPoint = rect.topLeft;
final offset = Offset(
(expandedRect.left - rotationPoint.dx) * scaleX / 2,
(expandedRect.top - rotationPoint.dy) * scaleY / 2,
);
final rotatedOffset = offset.rotate(Offset.zero, radians);
nextPosition -= rotatedOffset;
}

return _transform(
Expand Down
16 changes: 8 additions & 8 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ packages:
dependency: transitive
description:
name: code_builder
sha256: f692079e25e7869c14132d39f223f8eec9830eb76131925143b2129c4bb01b37
sha256: "0ec10bf4a89e4c613960bf1e8b42c64127021740fb21640c29c909826a5eea3e"
url: "https://pub.dev"
source: hosted
version: "4.10.0"
version: "4.10.1"
collection:
dependency: "direct main"
description:
Expand Down Expand Up @@ -305,10 +305,10 @@ packages:
dependency: transitive
description:
name: csslib
sha256: "706b5707578e0c1b4b7550f64078f0a0f19dec3f50a178ffae7006b0a9ca58fb"
sha256: "09bad715f418841f976c77db72d5398dc1253c21fb9c0c7f0b0b985860b2d58e"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
version: "1.0.2"
dart_leap:
dependency: transitive
description:
Expand Down Expand Up @@ -673,10 +673,10 @@ packages:
dependency: transitive
description:
name: html
sha256: "3a7812d5bcd2894edf53dfaf8cd640876cf6cef50a8f238745c8b8120ea74d3a"
sha256: "1fc58edeaec4307368c60d59b7e15b9d658b57d7f3125098b6294153c75337ec"
url: "https://pub.dev"
source: hosted
version: "0.15.4"
version: "0.15.5"
http:
dependency: "direct main"
description:
Expand Down Expand Up @@ -988,10 +988,10 @@ packages:
dependency: "direct main"
description:
name: path_provider
sha256: fec0d61223fba3154d87759e3cc27fe2c8dc498f6386c6d6fc80d1afdd1bf378
sha256: "50c5dd5b6e1aaf6fb3a78b33f6aa3afca52bf903a8a5298f53101fdaee55bbcd"
url: "https://pub.dev"
source: hosted
version: "2.1.4"
version: "2.1.5"
path_provider_android:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"sharp": "^0.33.5",
"typescript": "^5.6.3"
},
"packageManager": "[email protected].2",
"packageManager": "[email protected].3",
"devDependencies": {
"sass": "^1.80.4"
}
Expand Down
2 changes: 1 addition & 1 deletion metadata/en-US/changelogs/121.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* Add checksums to release assets ([#753](https://github.com/LinwoodDev/Butterfly/issues/753))
* Improve scaling rotated elements ([#518](https://github.com/LinwoodDev/Butterfly/issues/518))
* Improve scaling rotated elements ([#518](https://github.com/LinwoodDev/Butterfly/issues/518) partially, offset needs to be fixed)

Read more here: https://linwood.dev/butterfly/2.2.2-rc.1

0 comments on commit 3f7bfb2

Please sign in to comment.