Skip to content

Commit 4a1f642

Browse files
committed
Update loadImage to version 2.7.0.
1 parent 931a437 commit 4a1f642

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

test/vendor/load-image.js

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,28 @@
99
* http://www.opensource.org/licenses/MIT
1010
*/
1111

12-
/*global define, module, window, document, URL, webkitURL, FileReader */
12+
/* global define, URL, webkitURL, FileReader */
1313

1414
;(function ($) {
1515
'use strict'
1616

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)
38-
// Store the file type for resize processing:
39-
img._type = file.type
33+
url = img._objectURL = loadImage.createObjectURL(file)
4034
} else if (typeof file === 'string') {
4135
url = file
4236
if (options && options.crossOrigin) {
@@ -53,10 +47,8 @@
5347
var target = e.target
5448
if (target && target.result) {
5549
img.src = target.result
56-
} else {
57-
if (callback) {
58-
callback(e)
59-
}
50+
} else if (callback) {
51+
callback(e)
6052
}
6153
})
6254
}
@@ -66,11 +58,36 @@
6658
(window.URL && URL.revokeObjectURL && URL) ||
6759
(window.webkitURL && webkitURL)
6860

61+
function revokeHelper (img, options) {
62+
if (img._objectURL && !(options && options.noRevoke)) {
63+
loadImage.revokeObjectURL(img._objectURL)
64+
delete img._objectURL
65+
}
66+
}
67+
6968
loadImage.isInstanceOf = function (type, obj) {
7069
// Cross-frame instanceof check
7170
return Object.prototype.toString.call(obj) === '[object ' + type + ']'
7271
}
7372

73+
loadImage.transform = function (img, options, callback, file, data) {
74+
callback(loadImage.scale(img, options, data), data)
75+
}
76+
77+
loadImage.onerror = function (img, event, file, callback, options) {
78+
revokeHelper(img, options)
79+
if (callback) {
80+
callback.call(img, event)
81+
}
82+
}
83+
84+
loadImage.onload = function (img, event, file, callback, options) {
85+
revokeHelper(img, options)
86+
if (callback) {
87+
loadImage.transform(img, options, callback, file, {})
88+
}
89+
}
90+
7491
// Transform image coordinates, allows to override e.g.
7592
// the canvas orientation based on the orientation option,
7693
// gets canvas, options passed as arguments:
@@ -109,8 +126,7 @@
109126
return newOptions
110127
}
111128

112-
// Canvas render method, allows to override the
113-
// rendering e.g. to work around issues on iOS:
129+
// Canvas render method, allows to implement a different rendering algorithm:
114130
loadImage.renderImageToCanvas = function (
115131
canvas,
116132
img,
@@ -137,8 +153,7 @@
137153
return canvas
138154
}
139155

140-
// This method is used to determine if the target image
141-
// should be a canvas element:
156+
// Determines if the target image should be a canvas element:
142157
loadImage.hasCanvasOption = function (options) {
143158
return options.canvas || options.crop || !!options.aspectRatio
144159
}
@@ -148,7 +163,7 @@
148163
// Returns a canvas object if the browser supports canvas
149164
// and the hasCanvasOption method returns true or a canvas
150165
// object is passed as image, else the scaled image:
151-
loadImage.scale = function (img, options) {
166+
loadImage.scale = function (img, options, data) {
152167
options = options || {}
153168
var canvas = document.createElement('canvas')
154169
var useCanvas = img.getContext ||
@@ -189,7 +204,7 @@
189204
}
190205
}
191206
if (useCanvas) {
192-
options = loadImage.getTransformedOptions(img, options)
207+
options = loadImage.getTransformedOptions(img, options, data)
193208
sourceX = options.left || 0
194209
sourceY = options.top || 0
195210
if (options.sourceWidth) {

0 commit comments

Comments
 (0)