基于表格滚动截屏(表格全部展开,没有滚动条)

javascript 复制代码
import html2canvasPro from 'html2canvas'
// 截图,平辅表格
async function resetAgSize() {
  const allColumns = gridApi.value.getColumns()
  let totalColumnWidth = 0
  let totalColumnHeight = 0
  // 遍历每一个行节点
  gridApi.value.forEachNode((rowNode) => {
    totalColumnHeight += rowNode.rowHeight
  })
  // debugger
  // 遍历每一个列节点
  allColumns.forEach((column) => {
    totalColumnWidth += column.getActualWidth()
  })
  // 处理让内容撑开  ,渲染完毕之后
  // 重新应用更新后的设置到ag-grid
  AgGridVueScreen.value = document.querySelector('.ag-grid-vue') as HTMLElement
  const initHeight = AgGridVueScreen.value.getBoundingClientRect().height
  AgGridVueScreen.value.style.width = totalColumnWidth + 10 + 'px'
  AgGridVueScreen.value.style.height = totalColumnHeight + 100 + 'px'
  AgGridScreen.value = document.querySelector('.ag-grid') as HTMLElement
  AgGridScreen.value.style.height = initHeight + 'px'
  AgGridScreen.value.style.overflow = 'scroll'
  await nextTick()
  await onScreenshot(AgGridVueScreen.value, AgGridScreen.value)
  //截图结束后记得恢复表格原本样式
  AgGridVueScreen.value.style.height = initHeight + 'px'
  AgGridVueScreen.value.style.width = '100%'
  AgGridScreen.value.removeAttribute('style')
}
javascript 复制代码
export default async function onScreenshot(dom, dom2) {
  console.log(dom2, dom2.scrollHeight)

  // 获取页面的总高度
  const pageHeight = dom2.scrollHeight
  // 获取当前视口的高度
  const viewportHeight = dom2.clientHeight
  // 获取页面的总高度
  const pageWidth = dom2.scrollWidth
  // 获取当前视口的高度
  const viewportWidth = dom2.clientWidth
  // 创建一个用于存储截图片段的数组
  // 创建用于存储截图片段的二维数组,第一维表示垂直方向的切片,第二维表示水平方向的切片
  const screenshots = []

  // 记录当前垂直滚动位置
  let currentScrollTop = 0

  while (currentScrollTop < pageHeight) {
    // 为当前垂直切片创建一个数组,用于存储水平方向的截图片段
    const horizontalScreenshots = []

    // 记录当前水平滚动位置
    let currentScrollLeft = 0

    while (currentScrollLeft < pageWidth) {
      // 使用html2canvas对当前可视区域进行截图
      const canvas = await html2canvasPro(dom, {
        // 设置截图区域为当前可视区域
        windowWidth: viewportWidth,
        windowHeight: viewportHeight,
        scrollX: currentScrollLeft,
        scrollY: currentScrollTop,
        useCORS: true,
        allowTaint: true
      })
      try {
        // 将截图转换为数据URL格式并添加到水平截图片段数组中
        horizontalScreenshots.push(canvas.toDataURL())
      } catch (e) {
        console.error('截图过程出现问题:', e)
      }
      // 滚动内容容器到下一个可视区域(水平方向)
      dom2.scrollTo(currentScrollLeft + viewportWidth, currentScrollTop)
      // 更新当前水平滚动位置
      currentScrollLeft += viewportWidth
    }
    // 将当前垂直切片的水平截图片段数组添加到总的截图片段二维数组中
    screenshots.push(horizontalScreenshots)
    // 滚动内容容器到下一个可视区域(垂直方向)
    dom2.scrollTo(0, currentScrollTop + viewportHeight)
    // 更新当前垂直滚动位置
    currentScrollTop += viewportHeight
  }
  // 获取完整截图的Data URL
  const finalScreenshot = screenshots[1] ? screenshots[0][screenshots[1].length - 1] : [screenshots.length - 1]

  // 创建一个下载链接并触发下载
  const downloadLink = document.createElement('a')
  downloadLink.href = finalScreenshot
  downloadLink.download = 'screenshot.png'
  document.body.appendChild(downloadLink)
  downloadLink.click()
  document.body.removeChild(downloadLink)
}

这种滚动截图比较耗费性能,如果表格过大没有必要使用这种方式。

尝试让后端调用页面截图,使用无头浏览器,节省性能。

相关推荐
Lvan的前端笔记3 分钟前
docker:每个前端项目一个 Nginx 容器还是只有一个Nginx容器
前端·nginx·docker
程序猿编码9 分钟前
榨干 RTX5090 算力!Qwen3 专用单卡推理引擎,手写C++/CUDA 算子实现 MTP 推测解码
开发语言·c++·大模型推理·qwen3·多模态推理
三天不学习31 分钟前
Egg.js 4 突然爆火,原因是否归结于AI 原生落地需求爆发?
前端·javascript·全栈·egg.js
xcs194051 小时前
前端 vue 的前端页面debugger 进不去
前端·javascript·vue.js
bkspiderx2 小时前
Qt 插件机制:动态扩展应用功能的核心框架
开发语言·qt·元数据·qt 插件·qpluginloader
程序员老陆2 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
沐晓时光2 小时前
C语言入门,深入理解指针(3)
c语言·开发语言
明月_清风2 小时前
Deno 终局来了:从挑战 Node 到被 Cloudflare 收编
前端·后端·node.js
码云数智-园园2 小时前
unique_ptr 还是 shared_ptr?C++ 智能指针选型与内存泄漏实战分析
java·开发语言
禾小西3 小时前
07丨Redis 哨兵机制:主库故障后,如何恢复服务?
java·开发语言·redis