You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by sh...@apache.org on 2020/12/27 10:46:35 UTC

[incubator-echarts-examples] branch next updated: not update barely changed images.

This is an automated email from the ASF dual-hosted git repository.

shenyi pushed a commit to branch next
in repository https://gitbox.apache.org/repos/asf/incubator-echarts-examples.git


The following commit(s) were added to refs/heads/next by this push:
     new e9ddd37  not update barely changed images.
e9ddd37 is described below

commit e9ddd375017ef85c3bb0d33e8b55c58ff9e651ec
Author: pissang <bm...@gmail.com>
AuthorDate: Sun Dec 27 18:46:14 2020 +0800

    not update barely changed images.
---
 common/compareImage.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 common/optionDeps.js   |  1 -
 package.json           |  3 +++
 tool/build-example.js  | 32 ++++++++++++++++++++++++++------
 4 files changed, 76 insertions(+), 7 deletions(-)

diff --git a/common/compareImage.js b/common/compareImage.js
new file mode 100644
index 0000000..930027b
--- /dev/null
+++ b/common/compareImage.js
@@ -0,0 +1,47 @@
+const fs = require('fs');
+const PNG = require('pngjs').PNG;
+const pixelmatch = require('pixelmatch');
+
+function readPNG(path) {
+    return new Promise(resolve => {
+        fs.createReadStream(path)
+            .pipe(new PNG())
+            .on('parsed', function () {
+                resolve({
+                    data: this.data,
+                    width: this.width,
+                    height: this.height
+                });
+            });
+    });
+}
+
+module.exports.compareImage = function(targetPath, sourcePath, threshold) {
+    if (!fs.existsSync(targetPath)) {
+        return true;
+    }
+
+    return Promise.all([
+        readPNG(targetPath),
+        readPNG(sourcePath)
+    ]).then(([expectedImg, actualImg]) => {
+        let width = expectedImg.width;
+        let height = expectedImg.height;
+        if (
+            (width !== actualImg.width)
+          || (height !== actualImg.height)
+        ) {
+            return {diffRatio: 1}
+        }
+        const diffPNG = new PNG({width, height});
+        let diffPixelsCount = pixelmatch(expectedImg.data, actualImg.data, diffPNG.data, width, height, {
+            threshold
+        });
+        let totalPixelsCount = width * height;
+
+        return {
+            diffRatio: diffPixelsCount / totalPixelsCount,
+            diffPNG
+        };
+    });
+}
\ No newline at end of file
diff --git a/common/optionDeps.js b/common/optionDeps.js
index 3311879..e7e9b05 100644
--- a/common/optionDeps.js
+++ b/common/optionDeps.js
@@ -59,7 +59,6 @@ const RENDERERS_MAP_REVERSE = {
     'CanvasRenderer': 'canvas'
 }
 
-
 // Component that will be injected automatically in preprocessor
 // These should be excluded util find they were used explicitly.
 const MARKERS = ['markLine', 'markArea', 'markPoint'];
diff --git a/package.json b/package.json
index 4cfa2a5..7acedbe 100644
--- a/package.json
+++ b/package.json
@@ -32,9 +32,12 @@
     "mini-css-extract-plugin": "^0.8.2",
     "node-static": "^0.7.11",
     "open": "^7.1.0",
+    "pixelmatch": "^5.2.1",
+    "pngjs": "^6.0.0",
     "prettier": "^2.2.1",
     "sass.js": "^0.11.1",
     "sassjs-loader": "^2.0.0",
+    "seedrandom": "^3.0.5",
     "sharp": "^0.26.2",
     "style-loader": "^1.2.1",
     "typescript": "^4.1.3",
diff --git a/tool/build-example.js b/tool/build-example.js
index b55cd01..f1891be 100644
--- a/tool/build-example.js
+++ b/tool/build-example.js
@@ -11,6 +11,7 @@ const util = require('util');
 const chalk = require('chalk');
 const sharp = require('sharp');
 const fse = require('fs-extra');
+const { compareImage } = require('../common/compareImage');
 
 function optionToJson(obj, prop) {
     let json = JSON.stringify(obj, function(key, value) {
@@ -131,6 +132,7 @@ async function takeScreenshot(
         await waitTime(200);
         await waitTime(screenshotDelay || 0);
         const fileBase = `${rootDir}public/${sourceFolder}/${thumbFolder}/${basename}`;
+        const filePathTmpRaw = `${fileBase}-tmp-raw.png`;
         const filePathTmp = `${fileBase}-tmp.png`;
         const filePath = `${fileBase}.png`;
 
@@ -147,16 +149,34 @@ async function takeScreenshot(
         fs.writeFileSync(`${rootDir}public/${sourceFolder}/option/${basename}.json`, optionStr, 'utf-8');
         // }
 
-        console.log(filePath);
-
         await page.screenshot({
-            path: filePathTmp,
+            path: filePathTmpRaw,
             type: 'png'
         });
-        await sharp(filePathTmp).resize(OUTPUT_IMAGE_WIDTH, OUTPUT_IMAGE_WIDTH * DEFAULT_PAGE_RATIO)
-            .toFile(filePath);
+
+        await sharp(filePathTmpRaw)
+            .resize(OUTPUT_IMAGE_WIDTH, OUTPUT_IMAGE_WIDTH * DEFAULT_PAGE_RATIO)
+            .toFile(filePathTmp);
+
+        const {diffRatio} = await compareImage(filePath, filePathTmp, 0.1);
+
+        console.log(filePath);
+        if (diffRatio < 0.01) {
+            console.log('Not changed');
+        }
+        else {
+            console.log(diffRatio);
+            fs.copyFileSync(filePathTmp, filePath);
+            await convertToWebP(filePath);
+        }
+
+        try {
+            fs.unlinkSync(filePathOld);
+        }
+        catch (e) {}
+
+        fs.unlinkSync(filePathTmpRaw);
         fs.unlinkSync(filePathTmp);
-        await convertToWebP(filePath);
     }
     catch (e) {
         console.error(url);


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org