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
bedcef9e
Commit
bedcef9e
authored
Jan 20, 2021
by
rockyl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持解析背景和边框
parent
bbc45333
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
29 deletions
+61
-29
dom-parser.ts
src/dom-parser.ts
+50
-24
toCanvas.ts
src/toCanvas.ts
+2
-1
test.html
test.html
+5
-1
toCanvas.d.ts
types/toCanvas.d.ts
+4
-3
No files found.
src/dom-parser.ts
View file @
bedcef9e
...
@@ -2,6 +2,13 @@
...
@@ -2,6 +2,13 @@
* Created by rockyl on 2021/1/11.
* Created by rockyl on 2021/1/11.
*/
*/
const
commonStyleKeys
=
[
'borderRadius'
,
'borderColor'
,
'borderStyle'
,
'borderWidth'
,
]
const
styleKeys
=
[
const
styleKeys
=
[
'fontSize'
,
'fontSize'
,
'fontWeight'
,
'fontWeight'
,
...
@@ -17,51 +24,70 @@ export function parseDom(el: HTMLElement = document.body) {
...
@@ -17,51 +24,70 @@ export function parseDom(el: HTMLElement = document.body) {
let
nodes
=
[];
let
nodes
=
[];
walkNode
(
el
,
function
(
childNode
)
{
walkNode
(
el
,
function
(
childNode
)
{
let
vNode
,
bound
,
node
;
let
vNode
,
bound
,
node
,
isText
;
switch
(
childNode
.
nodeName
)
{
switch
(
childNode
.
nodeName
)
{
case
'IMG'
:
case
'IMG'
:
node
=
childNode
;
node
=
childNode
;
vNode
=
{
vNode
=
{
type
:
1
,
type
:
2
,
src
:
childNode
.
src
,
src
:
childNode
.
src
,
}
}
bound
=
childNode
.
getBoundingClientRect
();
break
;
break
;
case
'CANVAS'
:
case
'CANVAS'
:
node
=
childNode
;
node
=
childNode
;
vNode
=
{
vNode
=
{
type
:
2
,
type
:
3
,
img
:
childNode
,
img
:
childNode
,
}
}
bound
=
childNode
.
getBoundingClientRect
();
break
;
break
;
default
:
case
'#text'
:
if
(
childNode
.
data
)
{
isText
=
true
;
node
=
childNode
.
parentElement
;
node
=
childNode
.
parentElement
;
let
text
=
childNode
.
data
.
trim
();
let
text
=
childNode
.
data
.
trim
();
if
(
text
)
{
//过滤空文本
if
(
text
)
{
//过滤空文本
let
range
=
document
.
createRange
();
let
range
=
document
.
createRange
();
range
.
selectNode
(
childNode
);
range
.
selectNode
(
childNode
);
bound
=
range
.
getBoundingClientRect
();
bound
=
range
.
getBoundingClientRect
();
range
.
detach
();
range
.
detach
();
vNode
=
{
vNode
=
{
type
:
0
,
type
:
1
,
text
,
text
,
}
}
}
}
}
break
;
break
;
default
:
break
;
}
if
(
!
bound
&&
childNode
.
getBoundingClientRect
){
bound
=
childNode
.
getBoundingClientRect
();
}
}
if
(
vNode
)
{
let
styles
=
getStylesWithoutDefaults
(
node
||
childNode
);
if
(
!
isText
)
{
for
(
let
skey
of
commonStyleKeys
){
if
(
commonStyleKeys
.
indexOf
(
skey
)
<
0
){
continue
}
if
(
!
vNode
)
{
vNode
=
{
type
:
0
,
}
}
vNode
[
skey
]
=
styles
[
skey
];
}
}
if
(
vNode
&&
bound
)
{
const
{
left
,
top
,
width
,
height
}
=
bound
;
const
{
left
,
top
,
width
,
height
}
=
bound
;
vNode
.
x
=
left
-
pX
;
vNode
.
x
=
left
-
pX
;
vNode
.
y
=
top
-
pY
;
vNode
.
y
=
top
-
pY
;
vNode
.
width
=
width
;
vNode
.
width
=
width
;
vNode
.
height
=
height
;
vNode
.
height
=
height
;
let
styles
=
getStylesWithoutDefaults
(
node
);
for
(
let
skey
in
styles
)
{
for
(
let
skey
in
styles
)
{
if
(
styleKeys
.
indexOf
(
skey
)
<
0
)
{
if
(
styleKeys
.
indexOf
(
skey
)
<
0
)
{
continue
continue
...
@@ -72,7 +98,7 @@ export function parseDom(el: HTMLElement = document.body) {
...
@@ -72,7 +98,7 @@ export function parseDom(el: HTMLElement = document.body) {
nodes
.
push
(
vNode
);
nodes
.
push
(
vNode
);
}
}
});
});
console
.
log
(
nodes
);
return
{
return
{
width
,
width
,
height
,
height
,
...
@@ -89,7 +115,7 @@ function walkNode(root, callback) {
...
@@ -89,7 +115,7 @@ function walkNode(root, callback) {
}
}
}
}
function
getStylesWithoutDefaults
(
element
)
{
function
getStylesWithoutDefaults
(
element
)
:
any
{
let
dummy
=
document
.
createElement
(
'element-'
+
(
new
Date
().
getTime
()));
let
dummy
=
document
.
createElement
(
'element-'
+
(
new
Date
().
getTime
()));
document
.
body
.
appendChild
(
dummy
);
document
.
body
.
appendChild
(
dummy
);
...
...
src/toCanvas.ts
View file @
bedcef9e
...
@@ -11,7 +11,8 @@ interface ICData {
...
@@ -11,7 +11,8 @@ interface ICData {
* 节点类型
* 节点类型
*/
*/
enum
NodeType
{
enum
NodeType
{
TEXT
=
0
,
ANY
=
0
,
TEXT
,
IMAGE
,
IMAGE
,
CANVAS
,
CANVAS
,
}
}
...
...
test.html
View file @
bedcef9e
...
@@ -9,6 +9,9 @@
...
@@ -9,6 +9,9 @@
<style>
<style>
.poster
{
.poster
{
position
:
relative
;
position
:
relative
;
background-color
:
orange
;
border-radius
:
2.00rem
;
border
:
0.1rem
solid
blue
;
}
}
.avatar
{
.avatar
{
border-radius
:
1.00rem
;
border-radius
:
1.00rem
;
...
@@ -27,6 +30,7 @@
...
@@ -27,6 +30,7 @@
word-wrap
:
break-word
;
word-wrap
:
break-word
;
font-family
:
Arial
;
font-family
:
Arial
;
font-style
:
italic
;
font-style
:
italic
;
background-color
:
red
;
}
}
</style>
</style>
...
@@ -36,7 +40,7 @@
...
@@ -36,7 +40,7 @@
<img
class=
"avatar"
src=
"//yun.duiba.com.cn/aurora/14e3d0fa0e1ff54553a2c8c094b1caffd90f0a43.png"
/>
<img
class=
"avatar"
src=
"//yun.duiba.com.cn/aurora/14e3d0fa0e1ff54553a2c8c094b1caffd90f0a43.png"
/>
<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/>
bcde
<span
style=
"background-color: rgba(0,255,0,0.5);"
>
span
</span>
fghij
</p>
</p>
</div>
</div>
...
...
types/toCanvas.d.ts
View file @
bedcef9e
...
@@ -10,9 +10,10 @@ interface ICData {
...
@@ -10,9 +10,10 @@ interface ICData {
* 节点类型
* 节点类型
*/
*/
declare
enum
NodeType
{
declare
enum
NodeType
{
TEXT
=
0
,
ANY
=
0
,
IMAGE
=
1
,
TEXT
=
1
,
CANVAS
=
2
IMAGE
=
2
,
CANVAS
=
3
}
}
/**
/**
* 节点数据
* 节点数据
...
...
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