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
ffe5c8e1
Commit
ffe5c8e1
authored
Oct 08, 2021
by
wjf
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
8e701459
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
11 deletions
+17
-11
toCanvas.ts
src/toCanvas.ts
+17
-11
No files found.
src/toCanvas.ts
View file @
ffe5c8e1
...
...
@@ -46,8 +46,8 @@ interface INodeData {
'borderColor'
?,
'borderWidth'
?,
maskBegin
?:
boolean
,
maskEnd
?:
boolean
,
maskBegin
?:
boolean
,
maskEnd
?:
boolean
,
}
export
interface
RenderOptions
{
...
...
@@ -64,10 +64,10 @@ export interface RenderOptions {
* @return 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
let
{
scale
=
window
[
'devicePixelRatio'
]
||
1
}
=
options
const
{
type
:
exportType
=
'png'
,
quality
=
0.7
}
=
options
let
{
scale
=
window
[
'devicePixelRatio'
]
||
1
}
=
options
let
{
nodes
,
width
,
height
}
=
data
let
{
nodes
,
width
,
height
}
=
data
width
*=
scale
height
*=
scale
...
...
@@ -119,10 +119,10 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
})
if
(
p
.
length
)
await
Promise
.
all
(
p
)
nodes
.
forEach
((
n
)
=>
{
if
(
n
.
maskBegin
)
{
if
(
n
.
maskBegin
)
{
ctx
.
save
();
ctx
.
globalAlpha
=
0
;
drawBackgroundColor
(
n
,
ctx
,
scale
)
drawBackgroundColor
(
n
,
ctx
,
scale
)
ctx
.
globalAlpha
=
1
;
//可能不需要
ctx
.
clip
()
}
...
...
@@ -144,7 +144,7 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
break
}
if
(
n
.
maskEnd
)
{
if
(
n
.
maskEnd
)
{
ctx
.
restore
();
}
})
...
...
@@ -168,7 +168,7 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
* @param scale
*/
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
y
*=
scale
width
*=
scale
...
...
@@ -216,7 +216,7 @@ function drawText(data: INodeData, ctx: CanvasRenderingContext2D, scale: number)
ctx
.
font
=
font
ctx
.
textBaseline
=
"bottom"
ctx
.
fillStyle
=
color
||
"black"
ctx
.
fillStyle
=
color
||
"black"
var
textMetrics
=
ctx
.
measureText
(
text
)
let
widthAll
=
textMetrics
.
width
...
...
@@ -279,7 +279,13 @@ function drawBackgroundColor(data: INodeData, ctx: CanvasRenderingContext2D, sca
borderRadiusPath
(
data
,
ctx
,
scale
)
ctx
.
fill
()
}
else
{
ctx
.
fillRect
(
x
,
y
,
width
,
height
)
// ctx.fillRect(x, y, width, height)
ctx
.
moveTo
(
x
,
y
)
ctx
.
lineTo
(
x
+
width
,
y
)
ctx
.
lineTo
(
x
+
width
,
y
+
height
)
ctx
.
lineTo
(
x
,
y
+
height
)
ctx
.
lineTo
(
x
,
y
)
ctx
.
fill
()
}
}
...
...
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