Skip to content

Commit

Permalink
Restore <canvas> → <img> freezer
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrozenberg committed Sep 3, 2021
1 parent 7ceacc9 commit 7568ca8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build-system/tasks/visual-diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ const REMOVE_AMP_SCRIPTS_SNIPPET = fs.readFileSync(
path.resolve(__dirname, 'snippets/remove-amp-scripts.js'),
'utf8'
);
const FREEZE_CANVAS_IMAGE_SNIPPET = fs.readFileSync(
path.resolve(__dirname, 'snippets/freeze-canvas-image.js'),
'utf8'
);
const REMOVE_NO_SCRIPT_ELEMENT_SNIPPET = fs.readFileSync(
path.resolve(__dirname, 'snippets/remove-no-script.js'),
'utf8'
Expand Down Expand Up @@ -636,6 +640,7 @@ async function snapshotWebpages(browser, webpages) {
// prepare it for snapshotting on Percy. See comments inside the
// snippet files for description of each.
await page.evaluate(REMOVE_AMP_SCRIPTS_SNIPPET);
await page.evaluate(FREEZE_CANVAS_IMAGE_SNIPPET);
await page.evaluate(REMOVE_NO_SCRIPT_ELEMENT_SNIPPET);

// Create a default set of snapshot options for Percy and modify
Expand Down
14 changes: 14 additions & 0 deletions build-system/tasks/visual-diff/snippets/freeze-canvas-image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @fileoverview This file is executed via Puppeteer's page.evaluate on a
* document to copy the current image data of the canvas to an attribute so that
* it will be passed in the snapshots to Percy.
*/

const canvases = document.querySelectorAll('canvas');
canvases.forEach((canvas) => {
const img = document.createElement('img');
img.style.width = '100%'; // eslint-disable-line local/no-style-property-setting
img.style.height = '100%'; // eslint-disable-line local/no-style-property-setting
img.setAttribute('src', canvas.toDataURL());
canvas.replaceWith(img);
});

0 comments on commit 7568ca8

Please sign in to comment.