Skip to content

Commit 52d77ff

Browse files
committed
add fallback loading to cross-domain images vuematerial#424
1 parent 47b17b8 commit 52d77ff

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/components/mdCard/mdCardMediaCover.vue

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
}
4747
},
4848
mounted() {
49+
const applyBackground = (darkness = 0.6) => {
50+
if (this.mdTextScrim) {
51+
this.applyScrimColor(darkness);
52+
} else if (this.mdSolid) {
53+
this.applySolidColor(darkness);
54+
}
55+
};
4956
let image = this.$el.querySelector('img');
5057
5158
if (image && (this.mdTextScrim || this.mdSolid)) {
@@ -57,12 +64,8 @@
5764
darkness = 0.7;
5865
}
5966
60-
if (this.mdTextScrim) {
61-
this.applyScrimColor(darkness);
62-
} else if (this.mdSolid) {
63-
this.applySolidColor(darkness);
64-
}
65-
});
67+
applyBackground(darkness);
68+
}, applyBackground);
6669
}
6770
}
6871
};

src/components/mdImage/mdImage.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
},
3232
methods: {
3333
analyzeLightness(image) {
34+
const applyLoad = () => {
35+
this.loaded = true;
36+
};
37+
3438
getImageLightness(image, (lightness) => {
3539
let limit = 256;
3640
let darkness = (Math.abs(limit - lightness) * 100 / limit + 15) / 100;
@@ -39,10 +43,8 @@
3943
this.applyBlack = true;
4044
}
4145
42-
this.$nextTick(() => {
43-
this.loaded = true;
44-
});
45-
});
46+
this.$nextTick(applyLoad);
47+
}, applyLoad);
4648
},
4749
createImage() {
4850
this.loaded = false;

src/core/utils/getImageLightness.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
const getImageLightness = (image, onLoad) => {
1+
const getImageLightness = (image, onLoad, onError) => {
22
let canvas = document.createElement('canvas');
33

4+
image.crossOrigin = 'Anonymous';
5+
46
image.onload = function() {
57
let colorSum = 0;
68
let ctx;
@@ -31,6 +33,8 @@ const getImageLightness = (image, onLoad) => {
3133

3234
onLoad(Math.floor(colorSum / (this.width * this.height)));
3335
};
36+
37+
image.onerror = onError;
3438
};
3539

3640
export default getImageLightness;

0 commit comments

Comments
 (0)