Browse Source

Image caching

Patrick Augusto 4 years ago
parent
commit
4991b7973c
1 changed files with 10 additions and 4 deletions
  1. 10 4
      Pixel.js

+ 10 - 4
Pixel.js

@@ -1,13 +1,17 @@
-var canvas;
+var canvas = {};
 
 const draw = function(ctx, source, coords) {
     ctx.clearRect(0, 0, canvas.element.width, canvas.element.height);
     const img = new Image();
+    img.src = source;
 
-    img.onload = function() {
+    if (!img.naturalWidth) {
+        img.onload = function() {
+            ctx.drawImage(img, coords.x, coords.y);
+        };
+    } else {
         ctx.drawImage(img, coords.x, coords.y);
-    };
-    img.src = source;
+    }
 };
 
 // const dotProduct = function(U, V) {
@@ -64,6 +68,8 @@ const init = function() {
         //     get c() { return { x: canvas.boundary.bottom,               y: canvas.boundary.right                 }; },
         //     get d() { return { x: canvas.boundary.bottom,               y: canvas.boundary.left + window.scrollX }; }
         // }
+
+        fragments: []
     };
     
     canvas.element.setAttribute("width",  500); // window.getComputedStyle(document.body).getPropertyValue("width"));