Skip to content

Commit 55ba13d

Browse files
committed
resize canvas
1 parent 888529e commit 55ba13d

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

demo/index.html

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,20 +259,34 @@ <h2 class="sub-headline">
259259
<div class="header-container-image">
260260
<img class="head-image" src="./pexels-niklas-jeromin-12734294.jpg" />
261261
</div>
262-
<canvas id="myCanvas" width="200" height="200" style="border:1px solid #000000;"></canvas>
262+
<canvas id="myCanvas" width="5000" height="5000" style="border:1px solid #000000;"></canvas>
263+
<canvas id="myCanvas2" width="5000" height="5000" style="border:1px solid #000000;"></canvas>
264+
<canvas id="myCanvas3" width="5000" height="5000" style="border:1px solid #000000;"></canvas>
265+
<canvas id="myCanvas4" width="5000" height="5000" style="border:1px solid #000000;"></canvas>
266+
<canvas id="myCanvas5" width="5000" height="5000" style="border:1px solid #000000;"></canvas>
267+
263268

264269
<script>
265-
var canvas = document.getElementById('myCanvas');
266-
var ctx = canvas.getContext('2d');
267-
var centerX = canvas.width / 2;
268-
var centerY = canvas.height / 2;
269-
var radius = 70;
270-
271-
ctx.beginPath();
272-
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
273-
ctx.fillStyle = 'blue';
274-
ctx.fill();
275-
ctx.stroke();
270+
const canvases = [
271+
document.getElementById('myCanvas'),
272+
document.getElementById('myCanvas2'),
273+
document.getElementById('myCanvas3'),
274+
document.getElementById('myCanvas4'),
275+
document.getElementById('myCanvas5')
276+
];
277+
278+
canvases.forEach(canvas => {
279+
const ctx = canvas.getContext('2d');
280+
const centerX = canvas.width / 2;
281+
const centerY = canvas.height / 2;
282+
const radius = 1500;
283+
284+
ctx.beginPath();
285+
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
286+
ctx.fillStyle = 'blue';
287+
ctx.fill();
288+
ctx.stroke();
289+
});
276290
</script>
277291

278292
<!-- The Modal -->

src/ScreenCapture.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,38 @@ const handleAdoptedStyleSheets = (doc, clone, shadowNodeId) => {
351351
}
352352
}
353353

354+
355+
const resizeCanvas = (canvas, pct) => {
356+
return new Promise((resolve, reject) => {
357+
const cw = canvas.width;
358+
const ch = canvas.height;
359+
360+
// Create a copy of the original canvas
361+
const originalCanvas = document.createElement("canvas");
362+
originalCanvas.width = cw;
363+
originalCanvas.height = ch;
364+
const originalCtx = originalCanvas.getContext("2d");
365+
originalCtx.drawImage(canvas, 0, 0);
366+
367+
// Create an off-screen canvas for resizing
368+
const resizedCanvas = document.createElement("canvas");
369+
resizedCanvas.width = cw * pct;
370+
resizedCanvas.height = ch * pct;
371+
const rctx = resizedCanvas.getContext("2d");
372+
373+
// Draw the image from the original canvas onto the off-screen resized canvas
374+
rctx.drawImage(originalCanvas, 0, 0, cw, ch, 0, 0, cw * pct, ch * pct);
375+
376+
// Get the resized image data
377+
const resizedCanvasData = resizedCanvas.toDataURL();
378+
resolve(resizedCanvasData);
379+
});
380+
};
381+
354382
const deepClone = (host) => {
355383
let shadowNodeId = 1;
356384

357-
const cloneNode = (node, parent, shadowRoot) => {
385+
const cloneNode = async (node, parent, shadowRoot) => {
358386
const walkTree = (nextn, nextp, innerShadowRoot) => {
359387
while (nextn) {
360388
cloneNode(nextn, nextp, innerShadowRoot);
@@ -371,7 +399,12 @@ const deepClone = (host) => {
371399

372400
if (node instanceof HTMLCanvasElement) {
373401
try {
374-
clone.setAttribute("bb-canvas-data", resizeImage(node.toDataURL(), 750, 750));
402+
const resizedCanvasData = await resizeCanvas(node, 0.50); // Scale down by 50%
403+
404+
// const resizedImage = await resizeImage(node.toDataURL(), 3500, 3500);
405+
// const originalCanvas = resizeTo(node, 0.50);
406+
407+
clone.setAttribute("bb-canvas-data", resizedCanvasData);
375408
} catch (exp) {
376409
console.warn("Gleap: Failed to clone canvas data.", exp);
377410
}

0 commit comments

Comments
 (0)