Skip to content

Commit 982d612

Browse files
committed
Update loadImage vendor dependency.
1 parent ae2d7c3 commit 982d612

File tree

2 files changed

+67
-46
lines changed

2 files changed

+67
-46
lines changed

test/vendor/load-image-scale.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,26 @@
8585
destX,
8686
destY,
8787
destWidth,
88-
destHeight
88+
destHeight,
89+
options
8990
) {
90-
canvas
91-
.getContext('2d')
92-
.drawImage(
93-
img,
94-
sourceX,
95-
sourceY,
96-
sourceWidth,
97-
sourceHeight,
98-
destX,
99-
destY,
100-
destWidth,
101-
destHeight
102-
)
91+
var ctx = canvas.getContext('2d')
92+
if (options.imageSmoothingEnabled === false) {
93+
ctx.imageSmoothingEnabled = false
94+
} else if (options.imageSmoothingQuality) {
95+
ctx.imageSmoothingQuality = options.imageSmoothingQuality
96+
}
97+
ctx.drawImage(
98+
img,
99+
sourceX,
100+
sourceY,
101+
sourceWidth,
102+
sourceHeight,
103+
destX,
104+
destY,
105+
destWidth,
106+
destHeight
107+
)
103108
return canvas
104109
}
105110

@@ -246,7 +251,8 @@
246251
0,
247252
0,
248253
canvas.width,
249-
canvas.height
254+
canvas.height,
255+
options
250256
)
251257
sourceX = 0
252258
sourceY = 0
@@ -266,7 +272,8 @@
266272
0,
267273
0,
268274
sourceWidth,
269-
sourceHeight
275+
sourceHeight,
276+
options
270277
)
271278
}
272279
}
@@ -283,7 +290,8 @@
283290
0,
284291
0,
285292
destWidth,
286-
destHeight
293+
destHeight,
294+
options
287295
)
288296
}
289297
img.width = destWidth

test/vendor/load-image.js

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,46 @@
2727
function loadImage(file, callback, options) {
2828
var img = document.createElement('img')
2929
var url
30+
/**
31+
* Callback for the fetchBlob call.
32+
*
33+
* @param {Blob} blob Blob object
34+
* @param {Error} err Error object
35+
*/
36+
function fetchBlobCallback(blob, err) {
37+
if (err) console.log(err) // eslint-disable-line no-console
38+
if (blob && loadImage.isInstanceOf('Blob', blob)) {
39+
// eslint-disable-next-line no-param-reassign
40+
file = blob
41+
url = loadImage.createObjectURL(file)
42+
} else {
43+
url = file
44+
if (options && options.crossOrigin) {
45+
img.crossOrigin = options.crossOrigin
46+
}
47+
}
48+
img.src = url
49+
}
3050
img.onerror = function (event) {
31-
return loadImage.onerror(img, event, file, callback, options)
51+
return loadImage.onerror(img, event, file, url, callback, options)
3252
}
3353
img.onload = function (event) {
34-
return loadImage.onload(img, event, file, callback, options)
54+
return loadImage.onload(img, event, file, url, callback, options)
3555
}
3656
if (typeof file === 'string') {
37-
loadImage.fetchBlob(
38-
file,
39-
function (blob) {
40-
if (blob && loadImage.isInstanceOf('Blob', blob)) {
41-
// eslint-disable-next-line no-param-reassign
42-
file = blob
43-
url = loadImage.createObjectURL(file)
44-
} else {
45-
url = file
46-
if (options && options.crossOrigin) {
47-
img.crossOrigin = options.crossOrigin
48-
}
49-
}
50-
img.src = url
51-
},
52-
options
53-
)
57+
if (loadImage.hasMetaOption(options)) {
58+
loadImage.fetchBlob(file, fetchBlobCallback, options)
59+
} else {
60+
fetchBlobCallback()
61+
}
5462
return img
5563
} else if (
5664
loadImage.isInstanceOf('Blob', file) ||
5765
// Files are also Blob instances, but some browsers
5866
// (Firefox 3.6) support the File API but not Blobs:
5967
loadImage.isInstanceOf('File', file)
6068
) {
61-
url = img._objectURL = loadImage.createObjectURL(file)
69+
url = loadImage.createObjectURL(file)
6270
if (url) {
6371
img.src = url
6472
return img
@@ -83,16 +91,21 @@
8391
/**
8492
* Helper function to revoke an object URL
8593
*
86-
* @param {HTMLImageElement} img Image element
94+
* @param {string} url Blob Object URL
8795
* @param {object} [options] Options object
8896
*/
89-
function revokeHelper(img, options) {
90-
if (img._objectURL && !(options && options.noRevoke)) {
91-
loadImage.revokeObjectURL(img._objectURL)
92-
delete img._objectURL
97+
function revokeHelper(url, options) {
98+
if (url && url.slice(0, 5) === 'blob:' && !(options && options.noRevoke)) {
99+
loadImage.revokeObjectURL(url)
93100
}
94101
}
95102

103+
// Determines if meta data should be loaded automatically.
104+
// Requires the load image meta extension to load meta data.
105+
loadImage.hasMetaOption = function (options) {
106+
return options && options.meta
107+
}
108+
96109
// If the callback given to this function returns a blob, it is used as image
97110
// source instead of the original url and overrides the file argument used in
98111
// the onload and onerror event callbacks:
@@ -109,15 +122,15 @@
109122
callback(img, data)
110123
}
111124

112-
loadImage.onerror = function (img, event, file, callback, options) {
113-
revokeHelper(img, options)
125+
loadImage.onerror = function (img, event, file, url, callback, options) {
126+
revokeHelper(url, options)
114127
if (callback) {
115128
callback.call(img, event)
116129
}
117130
}
118131

119-
loadImage.onload = function (img, event, file, callback, options) {
120-
revokeHelper(img, options)
132+
loadImage.onload = function (img, event, file, url, callback, options) {
133+
revokeHelper(url, options)
121134
if (callback) {
122135
loadImage.transform(img, options, callback, file, {
123136
originalWidth: img.naturalWidth || img.width,

0 commit comments

Comments
 (0)