Convert canvas to image: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
(Created page with "Convert HTML5 canvas to image <pre> var canvas = document.getElementById('dom_id'), dataUrl = canvas.toDataURL(), imageFoo = document.createElement('img'); imag...")
 
No edit summary
 
Line 1: Line 1:
Convert HTML5 canvas to image
How to Convert & Download HTML5 Canvas as Image


<pre>
== Steps to convert HTML5 Canvas to Image ==
1. Visit any webpage containing HTML5 Canvas elements (Example: [https://www.w3schools.com/html/html5_canvas.asp HTML Canvas tutorial on W3Schools])


var canvas = document.getElementById('dom_id'),
2. Find canvas id in browser inspector:
    dataUrl = canvas.toDataURL(),
<pre lang="html">
    imageFoo = document.createElement('img');
<canvas id="myCanvas" width="270" height="160" style=" ... ">
   
Your browser does not support the canvas element.
imageFoo.src = dataUrl;
</canvas>
</pre>
 
In this example, the canvas element has an id of "myCanvas"
 
3. Open Developer Tools (e.g. [https://developer.chrome.com/docs/devtools/open Open Chrome DevTools] on {{Chrome}})
 
3-1 Switch to Console


// Style your image here
3-2 Paste this JavaScript code:
imageFoo.style.width = '800px';
<pre lang="javascript">
imageFoo.style.height = '600px';
function downloadCanvas(canvasId, filename = 'canvas-image.png') {
  const canvas = document.getElementById(canvasId);
  const dataUrl = canvas.toDataURL();
  const image = document.createElement('img');
 
  image.src = dataUrl;
  image.style.width = canvas.width + 'px';  
  image.style.height = canvas.height + 'px';


// After you are done styling it, append it to the BODY element
  const downloadUrl = dataUrl.replace('image/png', 'application/octet-stream');
// document.body.appendChild(imageFoo);
  const link = document.createElement('a');
  link.download = filename;
  link.href = downloadUrl;
  link.click();
 
  return image;
}
</pre>


var url = 'data:application/octet-stream' + dataUrl;
3-2 Execute download function:
window.open(url);
<pre lang="javascript">
downloadCanvas('myCanvas', 'my-image.png');
</pre>
</pre>


Testing result:
Notes:
* Recommend using [https://www.mozilla.org/zh-TW/firefox/ Firefox Quantum] not Chrome
* Function name: Keep downloadCanvas unchanged unless you're comfortable with JavaScript
* Parameters:
** canvasId: Must match the canvas id in HTML (e.g. 'myCanvas')
** filename: Optional, customize the download filename
 
== Tested browser version ==
* Google {{Chrome}} v. 132.0.6834.160
* Microsoft {{Edge}} v. 132.0.2957.140


References:
== References ==
* [https://stackoverflow.com/questions/18008473/convert-html5-canvas-to-img-element javascript - Convert HTML5 canvas to IMG element - Stack Overflow]
* [https://stackoverflow.com/questions/18008473/convert-html5-canvas-to-img-element javascript - Convert HTML5 canvas to IMG element - Stack Overflow]


[[Category:Tool]]
[[Category: Tool]]
[[Category: Revised with LLMs]]

Latest revision as of 23:27, 4 February 2025

How to Convert & Download HTML5 Canvas as Image

Steps to convert HTML5 Canvas to Image[edit]

1. Visit any webpage containing HTML5 Canvas elements (Example: HTML Canvas tutorial on W3Schools)

2. Find canvas id in browser inspector:

<canvas id="myCanvas" width="270" height="160" style=" ... ">
Your browser does not support the canvas element.
</canvas>

In this example, the canvas element has an id of "myCanvas"

3. Open Developer Tools (e.g. Open Chrome DevTools on Chrome Browser chrome.png )

3-1 Switch to Console

3-2 Paste this JavaScript code:

function downloadCanvas(canvasId, filename = 'canvas-image.png') {
  const canvas = document.getElementById(canvasId);
  const dataUrl = canvas.toDataURL();
  const image = document.createElement('img');
  
  image.src = dataUrl;
  image.style.width = canvas.width + 'px'; 
  image.style.height = canvas.height + 'px';

  const downloadUrl = dataUrl.replace('image/png', 'application/octet-stream');
  const link = document.createElement('a');
  link.download = filename;
  link.href = downloadUrl;
  link.click();
  
  return image;
}

3-2 Execute download function:

downloadCanvas('myCanvas', 'my-image.png');

Notes:

  • Function name: Keep downloadCanvas unchanged unless you're comfortable with JavaScript
  • Parameters:
    • canvasId: Must match the canvas id in HTML (e.g. 'myCanvas')
    • filename: Optional, customize the download filename

Tested browser version[edit]

  • Google Chrome Browser chrome.png v. 132.0.6834.160
  • Microsoft Edge v. 132.0.2957.140

References[edit]