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
Show 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
...
@@ -41,6 +41,7 @@ interface INodeData {
...
@@ -41,6 +41,7 @@ interface INodeData {
}
}
export
interface
RenderOptions
{
export
interface
RenderOptions
{
scale
?:
number
,
type
?:
'canvas'
|
'jpeg'
|
'png'
,
type
?:
'canvas'
|
'jpeg'
|
'png'
,
quality
?:
number
,
quality
?:
number
,
}
}
...
@@ -53,9 +54,13 @@ export interface RenderOptions {
...
@@ -53,9 +54,13 @@ 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
{
nodes
,
width
,
height
}
=
data
;
width
*=
scale
;
height
*=
scale
;
const
{
nodes
,
width
,
height
}
=
data
;
let
canvas
=
document
.
createElement
(
"canvas"
);
let
canvas
=
document
.
createElement
(
"canvas"
);
canvas
.
width
=
width
;
canvas
.
width
=
width
;
canvas
.
height
=
height
;
canvas
.
height
=
height
;
...
@@ -81,10 +86,10 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
...
@@ -81,10 +86,10 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
switch
(
n
.
type
)
{
switch
(
n
.
type
)
{
case
NodeType
.
IMAGE
:
case
NodeType
.
IMAGE
:
case
NodeType
.
CANVAS
:
case
NodeType
.
CANVAS
:
drawImage
(
n
,
ctx
)
drawImage
(
n
,
ctx
,
scale
)
break
break
case
NodeType
.
TEXT
:
case
NodeType
.
TEXT
:
drawText
(
n
,
ctx
)
drawText
(
n
,
ctx
,
scale
)
break
break
}
}
})
})
...
@@ -105,13 +110,20 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
...
@@ -105,13 +110,20 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
* 绘制图片
* 绘制图片
* @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
;
x
*=
scale
;
y
*=
scale
;
width
*=
scale
;
height
*=
scale
;
let
borderRadius
=
parseFloat
(
borderRadiusTxt
)
*
scale
;
if
(
borderRadius
)
{
//有圆角,画遮罩,暂时只管一个
if
(
borderRadius
)
{
//有圆角,画遮罩,暂时只管一个
ctx
.
save
();
ctx
.
save
();
let
max
=
(
width
<
height
?
width
:
height
)
/
2
;
let
max
=
(
width
<
height
?
width
:
height
)
/
2
;
let
radius
=
Math
.
min
(
+
borderRadius
.
replace
(
"px"
,
""
)
,
max
);
let
radius
=
Math
.
min
(
borderRadius
,
max
);
// ctx.beginPath();
// ctx.beginPath();
// ctx.moveTo(x, y + radius);
// ctx.moveTo(x, y + radius);
// ctx.quadraticCurveTo(x, y, x + radius, y);
// ctx.quadraticCurveTo(x, y, x + radius, y);
...
@@ -143,19 +155,27 @@ function drawImage(data: INodeData, ctx: CanvasRenderingContext2D) {
...
@@ -143,19 +155,27 @@ function drawImage(data: INodeData, ctx: CanvasRenderingContext2D) {
* 绘制文本,暂时没那么复杂,不用离屏玩
* 绘制文本,暂时没那么复杂,不用离屏玩
* @param data
* @param data
* @param ctx
* @param ctx
* @param scale
*/
*/
function
drawText
(
data
:
INodeData
,
ctx
:
CanvasRenderingContext2D
)
{
function
drawText
(
data
:
INodeData
,
ctx
:
CanvasRenderingContext2D
,
scale
:
number
)
{
cons
t
{
le
t
{
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
;
x
*=
scale
;
y
*=
scale
;
width
*=
scale
;
height
*=
scale
;
let
fontSize
=
parseFloat
(
fontSizeTxt
)
*
scale
;
let
font
=
fontSize
+
'px'
;
font
+=
" Arial"
;
//字体没有
font
+=
" Arial"
;
//字体没有
//font-weight:bold;font-style:italic;
//font-weight:bold;font-style:italic;
if
(
fontWeight
)
font
=
fontWeight
+
" "
+
font
;
if
(
fontWeight
)
font
=
fontWeight
+
" "
+
font
;
...
...
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