mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-05-24 10:52:00 +00:00
Bug/498/signature slow firefox mobile (#3322)
# Description of Changes Please provide a summary of the changes, including: - What was changed - Why the change was made - Any challenges encountered Closes #(498) --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details. --------- Co-authored-by: Reece Browne <reece@stirling.pdf>
This commit is contained in:
parent
1c27944329
commit
6da84338dc
@ -26,7 +26,6 @@ window.addEventListener("keydown", (event) => {
|
|||||||
|
|
||||||
function undoDraw() {
|
function undoDraw() {
|
||||||
const data = signaturePad.toData();
|
const data = signaturePad.toData();
|
||||||
|
|
||||||
if (data && data.length > 0) {
|
if (data && data.length > 0) {
|
||||||
const removed = data.pop();
|
const removed = data.pop();
|
||||||
undoData.push(removed);
|
undoData.push(removed);
|
||||||
@ -35,7 +34,6 @@ function undoDraw() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function redoDraw() {
|
function redoDraw() {
|
||||||
|
|
||||||
if (undoData.length > 0) {
|
if (undoData.length > 0) {
|
||||||
const data = signaturePad.toData();
|
const data = signaturePad.toData();
|
||||||
data.push(undoData.pop());
|
data.push(undoData.pop());
|
||||||
@ -52,24 +50,18 @@ function addDraggableFromPad() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getCroppedCanvasDataUrl(canvas) {
|
function getCroppedCanvasDataUrl(canvas) {
|
||||||
let originalCtx = canvas.getContext('2d');
|
let originalCtx = canvas.getContext('2d', { willReadFrequently: true });
|
||||||
let originalWidth = canvas.width;
|
let originalWidth = canvas.width;
|
||||||
let originalHeight = canvas.height;
|
let originalHeight = canvas.height;
|
||||||
let imageData = originalCtx.getImageData(0, 0, originalWidth, originalHeight);
|
let imageData = originalCtx.getImageData(0, 0, originalWidth, originalHeight);
|
||||||
|
|
||||||
let minX = originalWidth + 1,
|
let minX = originalWidth + 1, maxX = -1, minY = originalHeight + 1, maxY = -1;
|
||||||
maxX = -1,
|
|
||||||
minY = originalHeight + 1,
|
|
||||||
maxY = -1,
|
|
||||||
x = 0,
|
|
||||||
y = 0,
|
|
||||||
currentPixelColorValueIndex;
|
|
||||||
|
|
||||||
for (y = 0; y < originalHeight; y++) {
|
for (let y = 0; y < originalHeight; y++) {
|
||||||
for (x = 0; x < originalWidth; x++) {
|
for (let x = 0; x < originalWidth; x++) {
|
||||||
currentPixelColorValueIndex = (y * originalWidth + x) * 4;
|
let idx = (y * originalWidth + x) * 4;
|
||||||
let currentPixelAlphaValue = imageData.data[currentPixelColorValueIndex + 3];
|
let alpha = imageData.data[idx + 3];
|
||||||
if (currentPixelAlphaValue > 0) {
|
if (alpha > 0) {
|
||||||
if (minX > x) minX = x;
|
if (minX > x) minX = x;
|
||||||
if (maxX < x) maxX = x;
|
if (maxX < x) maxX = x;
|
||||||
if (minY > y) minY = y;
|
if (minY > y) minY = y;
|
||||||
@ -81,14 +73,14 @@ function getCroppedCanvasDataUrl(canvas) {
|
|||||||
let croppedWidth = maxX - minX;
|
let croppedWidth = maxX - minX;
|
||||||
let croppedHeight = maxY - minY;
|
let croppedHeight = maxY - minY;
|
||||||
if (croppedWidth < 0 || croppedHeight < 0) return null;
|
if (croppedWidth < 0 || croppedHeight < 0) return null;
|
||||||
let cuttedImageData = originalCtx.getImageData(minX, minY, croppedWidth, croppedHeight);
|
let cutImageData = originalCtx.getImageData(minX, minY, croppedWidth, croppedHeight);
|
||||||
|
|
||||||
let croppedCanvas = document.createElement('canvas'),
|
let croppedCanvas = document.createElement('canvas');
|
||||||
croppedCtx = croppedCanvas.getContext('2d');
|
let croppedCtx = croppedCanvas.getContext('2d');
|
||||||
|
|
||||||
croppedCanvas.width = croppedWidth;
|
croppedCanvas.width = croppedWidth;
|
||||||
croppedCanvas.height = croppedHeight;
|
croppedCanvas.height = croppedHeight;
|
||||||
croppedCtx.putImageData(cuttedImageData, 0, 0);
|
croppedCtx.putImageData(cutImageData, 0, 0);
|
||||||
|
|
||||||
return croppedCanvas.toDataURL();
|
return croppedCanvas.toDataURL();
|
||||||
}
|
}
|
||||||
@ -114,10 +106,20 @@ function resizeCanvas() {
|
|||||||
signaturePad.clear();
|
signaturePad.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
new IntersectionObserver((entries, observer) => {
|
const debounce = (fn, delay = 100) => {
|
||||||
|
let timer;
|
||||||
|
return (...args) => {
|
||||||
|
clearTimeout(timer);
|
||||||
|
timer = setTimeout(() => fn(...args), delay);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const debouncedResize = debounce(resizeCanvas, 200);
|
||||||
|
|
||||||
|
new IntersectionObserver((entries) => {
|
||||||
if (entries.some((entry) => entry.intersectionRatio > 0)) {
|
if (entries.some((entry) => entry.intersectionRatio > 0)) {
|
||||||
resizeCanvas();
|
debouncedResize();
|
||||||
}
|
}
|
||||||
}).observe(signaturePadCanvas);
|
}).observe(signaturePadCanvas);
|
||||||
|
|
||||||
new ResizeObserver(resizeCanvas).observe(signaturePadCanvas);
|
new ResizeObserver(debouncedResize).observe(signaturePadCanvas);
|
||||||
|
@ -43,13 +43,13 @@
|
|||||||
</div>
|
</div>
|
||||||
<script type="module" th:src="@{'/pdfjs-legacy/pdf.mjs'}"></script>
|
<script type="module" th:src="@{'/pdfjs-legacy/pdf.mjs'}"></script>
|
||||||
<div class="tab-group show-on-file-selected">
|
<div class="tab-group show-on-file-selected">
|
||||||
<div class="tab-container" th:title="#{sign.upload}" th:data-title="#{sign.upload}">
|
<div class="tab-container"th:data-title="#{sign.upload}">
|
||||||
<div
|
<div
|
||||||
th:replace="~{fragments/common :: fileSelector(name='image-upload', disableMultipleFiles=false, multipleInputsForSingleRequest=true, accept='image/*', inputText=#{imgPrompt})}">
|
th:replace="~{fragments/common :: fileSelector(name='image-upload', disableMultipleFiles=false, multipleInputsForSingleRequest=true, accept='image/*', inputText=#{imgPrompt})}">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="tab-container drawing-pad-container" th:title="#{sign.draw}" th:data-title="#{sign.draw}">
|
<div class="tab-container drawing-pad-container" th:data-title="#{sign.draw}">
|
||||||
<canvas id="drawing-pad-canvas"></canvas>
|
<canvas id="drawing-pad-canvas"></canvas>
|
||||||
<br>
|
<br>
|
||||||
<button id="clear-signature" class="btn btn-outline-danger mt-2" onclick="signaturePad.clear()"
|
<button id="clear-signature" class="btn btn-outline-danger mt-2" onclick="signaturePad.clear()"
|
||||||
@ -62,7 +62,7 @@
|
|||||||
onclick="redoDraw()"></button>
|
onclick="redoDraw()"></button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="tab-container" th:title="#{sign.saved}" th:data-title="#{sign.saved}">
|
<div class="tab-container" th:data-title="#{sign.saved}">
|
||||||
<div class="saved-signatures-section" th:if="${not #lists.isEmpty(signatures)}">
|
<div class="saved-signatures-section" th:if="${not #lists.isEmpty(signatures)}">
|
||||||
|
|
||||||
<!-- Preview Modal -->
|
<!-- Preview Modal -->
|
||||||
@ -134,7 +134,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="tab-container" th:title="#{sign.text}" th:data-title="#{sign.text}">
|
<div class="tab-container" th:data-title="#{sign.text}">
|
||||||
<label class="form-check-label" for="sigText" th:text="#{text}"></label>
|
<label class="form-check-label" for="sigText" th:text="#{text}"></label>
|
||||||
<textarea class="form-control" id="sigText" name="sigText" rows="3"></textarea>
|
<textarea class="form-control" id="sigText" name="sigText" rows="3"></textarea>
|
||||||
<label th:text="#{font}"></label>
|
<label th:text="#{font}"></label>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user