Przeglądaj źródła

:lipstick: better trimming

LI JIAHAO 6 lat temu
rodzic
commit
36dbe9b12a
2 zmienionych plików z 23 dodań i 8 usunięć
  1. 13 5
      dist/webshot.js
  2. 10 3
      src/webshot.ts

+ 13 - 5
dist/webshot.js

@@ -51,15 +51,23 @@ class Webshot extends CallableInstance {
                                 this.height = boundary;
                                 boundary = null;
                                 x = Math.floor(this.width / 2);
-                                let flag = 0;
+                                let flag = false;
+                                let cnt = 0;
                                 for (let y = this.height - 1; y >= 0; y--) {
                                     const idx = (this.width * y + x) << 2;
                                     if (this.data[idx] !== 255) {
-                                        boundary = y;
                                         if (!flag)
-                                            flag = 1;
-                                        else
-                                            break;
+                                            cnt++;
+                                        flag = true;
+                                    }
+                                    else {
+                                        if (flag)
+                                            cnt++;
+                                        flag = false;
+                                    }
+                                    if (cnt === 3) {
+                                        boundary = y;
+                                        break;
                                     }
                                 }
                                 if (boundary != null) {

+ 10 - 3
src/webshot.ts

@@ -64,13 +64,20 @@ class Webshot extends CallableInstance {
 
                   boundary = null;
                   x = Math.floor(this.width / 2);
-                  let flag = 0;
+                  let flag = false;
+                  let cnt = 0;
                   for (let y = this.height - 1; y >= 0; y--) {
                     const idx = (this.width * y + x) << 2;
                     if (this.data[idx] !== 255) {
+                      if (!flag) cnt++;
+                      flag = true;
+                    } else {
+                      if (flag) cnt++;
+                      flag = false;
+                    }
+                    if (cnt === 3) {
                       boundary = y;
-                      if (!flag) flag = 1;
-                      else break;
+                      break;
                     }
                   }
                   if (boundary != null) {