function exportRLE(region) {
  const { naturalWidth, naturalHeight } = region.currentImageEntity;

  // Prepare the canvas with sizes of image and stage
  const canvas = document.createElement("canvas");

  // We only care about physical size, so set canvas dimensions to
  // image's natural dimensions
  canvas.width = naturalWidth;
  canvas.height = naturalHeight;

  // Make canvas offscreen and invisible
  canvas.style.setProperty("position", "absolute");
  canvas.style.setProperty("bottom", "200%");
  canvas.style.setProperty("right", "200%");
  canvas.style.setProperty("opacity", "0");

  const ctx = canvas.getContext("2d");

  document.body.appendChild(canvas);

  // Restore original RLE if available
  if (region.rle && region.rle.length > 0) {
    // Apply RLE to existing image data
    const imageData = ctx.createImageData(naturalWidth, naturalHeight);

    imageData.data.set(decode(region.rle));

    ctx.putImageData(imageData, 0, 0);
  }

  const maskImage = region.getMaskImage?.();

  if (maskImage) {
    // Apply maskDataURL to existing image data
    ctx.drawImage(maskImage, 0, 0);
  }

  // If the region was changed manually, we'll have access to user tuoches
  // Render those on the canvas after RLE
  if (region.touches.length > 0) {
    region.touches.forEach((touch) => {
      // We're using relative coordinates to calculate points
      // This way we don't need to have access to Konva#Stage and
      // render relatively to the image's natural dimensions
      const { relativePoints: points } = touch.toJSON();

      /**
       * Converts any given relative (x, y) to absolute position on an image
       * @param {number} x
       * @param {number} y
       */
      const relativeToAbsolutePoint = (x, y) => {
        return [naturalWidth * (x / 100), naturalHeight * (y / 100)];
      };

      ctx.save();
      ctx.beginPath();
      ctx.moveTo(...relativeToAbsolutePoint(points[0], points[1]));

      for (let i = 0; i < points.length / 2; i++) {
        ctx.lineTo(...relativeToAbsolutePoint(points[2 * i], points[2 * i + 1]));
      }

      ctx.strokeStyle = "#000";
      ctx.lineWidth = (touch.relativeStrokeWidth / 100) * naturalWidth;
      ctx.lineCap = "round";
      ctx.lineJoin = "round";
      ctx.globalCompositeOperation = touch.compositeOperation;
      ctx.stroke();
    });
  }

  const imageData = ctx.getImageData(0, 0, naturalWidth, naturalHeight).data;

  // Grayscale pixels respecting the opacity
  for (let i = imageData.length / 4; i--; ) {
    imageData[i * 4] = imageData[i * 4 + 1] = imageData[i * 4 + 2] = imageData[i * 4 + 3];
  }

  // When finished, remove the canvas
  canvas.remove();

  return encode(imageData, imageData.length);
}
Exports region using canvas. Doesn't require Konva#Stage access
@param {Region} region Brush region