marcglasberg / matrix4_transform

Flutter package: Helper math class for easily creating Matrix4 transformations, that you can use in Container's transform parameter and elsewhere.
Other
58 stars 5 forks source link

problem with from contractor #7

Closed mehdizarepour closed 4 years ago

mehdizarepour commented 4 years ago

Hi! I want to transform a widget on onScaleUpdate event then I used Matrix4Transform like this:

GestureDetector(
  onScaleStart: (d) {
    dx = d.focalPoint.dx;
    dy = d.focalPoint.dy;
  },
  onScaleUpdate: (d) {
    setState(() {
      matrix = Matrix4Transform()
          .translate(x: d.focalPoint.dx - dx, y: d.focalPoint.dy - dy)
          .scale(d.scale)
          .rotate(d.rotation)
          .matrix4;
    });
  },
  child: Center(
    child: Transform(
      transform: matrix,
      alignment: FractionalOffset.center,
      child: Container(width: 100, height: 100, color: Colors.red),
    ),
  ),
)

It works fine, but the problem is, I want to keep last transform on Matrix4 object and use it on the next transform, then I've tried using from contractor like this:

onScaleUpdate: (d) {
    setState(() {
      matrix = Matrix4Transform.fom(matrix)
          .translate(x: d.focalPoint.dx - dx, y: d.focalPoint.dy - dy)
          .scale(d.scale)
          .rotate(d.rotation)
          .matrix4;
    });
  },

But it does not work how I expected, as you can see it's too sharp: am I using it wrong? any suggestion?

ezgif com-video-to-gif(2)

mehdizarepour commented 4 years ago

@marcglasberg any solutions?

marcglasberg commented 4 years ago

Don't do this. Save the cumulative scale/rotation/translate information yourself, in variables, and then calculate the Matrix4 transform from the original scale 1, rotation 0, and position 0.