123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import CachedImage from './CachedImage';
- class Images {
-
- constructor(callback){
- this.images = {};
- this.imageBroken = {};
- this.callback = callback;
- }
-
-
-
- _tryloadBrokenUrl (url, brokenUrl, imageToLoadBrokenUrlOn) {
-
- if (url === undefined || imageToLoadBrokenUrlOn === undefined) return;
- if (brokenUrl === undefined) {
- console.warn("No broken url image defined");
- return;
- }
-
-
- imageToLoadBrokenUrlOn.onerror = () => {
- console.error("Could not load brokenImage:", brokenUrl);
-
- };
-
-
- imageToLoadBrokenUrlOn.image.src = brokenUrl;
- }
-
-
- _redrawWithImage (imageToRedrawWith) {
- if (this.callback) {
- this.callback(imageToRedrawWith);
- }
- }
-
-
-
- load (url, brokenUrl) {
-
- var cachedImage = this.images[url];
- if (cachedImage) return cachedImage;
-
-
- var img = new CachedImage();
-
-
- this.images[url] = img;
-
-
- img.image.onload = () => {
-
- this._fixImageCoordinates(img.image);
- img.init();
- this._redrawWithImage(img);
- };
-
-
- img.image.onerror = () => {
- console.error("Could not load image:", url);
-
- this._tryloadBrokenUrl(url, brokenUrl, img);
- };
-
-
- img.image.src = url;
-
-
- return img;
- }
-
- _fixImageCoordinates(imageToCache) {
- if (imageToCache.width === 0) {
- document.body.appendChild(imageToCache);
- imageToCache.width = imageToCache.offsetWidth;
- imageToCache.height = imageToCache.offsetHeight;
- document.body.removeChild(imageToCache);
- }
- }
- }
- export default Images;
|