Improve image loading, do not use setTimeout.
authorAlexandre Segura <mex.zktk@gmail.com>
Thu, 23 Feb 2017 20:01:52 +0100
changeset 375 8551c5f9189e
parent 373 03e72c3922d2 (current diff)
parent 374 01a7e6b61314 (diff)
child 377 a0ae89d96c27
Improve image loading, do not use setTimeout.
--- a/src_js/iconolab-bundle/src/components/editor/Canvas.vue	Thu Feb 23 17:51:40 2017 +0100
+++ b/src_js/iconolab-bundle/src/components/editor/Canvas.vue	Thu Feb 23 20:01:52 2017 +0100
@@ -123,6 +123,28 @@
             loaded: function(loaded) {
                 if (!loaded) { return; }
 
+                var bcr = this.paper.node.getBoundingClientRect();
+
+                console.log('Viewport: %s x %s', bcr.width, bcr.height);
+
+                var viewBox = [0 , 0, this.imageWidth, this.imageHeight];
+                var viewport = {
+                    width: bcr.width,
+                    height: bcr.height
+                };
+
+                Object.assign(this, {
+                    viewBox: viewBox,
+                    viewport: viewport,
+                });
+
+                var handlerSize = 15 * Math.min(viewBox[2], viewBox[3]) / viewport.width;
+
+                this.$refs.rect.handlerSize = handlerSize;
+                this.$refs.free.handlerRadius = handlerSize / 2;
+
+                this.paper.attr({"viewBox": this.viewBox});
+
                 if (this.annotation) {
                     this.loadAnnotation();
                 } else {
@@ -178,37 +200,11 @@
 
                 console.log('Image: %s x %s', img.width, img.height);
 
-                // FIXME
-                // Image is actually NOT loaded at this step
-                setTimeout(() => {
-
-                    var bcr = this.paper.node.getBoundingClientRect();
-
-                    console.log('Viewport: %s x %s', bcr.width, bcr.height);
-
-                    var viewBox = [0 , 0, img.width, img.height];
-                    var viewport = {
-                        width: bcr.width,
-                        height: bcr.height
-                    };
-
-                    Object.assign(this, {
-                        imageWidth: img.width,
-                        imageHeight: img.height,
-                        viewBox: viewBox,
-                        viewport: viewport,
-                    });
-
-                    var handlerSize = 15 * Math.min(viewBox[2], viewBox[3]) / viewport.width;
-
-                    this.$refs.rect.handlerSize = handlerSize;
-                    this.$refs.free.handlerRadius = handlerSize / 2;
-
-                    this.paper.attr({"viewBox": this.viewBox});
-
-                    this.loaded = true;
-
-                }, 100);
+                Object.assign(this, {
+                    imageWidth: img.width,
+                    imageHeight: img.height,
+                    loaded: true
+                });
 
             }
             img.src = this.image;