提交 4f7bfcda authored 作者: gzcnkilys_admin's avatar gzcnkilys_admin

fix:修复不同比例动态计算,修复文字层的叠加问题

上级 cd5b7f87
...@@ -15,11 +15,10 @@ ...@@ -15,11 +15,10 @@
.textLayer { .textLayer {
position: absolute; position: absolute;
left: 50%; left: 0;
top: 50%; top: 0;
transform: translate(-50%, -50%); width: 100% !important;
right: auto; height: 100% !important;
bottom: auto;
overflow: hidden; overflow: hidden;
opacity: 0.2; opacity: 0.2;
line-height: 1.0; line-height: 1.0;
...@@ -303,21 +302,22 @@ ...@@ -303,21 +302,22 @@
cursor: pointer; cursor: pointer;
} }
.pdfViewer .canvasWrapper { .pdfViewer {
overflow: hidden; overflow: hidden;
width: 100%; align-self: unset;
height: 100%; align-content: center;
}
.pdfViewer .canvasWrapper {
overflow: hidden;
} }
.pdfViewer .page { .pdfViewer .page {
direction: ltr; direction: ltr;
width: 816px;
height: 1056px;
/* margin: 1px auto -8px auto; */ /* margin: 1px auto -8px auto; */
position: relative; position: relative;
overflow: visible; overflow: visible;
border: 9px solid transparent; /* border: 9px solid transparent; */
background-clip: content-box; background-clip: content-box;
-o-border-image: url(images/shadow.png) 9 9 repeat; -o-border-image: url(images/shadow.png) 9 9 repeat;
border-image: url(images/shadow.png) 9 9 repeat; border-image: url(images/shadow.png) 9 9 repeat;
......
...@@ -579,26 +579,44 @@ const initBook = async () => { ...@@ -579,26 +579,44 @@ const initBook = async () => {
const viewportWidth = window.innerWidth; const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight; const viewportHeight = window.innerHeight;
// 移动端使用不同的尺寸计算 // 计算容器能容纳的最大尺寸,保持PDF的1:√2比例
const PDF_RATIO = 1.414; // PDF标准比例 1:√2 (高度是宽度的1.414倍)
// 计算基于宽度的最大尺寸
const maxWidth = viewportWidth / 2 - 50;
const maxHeightByWidth = maxWidth * PDF_RATIO; // 高度 = 宽度 × 1.414
// 计算基于高度的最大尺寸
const maxHeight = viewportHeight - 100;
const maxWidthByHeight = maxHeight / PDF_RATIO; // 宽度 = 高度 ÷ 1.414
let pageWidth, pageHeight; let pageWidth, pageHeight;
if (isMobile.value) {
// 移动端:宽度等于设备宽度减去边距 // 选择能撑满一边的方案
pageWidth = viewportWidth - 20; // 左右各留10px边距 if (maxHeightByWidth <= maxHeight) {
// 高度按比例计算(假设标准比例为 4:3) // 宽度能撑满,高度也满足
pageHeight = (pageWidth * 4) / 3; pageWidth = maxWidth;
// 确保高度不超过视口高度 pageHeight = maxHeightByWidth;
if (pageHeight > viewportHeight - 100) { console.log(`选择宽度撑满方案: 宽度=${pageWidth}, 高度=${pageHeight}`);
// 上下各留50px边距
pageHeight = viewportHeight - 92;
// 重新计算宽度以保持比例
pageWidth = (pageHeight * 3) / 4;
}
} else { } else {
// 桌面端保持原有逻辑 // 高度能撑满,宽度也满足
pageWidth = Math.min(600, viewportWidth / 2 - 50); pageWidth = maxWidthByHeight;
pageHeight = Math.min(800, viewportHeight - 100); pageHeight = maxHeight;
console.log(`选择高度撑满方案: 宽度=${pageWidth}, 高度=${pageHeight}`);
} }
// 确保不超过移动端的限制
if (isMobile.value) {
pageWidth = Math.min(pageWidth, viewportWidth - 20);
pageHeight = pageWidth * PDF_RATIO; // 高度 = 宽度 × 1.414
console.log(`移动端调整后: 宽度=${pageWidth}, 高度=${pageHeight}`);
}
// 添加调试信息
console.log(`视口尺寸: ${viewportWidth}x${viewportHeight}`);
console.log(`计算出的页面尺寸: ${pageWidth}x${pageHeight}, 比例: ${(pageWidth/pageHeight).toFixed(3)}`);
console.log(`PDF标准比例: ${PDF_RATIO}, 实际比例: ${(pageWidth/pageHeight).toFixed(3)}`);
await nextTick(); await nextTick();
try { try {
...@@ -644,6 +662,7 @@ const initBook = async () => { ...@@ -644,6 +662,7 @@ const initBook = async () => {
}); });
// 移动端使用单页显示 // 移动端使用单页显示
console.log(`turn.js初始化参数: 宽度=${isMobile.value ? pageWidth : pageWidth * 2}, 高度=${pageHeight}`);
$magazine.turn({ $magazine.turn({
width: isMobile.value ? pageWidth : pageWidth * 2, width: isMobile.value ? pageWidth : pageWidth * 2,
height: pageHeight, height: pageHeight,
...@@ -859,15 +878,8 @@ const renderPdfPage = async (pageIndex) => { ...@@ -859,15 +878,8 @@ const renderPdfPage = async (pageIndex) => {
// 创建页面div元素 // 创建页面div元素
// todo 这里可能能够优化一下 // todo 这里可能能够优化一下
const pageDiv = document.createElement('div'); const pageDiv = document.createElement('div');
pageDiv.className = 'pdf-page-container'; pageDiv.className = 'pdfViewer';
pageDiv.setAttribute('data-page-number', pageNum); pageDiv.setAttribute('data-page-number', pageNum);
pageDiv.style.position = 'relative';
pageDiv.style.width = '100%';
pageDiv.style.height = '100%';
pageDiv.style.overflow = 'hidden';
pageDiv.style.display = 'flex';
pageDiv.style.alignItems = 'center';
pageDiv.style.justifyContent = 'center';
// 将页面div添加到容器中 // 将页面div添加到容器中
pageContainer.appendChild(pageDiv); pageContainer.appendChild(pageDiv);
...@@ -902,29 +914,25 @@ const renderPdfPage = async (pageIndex) => { ...@@ -902,29 +914,25 @@ const renderPdfPage = async (pageIndex) => {
console.log(`页面${pageNum}容器尺寸: ${containerWidth}x${containerHeight}`); console.log(`页面${pageNum}容器尺寸: ${containerWidth}x${containerHeight}`);
// 设置页面div的尺寸
// 这里可能能够优化
// pageDiv.style.width = `${containerWidth}px`;
// pageDiv.style.height = `${containerHeight}px`;
// 创建PDF页面视图 // 创建PDF页面视图
const rawPdfDocument = toRaw(pdfDocument.value); const rawPdfDocument = toRaw(pdfDocument.value);
const pdfPage = await rawPdfDocument.getPage(pageNum); const pdfPage = await rawPdfDocument.getPage(pageNum);
// 先计算缩放比例 // 先计算缩放比例
const originalViewport = pdfPage.getViewport({ scale: 1.0 }); const originalViewport = pdfPage.getViewport({ scale: 1.0 });
const scaleX = containerWidth / originalViewport.width; const viewportWidth = originalViewport.viewBox[2];
const scaleY = containerHeight / originalViewport.height; const viewportHeight = originalViewport.viewBox[3];
const scale = Math.min(scaleX, scaleY); const scaleX = containerWidth / viewportWidth;
const scaleY = containerHeight / viewportHeight;
const scale = Math.max(scaleX, scaleY);
// 创建基于缩放比例的视口 const cssUnits = pdfjsViewer.CSS_UNITS || (96.0 / 72.0);
const scaledViewport = pdfPage.getViewport({ scale: scale });
// 源码可以设置宽高,但是对文字层和page层还是不按照这个来。 // 计算补偿后的 scale
// scaledViewport.width = containerWidth; const compensatedScale = scale / cssUnits;
// scaledViewport.height = containerHeight;
console.log(`PDF原始尺寸: ${originalViewport.width}x${originalViewport.height}, 缩放比例: ${scale}, 缩放后尺寸: ${scaledViewport.width}x${scaledViewport.height}`); // 3. 重新创建 viewport 和 pageView
const newViewport = pdfPage.getViewport(scale);
// 创建PDFPageView实例 // 创建PDFPageView实例
if (!pdfjsViewer) { if (!pdfjsViewer) {
...@@ -933,8 +941,8 @@ const renderPdfPage = async (pageIndex) => { ...@@ -933,8 +941,8 @@ const renderPdfPage = async (pageIndex) => {
const pageView = new pdfjsViewer.PDFPageView({ const pageView = new pdfjsViewer.PDFPageView({
container: pageDiv, container: pageDiv,
id: pageNum - 1, id: pageNum - 1,
scale: 1.0, // 使用1.0,因为我们已经通过scaledViewport处理了缩放 scale: compensatedScale, // 使用1.0,因为我们已经通过scaledViewport处理了缩放
defaultViewport: scaledViewport, defaultViewport: newViewport,
eventBus: toRaw(eventBus.value), eventBus: toRaw(eventBus.value),
textLayerMode: 1, // 根据开关状态设置 textLayerMode: 1, // 根据开关状态设置
annotationMode: 0, // 1=ENABLE annotationMode: 0, // 1=ENABLE
...@@ -942,7 +950,8 @@ const renderPdfPage = async (pageIndex) => { ...@@ -942,7 +950,8 @@ const renderPdfPage = async (pageIndex) => {
renderer: "canvas", renderer: "canvas",
enableXfa: false, enableXfa: false,
l10n: null, l10n: null,
textLayerFactory: new pdfjsViewer.DefaultTextLayerFactory() textLayerFactory: new pdfjsViewer.DefaultTextLayerFactory(),
useOnlyCssZoom: false // 添加这一行
}); });
// 设置PDF页面 // 设置PDF页面
...@@ -975,17 +984,6 @@ const renderPdfPage = async (pageIndex) => { ...@@ -975,17 +984,6 @@ const renderPdfPage = async (pageIndex) => {
if (canvas) { if (canvas) {
console.log(`Canvas CSS尺寸: ${elementStyle.width}x${elementStyle.height}, 实际尺寸: ${canvas.width}x${canvas.height}`); console.log(`Canvas CSS尺寸: ${elementStyle.width}x${elementStyle.height}, 实际尺寸: ${canvas.width}x${canvas.height}`);
// 确保canvas尺寸正确
if (Math.abs(rect.width - containerWidth) > 5 || Math.abs(rect.height - containerHeight) > 5) {
console.warn(`页面${pageNum}尺寸不匹配,Canvas: ${rect.width}x${rect.height}, 容器: ${containerWidth}x${containerHeight}`);
// 尝试强制调整canvas尺寸
if (Math.abs(canvas.width - scaledViewport.width) > 5) {
console.log(`强制调整Canvas尺寸从 ${canvas.width}x${canvas.height}${scaledViewport.width}x${scaledViewport.height}`);
canvas.style.width = `${scaledViewport.width}px`;
canvas.style.height = `${scaledViewport.height}px`;
}
}
} }
} }
...@@ -1172,11 +1170,37 @@ const updateZoom = () => { ...@@ -1172,11 +1170,37 @@ const updateZoom = () => {
} }
} }
// 计算新的尺寸 // 计算容器能容纳的最大尺寸,保持PDF的1:√2比例
const viewportWidth = window.innerWidth; const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight; const viewportHeight = window.innerHeight;
const baseWidth = Math.min(600, viewportWidth / 2 - 50); const PDF_RATIO = 1.414; // PDF标准比例 1:√2 (高度是宽度的1.414倍)
const baseHeight = Math.min(800, viewportHeight - 100);
// 计算基于宽度的最大尺寸
const maxWidth = viewportWidth / 2 - 50;
const maxHeightByWidth = maxWidth * PDF_RATIO; // 高度 = 宽度 × 1.414
// 计算基于高度的最大尺寸
const maxHeight = viewportHeight - 100;
const maxWidthByHeight = maxHeight / PDF_RATIO; // 宽度 = 高度 ÷ 1.414
let baseWidth, baseHeight;
// 选择能撑满一边的方案
if (maxHeightByWidth <= maxHeight) {
// 宽度能撑满,高度也满足
baseWidth = maxWidth;
baseHeight = maxHeightByWidth;
} else {
// 高度能撑满,宽度也满足
baseWidth = maxWidthByHeight;
baseHeight = maxHeight;
}
// 确保不超过移动端的限制
if (isMobile.value) {
baseWidth = Math.min(baseWidth, viewportWidth - 20);
baseHeight = baseWidth * PDF_RATIO; // 高度 = 宽度 × 1.414
}
// 应用缩放 // 应用缩放
const scaledWidth = baseWidth * zoomLevel.value; const scaledWidth = baseWidth * zoomLevel.value;
...@@ -1256,11 +1280,40 @@ const handleResize = async () => { ...@@ -1256,11 +1280,40 @@ const handleResize = async () => {
} }
} }
// 计算新的尺寸 // 计算容器能容纳的最大尺寸,保持PDF的1:√2比例
const viewportWidth = window.innerWidth; const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight; const viewportHeight = window.innerHeight;
const baseWidth = Math.min(600, viewportWidth / 2 - 50); const PDF_RATIO = 1.414; // PDF标准比例 1:√2 (高度是宽度的1.414倍)
const baseHeight = Math.min(800, viewportHeight - 100);
// 计算基于宽度的最大尺寸
const maxWidth = viewportWidth / 2 - 50;
const maxHeightByWidth = maxWidth * PDF_RATIO; // 高度 = 宽度 × 1.414
// 计算基于高度的最大尺寸
const maxHeight = viewportHeight - 100;
const maxWidthByHeight = maxHeight / PDF_RATIO; // 宽度 = 高度 ÷ 1.414
let baseWidth, baseHeight;
// 选择能撑满一边的方案
if (maxHeightByWidth <= maxHeight) {
// 宽度能撑满,高度也满足
baseWidth = maxWidth;
baseHeight = maxHeightByWidth;
console.log(`Resize选择宽度撑满方案: 宽度=${baseWidth}, 高度=${baseHeight}`);
} else {
// 高度能撑满,宽度也满足
baseWidth = maxWidthByHeight;
baseHeight = maxHeight;
console.log(`Resize选择高度撑满方案: 宽度=${baseWidth}, 高度=${baseHeight}`);
}
// 确保不超过移动端的限制
if (isMobile.value) {
baseWidth = Math.min(baseWidth, viewportWidth - 20);
baseHeight = baseWidth * PDF_RATIO; // 高度 = 宽度 × 1.414
console.log(`Resize移动端调整后: 宽度=${baseWidth}, 高度=${baseHeight}`);
}
// 应用缩放 // 应用缩放
const scaledWidth = baseWidth * currentZoom; const scaledWidth = baseWidth * currentZoom;
...@@ -1287,6 +1340,12 @@ const handleResize = async () => { ...@@ -1287,6 +1340,12 @@ const handleResize = async () => {
} }
}); });
// 如果是PDF模式,重新调整PDF页面尺寸
if (props.isPdf && pdfInitialized.value) {
console.log("Resize时重新调整PDF页面尺寸");
await resizeVisiblePdfPages();
}
// 更新视口样式 // 更新视口样式
const $viewport = $(".magazine-viewport"); const $viewport = $(".magazine-viewport");
$viewport.css({ $viewport.css({
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论