Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
html-shot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
劳工
html-shot
Commits
2d2c4dc6
Commit
2d2c4dc6
authored
May 07, 2021
by
wjf
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dev' of gitlab2.dui88.com:laoqifeng/html-shot into dev
parents
a2bf6334
2a890870
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
89 additions
and
16 deletions
+89
-16
.yarn-integrity
node_modules/.yarn-integrity
+48
-0
package.json
package.json
+1
-1
readme.md
readme.md
+5
-0
dom-parser.ts
src/dom-parser.ts
+2
-1
toCanvas.ts
src/toCanvas.ts
+25
-12
test.html
test.html
+4
-2
yarn.lock
yarn.lock
+4
-0
No files found.
node_modules/.yarn-integrity
0 → 100644
View file @
2d2c4dc6
{
"systemParams": "darwin-x64-72",
"modulesFolders": [],
"flags": [],
"linkedModules": [
"@spark/animation",
"@spark/api-base",
"@spark/api-common",
"@spark/common-helpers",
"@spark/projectx",
"@spark/share",
"@spark/ui",
"@spark/utils",
"alias-parser",
"code-mix",
"deviljs",
"duiba-utils",
"fyge",
"html-shot",
"init-project",
"package-readme-generator",
"props-compute",
"psd-parse-web",
"qunity",
"qunity-cli",
"qunity-pixi",
"qunity-scene",
"scss-simple-parser",
"sheet-packer",
"spark-cli",
"spark-test-lib",
"spark-utils",
"spark-wrapper-fyge",
"test",
"tiny-image",
"tree-dsl-react",
"visual-code",
"visual-process",
"zeroing-code-divider",
"zeroing-pack",
"zeroing-template-fill",
"zrender"
],
"topLevelPatterns": [],
"lockfileEntries": {},
"files": [],
"artifacts": {}
}
\ No newline at end of file
package.json
View file @
2d2c4dc6
{
{
"name"
:
"html-shot"
,
"name"
:
"html-shot"
,
"version"
:
"1.0.1
3
"
,
"version"
:
"1.0.1
7
"
,
"main"
:
"dist/index.js"
,
"main"
:
"dist/index.js"
,
"types"
:
"types/index.d.ts"
,
"types"
:
"types/index.d.ts"
,
"license"
:
"
MIT
"
,
"license"
:
"
MIT
"
,
...
...
readme.md
View file @
2d2c4dc6
...
@@ -20,6 +20,11 @@ document.body.appendChild(img);
...
@@ -20,6 +20,11 @@ document.body.appendChild(img);
|
`type`
| 'canvas'
|
'jpeg'
|
'png' | 否 | 'png' | 导出类型 |
|
`type`
| 'canvas'
|
'jpeg'
|
'png' | 否 | 'png' | 导出类型 |
|
`qaulity`
| number | 否 | 0.7 | 导出图片质量 |
|
`qaulity`
| number | 否 | 0.7 | 导出图片质量 |
## 开启调试
```
js
window
[
'html-shot/debug-mode'
]
=
true
```
## 注意点
## 注意点
*
仅支持的元素有:img、text、canvas(svga/spine/lottie)
*
仅支持的元素有:img、text、canvas(svga/spine/lottie)
*
不支持transform变换
*
不支持transform变换
...
...
src/dom-parser.ts
View file @
2d2c4dc6
...
@@ -28,9 +28,10 @@ const includeStyleKeys = [
...
@@ -28,9 +28,10 @@ const includeStyleKeys = [
export
function
parseDom
(
el
:
HTMLElement
=
document
.
body
)
{
export
function
parseDom
(
el
:
HTMLElement
=
document
.
body
)
{
const
{
left
:
pX
,
top
:
pY
,
width
,
height
}
=
el
.
getBoundingClientRect
();
const
{
left
:
pX
,
top
:
pY
,
width
,
height
}
=
el
.
getBoundingClientRect
();
let
nodes
=
[]
,
skip
=
false
;
let
nodes
=
[];
walkNode
(
el
,
function
(
childNode
)
{
walkNode
(
el
,
function
(
childNode
)
{
let
vNode
,
bound
,
node
,
isText
;
let
vNode
,
bound
,
node
,
isText
;
let
skip
=
false
;
switch
(
childNode
.
nodeName
)
{
switch
(
childNode
.
nodeName
)
{
case
'IMG'
:
case
'IMG'
:
node
=
childNode
;
node
=
childNode
;
...
...
src/toCanvas.ts
View file @
2d2c4dc6
...
@@ -61,10 +61,10 @@ export interface RenderOptions {
...
@@ -61,10 +61,10 @@ export interface RenderOptions {
* @return Promise<HTMLCanvasElement | string>
* @return Promise<HTMLCanvasElement | string>
*/
*/
export
async
function
toCanvas
(
data
:
ICData
,
options
:
RenderOptions
=
{},
callback
?:
(
canvas
:
HTMLCanvasElement
)
=>
void
):
Promise
<
HTMLCanvasElement
|
string
>
{
export
async
function
toCanvas
(
data
:
ICData
,
options
:
RenderOptions
=
{},
callback
?:
(
canvas
:
HTMLCanvasElement
)
=>
void
):
Promise
<
HTMLCanvasElement
|
string
>
{
const
{
type
:
exportType
=
'png'
,
quality
=
0.7
}
=
options
;
const
{
type
:
exportType
=
'png'
,
quality
=
0.7
}
=
options
;
let
{
scale
=
window
[
'devicePixelRatio'
]
||
1
}
=
options
;
let
{
scale
=
window
[
'devicePixelRatio'
]
||
1
}
=
options
;
let
{
nodes
,
width
,
height
}
=
data
;
let
{
nodes
,
width
,
height
}
=
data
;
width
*=
scale
;
width
*=
scale
;
height
*=
scale
;
height
*=
scale
;
...
@@ -77,12 +77,15 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
...
@@ -77,12 +77,15 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
nodes
.
forEach
((
n
)
=>
{
nodes
.
forEach
((
n
)
=>
{
if
(
n
.
type
==
NodeType
.
IMAGE
)
{
//图片标签
if
(
n
.
type
==
NodeType
.
IMAGE
)
{
//图片标签
p
.
push
(
p
.
push
(
new
Promise
((
r
)
=>
{
new
Promise
((
r
esolve
,
reject
)
=>
{
let
img
=
new
Image
();
let
img
=
new
Image
();
img
.
crossOrigin
=
'anonymous'
img
.
crossOrigin
=
'anonymous'
img
.
onload
=
()
=>
{
img
.
onload
=
()
=>
{
n
.
img
=
img
;
n
.
img
=
img
;
r
()
resolve
()
}
img
.
onerror
=
()
=>
{
reject
(
`html shot error: can't fetch image[
${
img
.
src
}
]`
);
}
}
img
.
src
=
n
.
src
;
img
.
src
=
n
.
src
;
})
})
...
@@ -91,14 +94,22 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
...
@@ -91,14 +94,22 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
//背景图片标签
//背景图片标签
if
(
n
.
backgroundImage
)
{
if
(
n
.
backgroundImage
)
{
p
.
push
(
p
.
push
(
new
Promise
((
r
)
=>
{
new
Promise
((
r
esolve
,
reject
)
=>
{
let
img
=
new
Image
();
let
img
=
new
Image
();
img
.
crossOrigin
=
'anonymous'
img
.
crossOrigin
=
'anonymous'
img
.
onload
=
()
=>
{
img
.
onload
=
()
=>
{
n
.
imgBg
=
img
;
n
.
imgBg
=
img
;
r
()
resolve
()
}
img
.
onerror
=
()
=>
{
reject
(
`html shot error: can't fetch image[
${
img
.
src
}
]`
);
}
let
result
=
n
.
backgroundImage
.
match
(
/url
\((
.*
)
*
\)
/
);
if
(
result
)
{
img
.
src
=
result
[
1
].
replace
(
/
[
'"
]
/g
,
''
);
}
else
{
reject
(
`html shot error: can't parse
${
n
.
backgroundImage
}
`
)
}
}
img
.
src
=
n
.
backgroundImage
.
replace
(
'url("'
,
""
).
replace
(
'")'
,
""
);
})
})
)
)
}
}
...
@@ -143,7 +154,7 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
...
@@ -143,7 +154,7 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
* @param scale
* @param scale
*/
*/
function
drawImage
(
data
:
INodeData
,
ctx
:
CanvasRenderingContext2D
,
scale
:
number
)
{
function
drawImage
(
data
:
INodeData
,
ctx
:
CanvasRenderingContext2D
,
scale
:
number
)
{
let
{
x
,
y
,
img
,
width
,
height
,
borderRadius
:
borderRadiusTxt
}
=
data
;
let
{
x
,
y
,
img
,
width
,
height
,
borderRadius
:
borderRadiusTxt
}
=
data
;
x
*=
scale
;
x
*=
scale
;
y
*=
scale
;
y
*=
scale
;
width
*=
scale
;
width
*=
scale
;
...
@@ -259,11 +270,13 @@ function drawBackgroundColor(data: INodeData, ctx: CanvasRenderingContext2D, sca
...
@@ -259,11 +270,13 @@ function drawBackgroundColor(data: INodeData, ctx: CanvasRenderingContext2D, sca
}
}
function
drawBorder
(
data
:
INodeData
,
ctx
:
CanvasRenderingContext2D
,
scale
:
number
)
{
function
drawBorder
(
data
:
INodeData
,
ctx
:
CanvasRenderingContext2D
,
scale
:
number
)
{
let
{
x
,
y
,
width
,
height
,
let
{
x
,
y
,
width
,
height
,
borderRadius
:
borderRadiusTxt
,
borderRadius
:
borderRadiusTxt
,
borderWidth
:
borderWidthTxt
,
borderWidth
:
borderWidthTxt
,
borderColor
}
=
data
;
borderColor
}
=
data
;
let
borderRadius
=
parseFloat
(
borderRadiusTxt
)
*
scale
;
let
borderRadius
=
parseFloat
(
borderRadiusTxt
)
*
scale
;
let
borderWidth
=
parseFloat
(
borderWidthTxt
)
*
scale
;
let
borderWidth
=
parseFloat
(
borderWidthTxt
)
*
scale
;
x
*=
scale
;
x
*=
scale
;
...
...
test.html
View file @
2d2c4dc6
...
@@ -12,6 +12,8 @@
...
@@ -12,6 +12,8 @@
background-color
:
orange
;
background-color
:
orange
;
border-radius
:
2.00rem
;
border-radius
:
2.00rem
;
border
:
0.1rem
solid
blue
;
border
:
0.1rem
solid
blue
;
width
:
5.20rem
;
height
:
4.40rem
;
}
}
.bg
{
.bg
{
...
@@ -46,8 +48,8 @@
...
@@ -46,8 +48,8 @@
</head>
</head>
<body>
<body>
<div
id=
"poster"
class=
"poster"
>
<div
id=
"poster"
class=
"poster"
>
<
div
class=
"bg"
></div
>
<
!--<div class="bg"></div>--
>
<img
class=
"avatar"
src=
"//yun.duiba.com.cn/aurora/14e3d0fa0e1ff54553a2c8c094b1caffd90f0a43.png"
/>
<img
class=
"avatar"
src=
"//yun.duiba.com.cn/aurora/14e3d0fa0e1ff54553a2c8c094b1caffd90f0a43.png"
style=
"display: none"
/>
<!--<canvas id="canvas" style="position: absolute; left: 10px; top: 10px;"></canvas>
<!--<canvas id="canvas" style="position: absolute; left: 10px; top: 10px;"></canvas>
<p class="ppp">
<p class="ppp">
a<br/>bcdefghij
a<br/>bcdefghij
...
...
yarn.lock
0 → 100644
View file @
2d2c4dc6
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment