Skip to content

Commit

Permalink
fix: try fix #53 with specific scale in html2canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Feb 27, 2023
1 parent f8e09bc commit fefda82
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/userscript/src/exporter/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import html2canvas from 'html2canvas'
import { downloadUrl, getFileNameWithFormat } from '../utils/download'
import { sleep } from '../utils/utils'

// https://github.com/niklasvh/html2canvas/issues/2792#issuecomment-1042948572
function fnIgnoreElements(el: any) {
return typeof el.shadowRoot === 'object' && el.shadowRoot !== null
}

export async function exportToPng(fileNameFormat: string) {
const thread = document.querySelector('main .group')?.parentElement as HTMLElement
if (!thread || thread.children.length === 0) return
Expand All @@ -12,10 +17,12 @@ export async function exportToPng(fileNameFormat: string) {
await sleep(100)

const canvas = await html2canvas(thread, {
scale: 1,
scrollX: -window.scrollX,
scrollY: -window.scrollY,
windowWidth: thread.scrollWidth,
windowHeight: thread.scrollHeight,
ignoreElements: fnIgnoreElements,
})

// restore the layout
Expand All @@ -25,4 +32,5 @@ export async function exportToPng(fileNameFormat: string) {
.replace(/^data:image\/[^;]/, 'data:application/octet-stream')
const fileName = getFileNameWithFormat(fileNameFormat, 'png')
downloadUrl(fileName, dataUrl)
window.URL.revokeObjectURL(dataUrl)
}

0 comments on commit fefda82

Please sign in to comment.