Convert canvas to image: Difference between revisions
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 | How to Convert & Download HTML5 Canvas as Image | ||
== 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]) | |||
2. Find canvas id in browser inspector: | |||
<pre lang="html"> | |||
<canvas id="myCanvas" width="270" height="160" style=" ... "> | |||
Your browser does not support the canvas element. | |||
</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 | |||
3-2 Paste this JavaScript code: | |||
<pre lang="javascript"> | |||
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; | |||
} | |||
</pre> | |||
3-2 Execute download function: | |||
<pre lang="javascript"> | |||
downloadCanvas('myCanvas', 'my-image.png'); | |||
</pre> | </pre> | ||
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 == | |||
* 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
)
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