Skip to content

Commit ba21513

Browse files
author
whyiyhw
committed
优化图像渲染性能和质量,改进 HTML2Canvas 配置
主要变更: - 移除小红书页面特定的样式和渲染逻辑 - 调整 HTML2Canvas 配置,提高图像渲染质量 - 增加抗锯齿、图像平滑和文本渲染的高级选项 - 优化缩放比例和渲染参数 - 简化图像处理逻辑,提升通用性
1 parent b2b2596 commit ba21513

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

src/content/features/image-renderer.js

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -368,21 +368,6 @@ export function createImagePreviewModal(htmlData, service) {
368368
width: 750px !important;
369369
box-sizing: border-box !important;
370370
}
371-
.xiaohongshu-page {
372-
height: auto !important;
373-
min-height: 100% !important;
374-
overflow: visible !important;
375-
padding: 12px !important;
376-
width: 750px !important;
377-
box-sizing: border-box !important;
378-
transform: none !important;
379-
max-width: 100% !important;
380-
margin: 0 auto !important;
381-
background-color: white !important;
382-
position: relative !important;
383-
z-index: 1 !important;
384-
display: block !important;
385-
}
386371
.chat-container {
387372
overflow: visible !important;
388373
padding: 12px !important;
@@ -625,7 +610,7 @@ export function createImagePreviewModal(htmlData, service) {
625610
allowTaint: true,
626611
useCORS: true,
627612
logging: false,
628-
scale: isXiaohongshuPage ? 3 : 2, // 小红书页面使用更高清的3倍图
613+
scale: 2, // 提高缩放比例到3倍以获得更高清晰度
629614
backgroundColor: '#FFFFFF',
630615
windowWidth: 750,
631616
windowHeight: contentHeight,
@@ -634,39 +619,29 @@ export function createImagePreviewModal(htmlData, service) {
634619
scrollY: 0,
635620
scrollX: 0,
636621
removeContainer: true,
637-
// 尝试更好的文本渲染
622+
// 优化文本渲染
638623
letterRendering: true,
624+
textRendering: 'geometricPrecision', // 使用几何精度文本渲染
625+
fontDisplay: 'swap', // 字体加载策略
639626
useCORS: true,
640627
allowTaint: true,
641-
foreignObjectRendering: isXiaohongshuPage, // 对小红书页面尝试启用foreignObject渲染
642-
ignoreElements: (element) => {
643-
return element.classList &&
644-
(element.classList.contains('ignore-for-screenshot') ||
645-
element.classList.contains('scroll-indicator'));
646-
},
647-
// 针对小红书页面的特殊优化
648-
imageTimeout: 0, // 不设置图像加载超时
649-
onclone: (clonedDoc) => {
650-
if (isXiaohongshuPage) {
651-
// 确保克隆文档中的小红书页面样式正确
652-
const xiaohongshuPages = clonedDoc.querySelectorAll('.xiaohongshu-page');
653-
xiaohongshuPages.forEach(page => {
654-
page.style.width = '750px';
655-
page.style.margin = '0 auto';
656-
page.style.boxSizing = 'border-box';
657-
page.style.transform = 'none';
658-
page.style.maxWidth = '100%';
659-
page.style.height = 'auto';
660-
});
661-
}
662-
}
628+
foreignObjectRendering: true, // 启用foreignObject渲染以获得更好的文本质量
629+
imageTimeout: 0,
630+
// 抗锯齿和平滑设置
631+
imageSmoothing: true,
632+
imageSmoothingEnabled: true,
633+
imageSmoothingQuality: 'high',
634+
// Canvas上下文属性
635+
alpha: true, // 启用alpha通道
636+
antialias: true, // 启用抗锯齿
637+
willReadFrequently: true // 优化频繁读取
663638
};
664639

665640
html2canvas(iframeBody, options)
666641
.then(canvas => {
667642
// 小红书页面保持原始尺寸
668643
const finalCanvas = document.createElement('canvas');
669-
const originalHeight = canvas.height / options.scale;
644+
// const originalHeight = canvas.height / options.scale;
670645

671646
// 设置画布尺寸,保留完整内容
672647
finalCanvas.width = 750;
@@ -678,7 +653,7 @@ export function createImagePreviewModal(htmlData, service) {
678653
const ctx = finalCanvas.getContext('2d');
679654

680655
// 填充白色背景
681-
ctx.fillStyle = '#FFFFFF';
656+
// ctx.fillStyle = '#FFFFFF';
682657
ctx.fillRect(0, 0, finalCanvas.width, finalCanvas.height);
683658

684659
// 关闭图像平滑以获得更清晰的文字

0 commit comments

Comments
 (0)