Skip to content

Commit b6a8e54

Browse files
committed
Make img event handlers extensible.
1 parent fc3c0ba commit b6a8e54

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

js/load-image.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,20 @@
1717
// Loads an image for a given File object.
1818
// Invokes the callback with an img or optional canvas
1919
// element (if supported by the browser) as parameter:
20-
var loadImage = function (file, callback, options) {
20+
function loadImage (file, callback, options) {
2121
var img = document.createElement('img')
2222
var url
23-
var oUrl
24-
img.onerror = callback
25-
img.onload = function () {
26-
if (oUrl && !(options && options.noRevoke)) {
27-
loadImage.revokeObjectURL(oUrl)
28-
}
29-
if (callback) {
30-
callback(loadImage.scale(img, options))
31-
}
23+
img.onerror = function (event) {
24+
return loadImage.onerror(img, event, file, callback, options)
25+
}
26+
img.onload = function (event) {
27+
return loadImage.onload(img, event, file, callback, options)
3228
}
3329
if (loadImage.isInstanceOf('Blob', file) ||
3430
// Files are also Blob instances, but some browsers
3531
// (Firefox 3.6) support the File API but not Blobs:
3632
loadImage.isInstanceOf('File', file)) {
37-
url = oUrl = loadImage.createObjectURL(file)
33+
url = img._objectURL = loadImage.createObjectURL(file)
3834
// Store the file type for resize processing:
3935
img._type = file.type
4036
} else if (typeof file === 'string') {
@@ -53,10 +49,8 @@
5349
var target = e.target
5450
if (target && target.result) {
5551
img.src = target.result
56-
} else {
57-
if (callback) {
58-
callback(e)
59-
}
52+
} else if (callback) {
53+
callback(e)
6054
}
6155
})
6256
}
@@ -71,6 +65,21 @@
7165
return Object.prototype.toString.call(obj) === '[object ' + type + ']'
7266
}
7367

68+
loadImage.onerror = function (img, event, file, callback, options) {
69+
if (callback) {
70+
callback.call(img, event)
71+
}
72+
}
73+
74+
loadImage.onload = function (img, event, file, callback, options) {
75+
if (img._objectURL && !(options && options.noRevoke)) {
76+
loadImage.revokeObjectURL(img._objectURL)
77+
}
78+
if (callback) {
79+
callback(loadImage.scale(img, options))
80+
}
81+
}
82+
7483
// Transform image coordinates, allows to override e.g.
7584
// the canvas orientation based on the orientation option,
7685
// gets canvas, options passed as arguments:

0 commit comments

Comments
 (0)