rotateCanvasImageSource function

HTMLCanvasElement rotateCanvasImageSource(
  1. CanvasImageSource image,
  2. int width,
  3. int height, [
  4. dynamic angleDegree = 90,
])

Rotates image (a CanvasImageSource) with angleDegree.

width Width of the image. height Height of the image.

Implementation

HTMLCanvasElement rotateCanvasImageSource(
    CanvasImageSource image, int width, int height,
    [angleDegree = 90]) {
  angleDegree ??= 90;

  var canvas = HTMLCanvasElement()
    ..width = height
    ..height = width;
  var context = canvas.getContext('2d') as CanvasRenderingContext2D;

  context.translate(canvas.width / 2, canvas.height / 2);
  context.rotate(angleDegree * math.pi / 180);
  context.drawImage(image, -width / 2, -height / 2);

  return canvas;
}