-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Easier way to composite 3+ layers #405
Comments
Hello, given all the images are the same dimension, I'd probably use a Promises and raw pixel data approach for this, something like the following (untested): const options = {
raw: {
width: 200,
height: 100,
channels: 4
}
};
const base = sharp('color.png').raw().toBuffer();
const composite = [
'accent0.png',
'accent1.png',
'accentn.png',
'shading.png',
'highlight.png',
'lineart.png'
].reduce( function(input, overlay) {
return input.then( function(data) {
return sharp(data, options).overlayWith(overlay).raw().toBuffer();
});
}, base);
composite.then(function(data) {
// data contains the multi-composited image as raw pixel data
}); |
I've updated the code sample: - return sharp(input, options).overlayWith(overlay).raw().toBuffer();
+ return input.then( function(data) {
+ return sharp(data, options).overlayWith(overlay).raw().toBuffer();
+ }); |
Perfect, very much appreciated! |
i try it,down all source to demo,use this your code run fail.
|
Repository owner
locked and limited conversation to collaborators
Mar 20, 2018
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Sorry about digging up the old thread. After looking at that I went poking around the tests where I found "Composite three transparent PNGs into one".
What I was hoping to do was be able to chain overlayWith together like this:
sharp(layer1)
.overlayWith(layer2)
.overlayWith(layer3)
etc. as necessary.
From what I can tell though, I need to nest them in callbacks like in the test?
Essentially what I am dealing with is a dynamic number of image layers that can be composited into a single image. Each layer file has the same dimension as well as an alpha channel.
The layer stack goes something like this:
• color layer
• accent layer (0-n)
• shading
• highlight
• lineart
There can be other layers as well, but that's the most basic version.
Is there a way to achieve what I'm looking for without the callback hell?
The text was updated successfully, but these errors were encountered: