Skip to content

Instantly share code, notes, and snippets.

@pavelpower
Last active January 3, 2016 00:29
Show Gist options
  • Save pavelpower/8382795 to your computer and use it in GitHub Desktop.
Save pavelpower/8382795 to your computer and use it in GitHub Desktop.
create special Image for html
var FactorySuperImage = (function() {
function _onLoad() {
this.isloaded = true;
if (this.width === 0 && this.height === 0)
this.load_deferred.reject( new Error('image is not loaded') );
else {
this.originalSize = {
width: this.width,
height: this.height
};
this.load_deferred.resolve(this);
}
}
function _onerror(e) {
this.load_deferred.reject(e);
}
function load(path) {
this.src = path;
return this.load_deferred;
}
function setSize(w, h) {
this.width = w;
this.height = h;
}
return function (options) {
var image = new Image();
image.load_deferred = $.Deferred();
image.isloaded = false;
image.onload = _onLoad;
image.onerror= _onerror;
image.load = load;
image.setSize = setSize;
return image;
}
})();
var i = new FactorySuperImage();
i.load('/images/login/watermark.png?123123')
.done(function(b) { console.log('->', b, b.originalSize); })
.fail(function(e) { console.log(e) });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment