//清除 var clearButton = wrapper.querySelector("[data-action=clear]"); clearButton.addEventListener("click", function (event) { signaturePad.clear(); });
//變更顏色 var changeColorButton = wrapper.querySelector("[data-action=change-color]"); changeColorButton.addEventListener("click", function (event) { var r = Math.round(Math.random() * 255); var g = Math.round(Math.random() * 255); var b = Math.round(Math.random() * 255); var color = "rgb(" + r + "," + g + "," + b +")";
signaturePad.penColor = color; });
//還原 var undoButton = wrapper.querySelector("[data-action=undo]"); undoButton.addEventListener("click", function (event) { var data = signaturePad.toData();
if (data) { data.pop(); // remove the last dot or line signaturePad.fromData(data); } });
var savePNGButton = wrapper.querySelector("[data-action=save-png]"); savePNGButton.addEventListener("click", function (event) { if (signaturePad.isEmpty()) { alert("Please provide a signature first."); } else { var dataURL = signaturePad.toDataURL(); download(dataURL, "signature.png"); } });
var saveJPGButton = wrapper.querySelector("[data-action=save-jpg]"); saveJPGButton.addEventListener("click", function (event) { if (signaturePad.isEmpty()) { alert("Please provide a signature first."); } else { var dataURL = signaturePad.toDataURL("image/jpeg"); download(dataURL, "signature.jpg"); } });
var saveSVGButton = wrapper.querySelector("[data-action=save-svg]"); saveSVGButton.addEventListener("click", function (event) { if (signaturePad.isEmpty()) { alert("Please provide a signature first."); } else { var dataURL = signaturePad.toDataURL('image/svg+xml'); download(dataURL, "signature.svg"); } });
// 下載 functiondownload(dataURL, filename) { if (navigator.userAgent.indexOf("Safari") > -1 && navigator.userAgent.indexOf("Chrome") === -1) { window.open(dataURL); } else { var blob = dataURLToBlob(dataURL); var url = window.URL.createObjectURL(blob);
var a = document.createElement("a"); a.style = "display: none"; a.href = url; a.download = filename;
document.body.appendChild(a); a.click();
window.URL.revokeObjectURL(url); } }
//產生blob functiondataURLToBlob(dataURL) { // Code taken from https://github.com/ebidel/filer.js var parts = dataURL.split(';base64,'); var contentType = parts[0].split(":")[1]; var raw = window.atob(parts[1]); var rawLength = raw.length; var uInt8Array = newUint8Array(rawLength);
for (var i = 0; i < rawLength; ++i) { uInt8Array[i] = raw.charCodeAt(i); }