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
5b16ae67
Commit
5b16ae67
authored
Jan 21, 2021
by
rockyl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
作用dpr
parent
2588c85c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
184 additions
and
162 deletions
+184
-162
dom-parser.ts
src/dom-parser.ts
+3
-3
toCanvas.ts
src/toCanvas.ts
+177
-157
test.html
test.html
+3
-2
toCanvas.d.ts
types/toCanvas.d.ts
+1
-0
No files found.
src/dom-parser.ts
View file @
5b16ae67
...
@@ -60,15 +60,15 @@ export function parseDom(el: HTMLElement = document.body) {
...
@@ -60,15 +60,15 @@ export function parseDom(el: HTMLElement = document.body) {
break
;
break
;
}
}
if
(
!
bound
&&
childNode
.
getBoundingClientRect
)
{
if
(
!
bound
&&
childNode
.
getBoundingClientRect
)
{
bound
=
childNode
.
getBoundingClientRect
();
bound
=
childNode
.
getBoundingClientRect
();
}
}
let
styles
=
getStylesWithoutDefaults
(
node
||
childNode
);
let
styles
=
getStylesWithoutDefaults
(
node
||
childNode
);
if
(
!
isText
)
{
if
(
!
isText
)
{
for
(
let
skey
of
commonStyleKeys
)
{
for
(
let
skey
of
commonStyleKeys
)
{
if
(
commonStyleKeys
.
indexOf
(
skey
)
<
0
)
{
if
(
commonStyleKeys
.
indexOf
(
skey
)
<
0
)
{
continue
continue
}
}
...
...
src/toCanvas.ts
View file @
5b16ae67
...
@@ -2,47 +2,48 @@
...
@@ -2,47 +2,48 @@
* 转换接口数据
* 转换接口数据
*/
*/
interface
ICData
{
interface
ICData
{
width
:
number
,
width
:
number
,
height
:
number
,
height
:
number
,
nodes
:
INodeData
[]
nodes
:
INodeData
[]
}
}
/**
/**
* 节点类型
* 节点类型
*/
*/
enum
NodeType
{
enum
NodeType
{
ANY
=
0
,
ANY
=
0
,
TEXT
,
TEXT
,
IMAGE
,
IMAGE
,
CANVAS
,
CANVAS
,
}
}
/**
/**
* 节点数据
* 节点数据
*/
*/
interface
INodeData
{
interface
INodeData
{
type
:
NodeType
,
type
:
NodeType
,
x
:
number
,
x
:
number
,
y
:
number
,
y
:
number
,
width
:
number
,
width
:
number
,
height
:
number
,
height
:
number
,
//图片需要
//图片需要
src
?:
string
,
src
?:
string
,
borderRadius
?:
string
,
borderRadius
?:
string
,
img
?:
HTMLImageElement
,
//加载后挂上的
img
?:
HTMLImageElement
,
//加载后挂上的
//文本需要
//文本需要
text
?:
string
text
?:
string
color
?:
string
,
color
?:
string
,
fontSize
?:
string
,
//文本字号
fontSize
?:
string
,
//文本字号
fontWeight
?:
string
,
//文本粗细
fontWeight
?:
string
,
//文本粗细
wordWrap
?:
"break-word"
|
null
wordWrap
?:
"break-word"
|
null
textAlign
?:
"center"
|
"left"
|
"right"
textAlign
?:
"center"
|
"left"
|
"right"
fontStyle
?:
"normal"
|
"italic"
|
"oblique"
fontStyle
?:
"normal"
|
"italic"
|
"oblique"
}
}
export
interface
RenderOptions
{
export
interface
RenderOptions
{
type
?:
'canvas'
|
'jpeg'
|
'png'
,
scale
?:
number
,
quality
?:
number
,
type
?:
'canvas'
|
'jpeg'
|
'png'
,
quality
?:
number
,
}
}
/**
/**
...
@@ -53,153 +54,172 @@ export interface RenderOptions {
...
@@ -53,153 +54,172 @@ 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
;
const
{
nodes
,
width
,
height
}
=
data
;
let
{
nodes
,
width
,
height
}
=
data
;
let
canvas
=
document
.
createElement
(
"canvas"
);
width
*=
scale
;
canvas
.
width
=
width
;
height
*=
scale
;
canvas
.
height
=
height
;
let
ctx
=
canvas
.
getContext
(
"2d"
);
//先加载完所有图片
let
p
:
Promise
<
void
>
[]
=
[];
nodes
.
forEach
((
n
)
=>
{
if
(
n
.
type
!==
NodeType
.
IMAGE
)
return
;
p
.
push
(
new
Promise
((
r
)
=>
{
let
img
=
new
Image
();
img
.
crossOrigin
=
'anonymous'
img
.
onload
=
()
=>
{
n
.
img
=
img
;
r
()
}
img
.
src
=
n
.
src
;
})
)
})
if
(
p
.
length
)
await
Promise
.
all
(
p
);
nodes
.
forEach
((
n
)
=>
{
switch
(
n
.
type
)
{
case
NodeType
.
IMAGE
:
case
NodeType
.
CANVAS
:
drawImage
(
n
,
ctx
)
break
case
NodeType
.
TEXT
:
drawText
(
n
,
ctx
)
break
}
})
let
result
:
any
=
canvas
;
let
canvas
=
document
.
createElement
(
"canvas"
);
switch
(
exportType
)
{
canvas
.
width
=
width
;
case
'jpeg'
:
canvas
.
height
=
height
;
case
'png'
:
let
ctx
=
canvas
.
getContext
(
"2d"
);
result
=
canvas
.
toDataURL
(
'image/'
+
exportType
,
quality
);
//先加载完所有图片
break
;
let
p
:
Promise
<
void
>
[]
=
[];
}
nodes
.
forEach
((
n
)
=>
{
if
(
n
.
type
!==
NodeType
.
IMAGE
)
return
;
p
.
push
(
new
Promise
((
r
)
=>
{
let
img
=
new
Image
();
img
.
crossOrigin
=
'anonymous'
img
.
onload
=
()
=>
{
n
.
img
=
img
;
r
()
}
img
.
src
=
n
.
src
;
})
)
})
if
(
p
.
length
)
await
Promise
.
all
(
p
);
nodes
.
forEach
((
n
)
=>
{
switch
(
n
.
type
)
{
case
NodeType
.
IMAGE
:
case
NodeType
.
CANVAS
:
drawImage
(
n
,
ctx
,
scale
)
break
case
NodeType
.
TEXT
:
drawText
(
n
,
ctx
,
scale
)
break
}
})
callback
&&
callback
(
result
)
let
result
:
any
=
canvas
;
return
result
switch
(
exportType
)
{
case
'jpeg'
:
case
'png'
:
result
=
canvas
.
toDataURL
(
'image/'
+
exportType
,
quality
);
break
;
}
callback
&&
callback
(
result
)
return
result
}
}
/**
/**
* 绘制图片
* 绘制图片
* @param data
* @param data
* @param ctx
* @param ctx
* @param scale
*/
*/
function
drawImage
(
data
:
INodeData
,
ctx
:
CanvasRenderingContext2D
)
{
function
drawImage
(
data
:
INodeData
,
ctx
:
CanvasRenderingContext2D
,
scale
:
number
)
{
const
{
x
,
y
,
img
,
width
,
height
,
borderRadius
}
=
data
;
let
{
x
,
y
,
img
,
width
,
height
,
borderRadius
:
borderRadiusTxt
}
=
data
;
if
(
borderRadius
)
{
//有圆角,画遮罩,暂时只管一个
x
*=
scale
;
ctx
.
save
();
y
*=
scale
;
let
max
=
(
width
<
height
?
width
:
height
)
/
2
;
width
*=
scale
;
let
radius
=
Math
.
min
(
+
borderRadius
.
replace
(
"px"
,
""
),
max
);
height
*=
scale
;
// ctx.beginPath();
let
borderRadius
=
parseFloat
(
borderRadiusTxt
)
*
scale
;
// ctx.moveTo(x, y + radius);
// ctx.quadraticCurveTo(x, y, x + radius, y);
if
(
borderRadius
)
{
//有圆角,画遮罩,暂时只管一个
// ctx.lineTo(x + width - radius, y);
ctx
.
save
();
// ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
let
max
=
(
width
<
height
?
width
:
height
)
/
2
;
// ctx.lineTo(x + width, y + height - radius);
let
radius
=
Math
.
min
(
borderRadius
,
max
);
// ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
// ctx.beginPath();
// ctx.lineTo(x + radius, y + height);
// ctx.moveTo(x, y + radius);
// ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
// ctx.quadraticCurveTo(x, y, x + radius, y);
// ctx.lineTo(x, y + radius);
// ctx.lineTo(x + width - radius, y);
// ctx.clip();
// ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx
.
beginPath
();
// ctx.lineTo(x + width, y + height - radius);
ctx
.
moveTo
(
x
+
width
-
radius
,
y
);
// ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx
.
arcTo
(
x
+
width
,
y
,
x
+
width
,
y
+
radius
,
radius
)
// ctx.lineTo(x + radius, y + height);
ctx
.
lineTo
(
x
+
width
,
y
+
height
-
radius
)
// ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx
.
arcTo
(
x
+
width
,
y
+
height
,
x
+
width
-
radius
,
y
+
height
,
radius
)
// ctx.lineTo(x, y + radius);
ctx
.
lineTo
(
x
+
radius
,
y
+
height
)
// ctx.clip();
ctx
.
arcTo
(
x
,
y
+
height
,
x
,
y
+
height
-
radius
,
radius
)
ctx
.
beginPath
();
ctx
.
lineTo
(
x
,
y
+
radius
)
ctx
.
moveTo
(
x
+
width
-
radius
,
y
);
ctx
.
arcTo
(
x
,
y
,
x
+
radius
,
y
,
radius
)
ctx
.
arcTo
(
x
+
width
,
y
,
x
+
width
,
y
+
radius
,
radius
)
ctx
.
closePath
();
ctx
.
lineTo
(
x
+
width
,
y
+
height
-
radius
)
ctx
.
clip
();
ctx
.
arcTo
(
x
+
width
,
y
+
height
,
x
+
width
-
radius
,
y
+
height
,
radius
)
}
ctx
.
lineTo
(
x
+
radius
,
y
+
height
)
ctx
.
drawImage
(
img
,
x
,
y
,
width
,
height
)
ctx
.
arcTo
(
x
,
y
+
height
,
x
,
y
+
height
-
radius
,
radius
)
if
(
borderRadius
)
ctx
.
restore
();
ctx
.
lineTo
(
x
,
y
+
radius
)
ctx
.
arcTo
(
x
,
y
,
x
+
radius
,
y
,
radius
)
ctx
.
closePath
();
ctx
.
clip
();
}
ctx
.
drawImage
(
img
,
x
,
y
,
width
,
height
)
if
(
borderRadius
)
ctx
.
restore
();
}
}
/**
/**
* 绘制文本,暂时没那么复杂,不用离屏玩
* 绘制文本,暂时没那么复杂,不用离屏玩
* @param data
* @param data
* @param ctx
* @param ctx
* @param scale
*/
*/
function
drawText
(
data
:
INodeData
,
ctx
:
CanvasRenderingContext2D
)
{
function
drawText
(
data
:
INodeData
,
ctx
:
CanvasRenderingContext2D
,
scale
:
number
)
{
const
{
let
{
x
,
y
,
width
,
height
,
x
,
y
,
width
,
height
,
text
,
text
,
color
,
color
,
fontSize
,
fontSize
:
fontSizeTxt
,
fontWeight
,
fontWeight
,
fontStyle
,
fontStyle
,
wordWrap
,
wordWrap
,
textAlign
textAlign
}
=
data
;
}
=
data
;
let
font
=
fontSize
;
font
+=
" Arial"
;
//字体没有
x
*=
scale
;
//font-weight:bold;font-style:italic;
y
*=
scale
;
if
(
fontWeight
)
font
=
fontWeight
+
" "
+
font
;
width
*=
scale
;
if
(
fontStyle
)
font
=
fontStyle
+
" "
+
font
;
height
*=
scale
;
let
fontSize
=
parseFloat
(
fontSizeTxt
)
*
scale
;
let
font
=
fontSize
+
'px'
;
font
+=
" Arial"
;
//字体没有
//font-weight:bold;font-style:italic;
if
(
fontWeight
)
font
=
fontWeight
+
" "
+
font
;
if
(
fontStyle
)
font
=
fontStyle
+
" "
+
font
;
ctx
.
font
=
font
;
ctx
.
font
=
font
;
ctx
.
textBaseline
=
"top"
;
ctx
.
textBaseline
=
"top"
;
ctx
.
fillStyle
=
color
;
ctx
.
fillStyle
=
color
;
let
widthAll
=
ctx
.
measureText
(
text
).
width
;
let
widthAll
=
ctx
.
measureText
(
text
).
width
;
// console.log(ctx.measureText(text))
// console.log(ctx.measureText(text))
//超过宽度需要换行,且需要用到居中方式
//超过宽度需要换行,且需要用到居中方式
if
(
wordWrap
==
"break-word"
&&
widthAll
>
width
)
{
if
(
wordWrap
==
"break-word"
&&
widthAll
>
width
)
{
let
realLines
=
[];
let
realLines
=
[];
let
w
=
ctx
.
measureText
(
text
[
0
]).
width
;
let
w
=
ctx
.
measureText
(
text
[
0
]).
width
;
let
lineStr
=
text
[
0
];
let
lineStr
=
text
[
0
];
let
wordW
=
0
;
let
wordW
=
0
;
let
strLen
=
text
.
length
;
let
strLen
=
text
.
length
;
for
(
let
j
=
1
;
j
<
strLen
;
j
++
)
{
for
(
let
j
=
1
;
j
<
strLen
;
j
++
)
{
wordW
=
ctx
.
measureText
(
text
[
j
]).
width
;
wordW
=
ctx
.
measureText
(
text
[
j
]).
width
;
w
+=
wordW
;
w
+=
wordW
;
if
(
w
>
width
)
{
if
(
w
>
width
)
{
realLines
[
realLines
.
length
]
=
lineStr
;
realLines
[
realLines
.
length
]
=
lineStr
;
lineStr
=
text
[
j
];
lineStr
=
text
[
j
];
w
=
wordW
;
w
=
wordW
;
}
else
{
}
else
{
lineStr
+=
text
[
j
];
lineStr
+=
text
[
j
];
}
}
}
}
//最后一行
//最后一行
realLines
[
realLines
.
length
]
=
lineStr
;
realLines
[
realLines
.
length
]
=
lineStr
;
ctx
.
textAlign
=
textAlign
||
"left"
;
ctx
.
textAlign
=
textAlign
||
"left"
;
let
tx
=
0
;
let
tx
=
0
;
if
(
ctx
.
textAlign
==
"center"
)
{
if
(
ctx
.
textAlign
==
"center"
)
{
tx
=
width
*
0.5
;
tx
=
width
*
0.5
;
}
else
if
(
ctx
.
textAlign
==
"right"
)
{
}
else
if
(
ctx
.
textAlign
==
"right"
)
{
tx
=
width
;
tx
=
width
;
}
}
//有待考虑.现在直接拿高度取平均算每行高度
//有待考虑.现在直接拿高度取平均算每行高度
let
lineH
=
height
/
realLines
.
length
;
let
lineH
=
height
/
realLines
.
length
;
realLines
.
forEach
((
r
,
i
)
=>
{
realLines
.
forEach
((
r
,
i
)
=>
{
ctx
.
fillText
(
r
,
x
+
tx
,
y
+
i
*
lineH
)
ctx
.
fillText
(
r
,
x
+
tx
,
y
+
i
*
lineH
)
})
})
}
else
{
}
else
{
ctx
.
textAlign
=
"left"
ctx
.
textAlign
=
"left"
ctx
.
fillText
(
text
,
x
,
y
)
ctx
.
fillText
(
text
,
x
,
y
)
}
}
}
}
test.html
View file @
5b16ae67
...
@@ -40,10 +40,11 @@
...
@@ -40,10 +40,11 @@
<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/>
bcde
<span
style=
"background-color: rgba(0,255,0,0.5);"
>
span
</span>
fghij
a
<br/>
bcdefghij
</p>
</p>
</div>
</div>
<!--
<span style="background-color: rgba(0,255,0,0.5);">span</span>-->
<script>
<script>
let
canvas
=
document
.
getElementById
(
'canvas'
);
let
canvas
=
document
.
getElementById
(
'canvas'
);
let
context
=
canvas
.
getContext
(
'2d'
);
let
context
=
canvas
.
getContext
(
'2d'
);
...
...
types/toCanvas.d.ts
View file @
5b16ae67
...
@@ -36,6 +36,7 @@ interface INodeData {
...
@@ -36,6 +36,7 @@ interface INodeData {
fontStyle
?:
"normal"
|
"italic"
|
"oblique"
;
fontStyle
?:
"normal"
|
"italic"
|
"oblique"
;
}
}
export
interface
RenderOptions
{
export
interface
RenderOptions
{
scale
?:
number
;
type
?:
'canvas'
|
'jpeg'
|
'png'
;
type
?:
'canvas'
|
'jpeg'
|
'png'
;
quality
?:
number
;
quality
?:
number
;
}
}
...
...
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