Skip to content

Commit

Permalink
add fallback loading to cross-domain images vuematerial#424
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosmoura committed Feb 8, 2017
1 parent 47b17b8 commit 52d77ff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
15 changes: 9 additions & 6 deletions src/components/mdCard/mdCardMediaCover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
}
},
mounted() {
const applyBackground = (darkness = 0.6) => {
if (this.mdTextScrim) {
this.applyScrimColor(darkness);
} else if (this.mdSolid) {
this.applySolidColor(darkness);
}
};
let image = this.$el.querySelector('img');
if (image && (this.mdTextScrim || this.mdSolid)) {
Expand All @@ -57,12 +64,8 @@
darkness = 0.7;
}
if (this.mdTextScrim) {
this.applyScrimColor(darkness);
} else if (this.mdSolid) {
this.applySolidColor(darkness);
}
});
applyBackground(darkness);
}, applyBackground);
}
}
};
Expand Down
10 changes: 6 additions & 4 deletions src/components/mdImage/mdImage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
},
methods: {
analyzeLightness(image) {
const applyLoad = () => {
this.loaded = true;
};
getImageLightness(image, (lightness) => {
let limit = 256;
let darkness = (Math.abs(limit - lightness) * 100 / limit + 15) / 100;
Expand All @@ -39,10 +43,8 @@
this.applyBlack = true;
}
this.$nextTick(() => {
this.loaded = true;
});
});
this.$nextTick(applyLoad);
}, applyLoad);
},
createImage() {
this.loaded = false;
Expand Down
6 changes: 5 additions & 1 deletion src/core/utils/getImageLightness.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const getImageLightness = (image, onLoad) => {
const getImageLightness = (image, onLoad, onError) => {
let canvas = document.createElement('canvas');

image.crossOrigin = 'Anonymous';

image.onload = function() {
let colorSum = 0;
let ctx;
Expand Down Expand Up @@ -31,6 +33,8 @@ const getImageLightness = (image, onLoad) => {

onLoad(Math.floor(colorSum / (this.width * this.height)));
};

image.onerror = onError;
};

export default getImageLightness;

0 comments on commit 52d77ff

Please sign in to comment.