Skip to content

Commit

Permalink
fix: allow drawing outside canvas for a smoother signature (#765)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

- Drawing outside of the canvas will record data outside the canvas
- Update SignatureEvent to store the original event, x, y, pressure
- move and up events are attached once down is triggered and they are on the window/ownerDocument target
  • Loading branch information
UziTech authored Apr 5, 2024
1 parent 2dc3e4b commit 4d31ade
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 87 deletions.
5 changes: 5 additions & 0 deletions docs/css/signature-pad.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ body {
-ms-flex: 1;
flex: 1;
border: 1px solid #f4f4f4;
border-radius: 4px;
}

.signature-pad--body.empty {
background-color: #333;
}

.signature-pad--body canvas {
Expand Down
6 changes: 5 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</span>

<div id="signature-pad" class="signature-pad">
<div class="signature-pad--body">
<div id="canvas-wrapper" class="signature-pad--body">
<canvas></canvas>
</div>
<div class="signature-pad--footer">
Expand All @@ -59,6 +59,10 @@
background</button>
</div>
</div>

<div>
<button type="button" class="button" data-action="open-in-window">Open in Window</button>
</div>
</div>
</div>

Expand Down
19 changes: 19 additions & 0 deletions docs/js/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const wrapper = document.getElementById("signature-pad");
const canvasWrapper = document.getElementById("canvas-wrapper");
const clearButton = wrapper.querySelector("[data-action=clear]");
const changeBackgroundColorButton = wrapper.querySelector("[data-action=change-background-color]");
const changeColorButton = wrapper.querySelector("[data-action=change-color]");
Expand All @@ -9,6 +10,7 @@ const savePNGButton = wrapper.querySelector("[data-action=save-png]");
const saveJPGButton = wrapper.querySelector("[data-action=save-jpg]");
const saveSVGButton = wrapper.querySelector("[data-action=save-svg]");
const saveSVGWithBackgroundButton = wrapper.querySelector("[data-action=save-svg-with-background]");
const openInWindowButton = wrapper.querySelector("[data-action=open-in-window]");
let undoData = [];
const canvas = wrapper.querySelector("canvas");
const signaturePad = new SignaturePad(canvas, {
Expand Down Expand Up @@ -182,3 +184,20 @@ saveSVGWithBackgroundButton.addEventListener("click", () => {
download(dataURL, "signature.svg");
}
});

openInWindowButton.addEventListener("click", () => {
var externalWin = window.open('', '', `width=${canvas.width / window.devicePixelRatio},height=${canvas.height / window.devicePixelRatio}`);
canvas.style.width = "100%";
canvas.style.height = "100%";
externalWin.onresize = resizeCanvas;
externalWin.document.body.style.margin = '0';
externalWin.document.body.appendChild(canvas);
canvasWrapper.classList.add("empty");
externalWin.onbeforeunload = () => {
canvas.style.width = "";
canvas.style.height = "";
canvasWrapper.classList.remove("empty");
canvasWrapper.appendChild(canvas);
resizeCanvas();
};
})
2 changes: 1 addition & 1 deletion docs/js/signature_pad.umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/js/signature_pad.umd.min.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 4d31ade

Please sign in to comment.