User Input & Media Exports
An overview of handling user input, listening to key presses, exporting the artwork at different resolutions and passing in values as URL parameters.
Exporting the Canvas
// Listen for a key press event
document.addEventListener('keydown', function(event) {
// Check if the pressed key is the 'e' key (you can change this to any key you want)
if (event.key === 's') {
// Call the function to export the canvas as a PNG image
exportCanvasAsPNG(canvas);
}
});function exportCanvasAsPNG(canvas) {
// Create an "a" element to trigger the download
const link = document.createElement('a');
// Convert the canvas to a data URL
const dataURL = canvas.toDataURL('image/png');
// Set the href attribute of the "a" element to the data URL
link.href = dataURL;
// Set the download attribute to specify the file name
link.download = 'canvas-export.png';
// Simulate a click on the "a" element to trigger the download
link.click();
}
Different Resolution Export
URL Parameters
Last updated