/**
* Ken Fyrstenberg Nilsen
* Abidas Software
*/
/**
* nowSave and savePdf are custom functions by ulmdesign - mediamaster.eu
**/
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
/**
* Demonstrates how to download a canvas an image with a single
* direct click on a link.
*/
function doCanvas() {
/* draw something */
ctx.fillStyle = '#f90';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#fff';
ctx.font = '60px sans-serif';
ctx.fillText('Code Project', 10, canvas.height / 2 - 15);
ctx.font = '26px sans-serif';
ctx.fillText('Click link below to save this as image', 15, canvas.height / 2 + 35);
}
/**
* This is the function that will take care of image extracting and
* setting proper filename for the download.
* IMPORTANT: Call it from within a onclick event.
*/
function downloadCanvas(link, canvasId, filename) {
link.href = document.getElementById(canvasId).toDataURL();
link.download = filename;
}
/**
* The event handler for the link's onclick event. We give THIS as a
* parameter (=the link element), ID of the canvas and a filename.
*/
function nowSave(){
var namefile = prompt("insert name of file to save in png");
if(namefile === "") {
alert("You must enter name of file")
} else {
downloadCanvas(document.getElementById("download"), 'canvas', namefile + ".png");
}
}
function savePDF(){
try {
canvas.getContext('2d');
var imgData = canvas.toDataURL("image/jpeg", 1.0);
var pdf = new jsPDF('p', 'mm', [297, 210]);
pdf.addImage(imgData, 'JPEG', 5, 5);
var namefile = prompt("insert name of file");
pdf.save(namefile + ".pdf");
} catch(e) {
alert("Error description: " + e.message);
}
}
document.getElementById('download').addEventListener('click', nowSave, false);
document.getElementById('downloadpdf').addEventListener('click', savePDF, false);
/**
* Draw something to canvas
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.