mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-06 18:30:57 +00:00
Images placed correctly
This commit is contained in:
parent
3db05d0abe
commit
507134697c
@ -544,21 +544,15 @@ const DraggableUtils = {
|
|||||||
angle: rotateAngle, // Store rotation
|
angle: rotateAngle, // Store rotation
|
||||||
};
|
};
|
||||||
|
|
||||||
// Auxiliary variables
|
|
||||||
let widthAdjusted = page.getWidth();
|
|
||||||
let heightAdjusted = page.getHeight();
|
|
||||||
const rotation = page.getRotation();
|
|
||||||
|
|
||||||
// Normalize page rotation angle
|
// Normalize page rotation angle
|
||||||
let normalizedAngle = rotation.angle % 360;
|
let normalizedAngle = page.getRotation() % 360;
|
||||||
if (normalizedAngle < 0) {
|
if (normalizedAngle < 0) {
|
||||||
normalizedAngle += 360;
|
normalizedAngle += 360;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adjust page dimensions for rotated pages
|
// Determine the viewed page dimensions based on the normalized rotation angle
|
||||||
if (normalizedAngle === 90 || normalizedAngle === 270) {
|
let viewedPageWidth = (normalizedAngle === 90 || normalizedAngle === 270) ? page.getHeight() : page.getWidth();
|
||||||
[widthAdjusted, heightAdjusted] = [heightAdjusted, widthAdjusted];
|
let viewedPageHeight = (normalizedAngle === 90 || normalizedAngle === 270) ? page.getWidth() : page.getHeight();
|
||||||
}
|
|
||||||
|
|
||||||
const draggablePositionRelative = {
|
const draggablePositionRelative = {
|
||||||
x: draggablePositionPixels.x / offsetWidth,
|
x: draggablePositionPixels.x / offsetWidth,
|
||||||
@ -569,51 +563,58 @@ const DraggableUtils = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const draggablePositionPdf = {
|
const draggablePositionPdf = {
|
||||||
x: draggablePositionRelative.x * widthAdjusted,
|
x: draggablePositionRelative.x * viewedPageWidth,
|
||||||
y: draggablePositionRelative.y * heightAdjusted,
|
y: draggablePositionRelative.y * viewedPageHeight,
|
||||||
width: draggablePositionRelative.width * widthAdjusted,
|
width: draggablePositionRelative.width * viewedPageWidth,
|
||||||
height: draggablePositionRelative.height * heightAdjusted,
|
height: draggablePositionRelative.height * viewedPageHeight,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Calculate position based on normalized page rotation
|
// Calculate position based on normalized page rotation
|
||||||
let x = draggablePositionPdf.x;
|
let x = draggablePositionPdf.x;
|
||||||
let y = heightAdjusted - draggablePositionPdf.y - draggablePositionPdf.height;
|
let y = viewedPageHeight - draggablePositionPdf.y - draggablePositionPdf.height;
|
||||||
|
|
||||||
let originx = x + draggablePositionPdf.width / 2;
|
|
||||||
let originy = heightAdjusted - draggablePositionPdf.y - draggablePositionPdf.height / 2;
|
|
||||||
|
|
||||||
if (normalizedAngle === 90) {
|
if (normalizedAngle === 90) {
|
||||||
x = draggablePositionPdf.y + draggablePositionPdf.height;
|
x = draggablePositionPdf.y;
|
||||||
y = draggablePositionPdf.x;
|
y = draggablePositionPdf.x;
|
||||||
} else if (normalizedAngle === 180) {
|
} else if (normalizedAngle === 180) {
|
||||||
x = widthAdjusted - draggablePositionPdf.x;
|
x = viewedPageWidth - draggablePositionPdf.x - draggablePositionPdf.width;
|
||||||
y = draggablePositionPdf.y + draggablePositionPdf.height;
|
y = draggablePositionPdf.y;
|
||||||
} else if (normalizedAngle === 270) {
|
} else if (normalizedAngle === 270) {
|
||||||
x = heightAdjusted - draggablePositionPdf.y - draggablePositionPdf.height;
|
x = viewedPageHeight - draggablePositionPdf.y - draggablePositionPdf.height;
|
||||||
y = widthAdjusted - draggablePositionPdf.x;
|
y = viewedPageWidth - draggablePositionPdf.x - draggablePositionPdf.width;
|
||||||
}
|
}
|
||||||
// let angle = draggablePositionPixels.angle % 360;
|
|
||||||
// if (angle < 0) angle += 360; // Normalize to positive angle
|
// Convert rotation angle to radians
|
||||||
const radians = -draggablePositionPixels.angle; // Convert angle to radians
|
let pageRotationInRadians = PDFLib.degreesToRadians(normalizedAngle);
|
||||||
|
const rotationInRadians = pageRotationInRadians - draggablePositionPixels.angle;
|
||||||
|
|
||||||
|
// Calculate the center of the image
|
||||||
|
const imageCenterX = x + draggablePositionPdf.width / 2;
|
||||||
|
const imageCenterY = y + draggablePositionPdf.height / 2;
|
||||||
|
|
||||||
|
// Apply transformations to rotate the image about its center
|
||||||
page.pushOperators(
|
page.pushOperators(
|
||||||
PDFLib.pushGraphicsState(),
|
PDFLib.pushGraphicsState(),
|
||||||
PDFLib.concatTransformationMatrix(1, 0, 0, 1, originx, originy),
|
PDFLib.concatTransformationMatrix(1, 0, 0, 1, imageCenterX, imageCenterY), // Translate to center
|
||||||
PDFLib.concatTransformationMatrix(
|
PDFLib.concatTransformationMatrix(
|
||||||
Math.cos(radians),
|
Math.cos(rotationInRadians),
|
||||||
Math.sin(radians),
|
Math.sin(rotationInRadians),
|
||||||
-Math.sin(radians),
|
-Math.sin(rotationInRadians),
|
||||||
Math.cos(radians),
|
Math.cos(rotationInRadians),
|
||||||
0,
|
0,
|
||||||
0
|
0
|
||||||
),
|
), // Rotate
|
||||||
PDFLib.concatTransformationMatrix(1, 0, 0, 1, -1 * originx, -1 * originy)
|
PDFLib.concatTransformationMatrix(1, 0, 0, 1, -imageCenterX, -imageCenterY) // Translate back
|
||||||
);
|
);
|
||||||
|
|
||||||
page.drawImage(pdfImageObject, {
|
page.drawImage(pdfImageObject, {
|
||||||
x: x,
|
x: x,
|
||||||
y: y,
|
y: y,
|
||||||
width: draggablePositionPdf.width,
|
width: draggablePositionPdf.width,
|
||||||
height: draggablePositionPdf.height,
|
height: draggablePositionPdf.height,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Restore the graphics state
|
||||||
page.pushOperators(PDFLib.popGraphicsState());
|
page.pushOperators(PDFLib.popGraphicsState());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user