merge-images
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -7,3 +7,4 @@ // Defaults | ||
height: undefined, | ||
Canvas: undefined | ||
Canvas: undefined, | ||
crossOrigin: undefined | ||
}; | ||
@@ -35,2 +36,3 @@ | ||
var img = new Image(); | ||
img.crossOrigin = options.crossOrigin; | ||
img.onerror = function () { return reject(new Error('Couldn\'t load image')); }; | ||
@@ -37,0 +39,0 @@ img.onload = function () { return resolve(Object.assign({}, source, { img: img })); }; |
@@ -7,81 +7,83 @@ (function (global, factory) { | ||
// Defaults | ||
var defaultOptions = { | ||
format: 'image/png', | ||
quality: 0.92, | ||
width: undefined, | ||
height: undefined, | ||
Canvas: undefined | ||
}; | ||
// Defaults | ||
var defaultOptions = { | ||
format: 'image/png', | ||
quality: 0.92, | ||
width: undefined, | ||
height: undefined, | ||
Canvas: undefined, | ||
crossOrigin: undefined | ||
}; | ||
// Return Promise | ||
var mergeImages = function (sources, options) { | ||
if ( sources === void 0 ) sources = []; | ||
if ( options === void 0 ) options = {}; | ||
// Return Promise | ||
var mergeImages = function (sources, options) { | ||
if ( sources === void 0 ) sources = []; | ||
if ( options === void 0 ) options = {}; | ||
return new Promise(function (resolve) { | ||
options = Object.assign({}, defaultOptions, options); | ||
return new Promise(function (resolve) { | ||
options = Object.assign({}, defaultOptions, options); | ||
// Setup browser/Node.js specific variables | ||
var canvas = options.Canvas ? new options.Canvas() : window.document.createElement('canvas'); | ||
var Image = options.Canvas ? options.Canvas.Image : window.Image; | ||
if (options.Canvas) { | ||
options.quality *= 100; | ||
} | ||
// Load sources | ||
var images = sources.map(function (source) { return new Promise(function (resolve, reject) { | ||
// Convert sources to objects | ||
if (source.constructor.name !== 'Object') { | ||
source = { src: source }; | ||
// Setup browser/Node.js specific variables | ||
var canvas = options.Canvas ? new options.Canvas() : window.document.createElement('canvas'); | ||
var Image = options.Canvas ? options.Canvas.Image : window.Image; | ||
if (options.Canvas) { | ||
options.quality *= 100; | ||
} | ||
// Resolve source and img when loaded | ||
var img = new Image(); | ||
img.onerror = function () { return reject(new Error('Couldn\'t load image')); }; | ||
img.onload = function () { return resolve(Object.assign({}, source, { img: img })); }; | ||
img.src = source.src; | ||
}); }); | ||
// Load sources | ||
var images = sources.map(function (source) { return new Promise(function (resolve, reject) { | ||
// Convert sources to objects | ||
if (source.constructor.name !== 'Object') { | ||
source = { src: source }; | ||
} | ||
// Get canvas context | ||
var ctx = canvas.getContext('2d'); | ||
// Resolve source and img when loaded | ||
var img = new Image(); | ||
img.crossOrigin = options.crossOrigin; | ||
img.onerror = function () { return reject(new Error('Couldn\'t load image')); }; | ||
img.onload = function () { return resolve(Object.assign({}, source, { img: img })); }; | ||
img.src = source.src; | ||
}); }); | ||
// When sources have loaded | ||
resolve(Promise.all(images) | ||
.then(function (images) { | ||
// Set canvas dimensions | ||
var getSize = function (dim) { return options[dim] || Math.max.apply(Math, images.map(function (image) { return image.img[dim]; })); }; | ||
canvas.width = getSize('width'); | ||
canvas.height = getSize('height'); | ||
// Get canvas context | ||
var ctx = canvas.getContext('2d'); | ||
// Draw images to canvas | ||
images.forEach(function (image) { | ||
ctx.globalAlpha = image.opacity ? image.opacity : 1; | ||
return ctx.drawImage(image.img, image.x || 0, image.y || 0); | ||
}); | ||
// When sources have loaded | ||
resolve(Promise.all(images) | ||
.then(function (images) { | ||
// Set canvas dimensions | ||
var getSize = function (dim) { return options[dim] || Math.max.apply(Math, images.map(function (image) { return image.img[dim]; })); }; | ||
canvas.width = getSize('width'); | ||
canvas.height = getSize('height'); | ||
if (options.Canvas && options.format === 'image/jpeg') { | ||
// Resolve data URI for node-canvas jpeg async | ||
return new Promise(function (resolve) { | ||
canvas.toDataURL(options.format, { | ||
quality: options.quality, | ||
progressive: false | ||
}, function (err, jpeg) { | ||
if (err) { | ||
throw err; | ||
} | ||
resolve(jpeg); | ||
}); | ||
// Draw images to canvas | ||
images.forEach(function (image) { | ||
ctx.globalAlpha = image.opacity ? image.opacity : 1; | ||
return ctx.drawImage(image.img, image.x || 0, image.y || 0); | ||
}); | ||
} | ||
// Resolve all other data URIs sync | ||
return canvas.toDataURL(options.format, options.quality); | ||
})); | ||
}); | ||
}; | ||
if (options.Canvas && options.format === 'image/jpeg') { | ||
// Resolve data URI for node-canvas jpeg async | ||
return new Promise(function (resolve) { | ||
canvas.toDataURL(options.format, { | ||
quality: options.quality, | ||
progressive: false | ||
}, function (err, jpeg) { | ||
if (err) { | ||
throw err; | ||
} | ||
resolve(jpeg); | ||
}); | ||
}); | ||
} | ||
return mergeImages; | ||
// Resolve all other data URIs sync | ||
return canvas.toDataURL(options.format, options.quality); | ||
})); | ||
}); | ||
}; | ||
return mergeImages; | ||
}))); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "merge-images", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Easily compose images together without messing around with canvas", | ||
@@ -45,3 +45,3 @@ "main": "dist/index.umd.js", | ||
"ava": "^0.25.0", | ||
"camelcase": "^4.0.0", | ||
"camelcase": "^5.0.0", | ||
"canvas": "^1.6.2", | ||
@@ -53,6 +53,6 @@ "coveralls": "^3.0.0", | ||
"pify": "^3.0.0", | ||
"rollup": "^0.55.3", | ||
"rollup-plugin-buble": "^0.18.0", | ||
"rollup": "^0.59.0", | ||
"rollup-plugin-buble": "^0.19.2", | ||
"xo": "^0.18.2" | ||
} | ||
} |
@@ -169,4 +169,11 @@ # merge-images | ||
##### options.crossOrigin | ||
Type: `string`<br> | ||
Default: `undefined` | ||
The `crossOrigin` attribute that `Image` instances should use. e.g `Anonymous` to [support CORS-enabled images](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image). | ||
## License | ||
MIT © Luke Childs |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22121
144
179