Skip to content
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

Support rotate images with rotate option #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const defaultOptions = {
width: undefined,
height: undefined,
Canvas: undefined,
crossOrigin: undefined
crossOrigin: undefined,
rotate: undefined
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't seem to be using this value anywhere. I think it can be removed.

};

// Return Promise
Expand Down Expand Up @@ -45,11 +46,26 @@ const mergeImages = (sources = [], options = {}) => new Promise(resolve => {
canvas.width = getSize('width');
canvas.height = getSize('height');

// Draw images to canvas
images.forEach(image => {
ctx.globalAlpha = image.opacity ? image.opacity : 1;
return ctx.drawImage(image.img, image.x || 0, image.y || 0);
});
// Draw images to canvas
images.forEach(image => {
ctx.globalAlpha = image.opacity ? image.opacity : 1;

let drawImage;
if (image.rotate) {
const x = image.x ? image.x + canvas.width / 2 : canvas.width / 2;
const y = image.y ? image.y + canvas.height / 2 : canvas.height / 2;
ctx.save();
ctx.translate(x, y);
ctx.rotate(image.rotate * Math.PI / 180);
ctx.translate(-x, -y);
drawImage = ctx.drawImage(image.img, image.x || 0, image.y || 0);
ctx.restore();
} else {
drawImage = ctx.drawImage(image.img, image.x || 0, image.y || 0);
}

return drawImage;
});

if (options.Canvas && options.format === 'image/jpeg') {
// Resolve data URI for node-canvas jpeg async
Expand Down