Utils/extractCanvasImage.js

/**
 * Extracts the canvas data and returns base64 data generated by PIXI renderer
 * You can use this data in combination with {@link module:File:tryWriteFile|tryWriteFile}
 * to save a screenshot of the entire canvas.
 *
 * @function extractCanvasImage
 * @since 1.0.0
 * @memberof module:Utils
 *
 * @param {number} [quality=0.1] - The quality of the image to produce
 *
 * @returns {string} returns the base64 data string.
 * @example
 * import {extractCanvasImage, tryWriteFile} from 'feni-tools'
 *
 * const canvasData = extractCanvasImage()
 *
 * // Now lets write the data to a file on the machine
 *
 * tryWriteFile(path/to/new/file, canvasData, 'base64')
 *   .then('Success writing screenshot')
 *
 *
 */
export default function extractCanvasImage (quality = 0.1) {
  const renderer = window.Graphics._renderer
  const stage = renderer._lastObjectRendered
  renderer.render(stage)
  const data = renderer.view.toDataURL('image/png', quality)
  return data
}