rotate method

void rotate(
  1. double width,
  2. double height,
  3. Canvas canvas
)

画布中心旋转

Implementation

void rotate(double width, double height, Canvas canvas) {
  if (rotateAngle == 0.0) return;
//    print("画布大小:$width , $height");
  // 计算画布中心轨迹圆半径
  double panelRadius = sqrt(pow(width, 2) + pow(height, 2));
  // 计算画布中心点初始弧度
  double startAngle = atan(height / width);
  // 计算画布初始中心点坐标
  Point p0 =
      Point(panelRadius * cos(startAngle), panelRadius * sin(startAngle));
  // 需要旋转的弧度
  double radian = rotateAngle * rad;
  // 计算旋转后的画布中心点坐标
  Point px = Point(panelRadius * cos(radian + startAngle),
      panelRadius * sin(radian + startAngle));

  // 先平移画布
  canvas.translate((p0.x - px.x) / 2, (p0.y - px.y) / 2);
  // 后旋转
  canvas.rotate(radian);
}