-
Notifications
You must be signed in to change notification settings - Fork 565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add usePageCss option, add toCanvasList and toImage method for large html #473
Open
hzsrc
wants to merge
19
commits into
bubkoo:master
Choose a base branch
from
hzsrc:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d5c17e1
feat: add option to resolve problems which caused by html that has l…
f5afad4
fix: if ratio=2 and style.border='1px', in html it is actually render…
5b7cfe2
fix: use getPixelRatio()
8ea57e6
feat: add `toCanvasList` and `toImage` method to get rid of the limit…
56b099e
fix: img missing alt
fc79106
fix: url(...) in css file lose efficacy
2ea87c3
Support image in html or image/font in <style>
31a2400
innerText
93b5841
fix: html encode
4b37fe0
避免图像底部不完整的问题
a538140
fix: add `checkTail` to whether the svg tail is integrated.
7cba196
alt
c121f50
willReadFrequently
4d38bd6
add `toOfflineHtml` method.
3912870
willReadFrequently: true
d71ce44
await
da1953d
修复高度问题
705e6aa
lint
e43e2e6
Image尚未加载导致不显示
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,19 +129,62 @@ | |
canvas.height *= canvasDimensionLimit / canvas.width | ||
canvas.width = canvasDimensionLimit | ||
} else { | ||
canvas.width *= canvasDimensionLimit / canvas.height | ||
canvas.height = canvasDimensionLimit | ||
const height1 = getMaxCanvasHeight(canvas.width) | ||
canvas.width *= height1 / canvas.height | ||
canvas.height = height1 | ||
} | ||
} else if (canvas.width > canvasDimensionLimit) { | ||
canvas.height *= canvasDimensionLimit / canvas.width | ||
canvas.width = canvasDimensionLimit | ||
} else { | ||
canvas.width *= canvasDimensionLimit / canvas.height | ||
canvas.height = canvasDimensionLimit | ||
const height = getMaxCanvasHeight(canvas.width) | ||
canvas.width *= height / canvas.height | ||
canvas.height = height | ||
} | ||
} | ||
} | ||
|
||
export function getDimensionLimit(): number { | ||
return canvasDimensionLimit | ||
} | ||
|
||
const dimenstionLimitCache: { [width: number]: number } = {} | ||
|
||
export function getMaxCanvasHeight(width: number): number { | ||
let val = dimenstionLimitCache[width] | ||
if (val) return val | ||
val = test() | ||
dimenstionLimitCache[width] = val | ||
return val | ||
|
||
function test(): number { | ||
const heights = [ | ||
// Chrome 83 (Mac, Win) | ||
65535, | ||
// Chrome 70 (Mac, Win) | ||
// Chrome 68 (Android 4.4-9) | ||
// Firefox 63 (Mac, Win) | ||
32767, | ||
// Edge 17 (Win) | ||
// IE11 (Win) | ||
// 16384, | ||
] | ||
for (let i = 0; i < heights.length; i++) { | ||
try { | ||
const canvas = document.createElement('canvas') | ||
canvas.width = width | ||
canvas.height = heights[i] | ||
const ctx = canvas.getContext('2d')! | ||
ctx.drawImage(new Image(), 0, 0) // check | ||
return heights[i] | ||
} catch (e) { | ||
// ignore | ||
} | ||
} | ||
return canvasDimensionLimit | ||
} | ||
} | ||
|
||
export function canvasToBlob( | ||
canvas: HTMLCanvasElement, | ||
options: Options = {}, | ||
|
@@ -203,13 +246,16 @@ | |
node: HTMLElement, | ||
width: number, | ||
height: number, | ||
usePageCss?: boolean, | ||
): Promise<string> { | ||
const xmlns = 'http://www.w3.org/2000/svg' | ||
const svg = document.createElementNS(xmlns, 'svg') | ||
const foreignObject = document.createElementNS(xmlns, 'foreignObject') | ||
|
||
svg.setAttribute('width', `${width}`) | ||
svg.setAttribute('height', `${height}`) | ||
// fix: if ratio=2 and style.border='1px', in html it is actually rendered to 1px, but in <img src="svg"> it is rendered to 2px. Then height is different and the bottom 1px is lost, 10 nodes will lost 10px. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This image is missing a text alternative. This is a problem for people using screen readers. |
||
const ratio = getPixelRatio() | ||
svg.setAttribute('width', `${width / ratio}`) | ||
svg.setAttribute('height', `${height / ratio}`) | ||
svg.setAttribute('viewBox', `0 0 ${width} ${height}`) | ||
|
||
foreignObject.setAttribute('width', '100%') | ||
|
@@ -220,6 +266,12 @@ | |
|
||
svg.appendChild(foreignObject) | ||
foreignObject.appendChild(node) | ||
if (usePageCss) { | ||
const style = document.createElementNS(xmlns, 'style') | ||
style.innerHTML = await getStyles() | ||
svg.insertBefore(style, foreignObject) | ||
} | ||
|
||
return svgToDataURL(svg) | ||
} | ||
|
||
|
@@ -240,3 +292,25 @@ | |
isInstanceOfElement(nodePrototype, instance) | ||
) | ||
} | ||
|
||
export function getStyles() { | ||
const styles = document.querySelectorAll('style,link[rel="stylesheet"]') | ||
const promises: Array<Promise<string>> = [] | ||
toArray(styles).forEach((el) => { | ||
const e = el as Element | ||
if (e.tagName === 'LINK') { | ||
const href = e.getAttribute('href') | ||
if (href) | ||
promises.push( | ||
fetch(href) | ||
.then((r) => r.text()) | ||
.catch(() => ''), | ||
) | ||
} else { | ||
promises.push(Promise.resolve(e.innerHTML)) | ||
} | ||
}) | ||
return Promise.all(promises).then((arr) => { | ||
return arr.join('\n\n') | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This image is missing a text alternative. This is a problem for people using screen readers.