Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Z
zeroing-editor
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
劳工
zeroing-editor
Commits
c0992a69
Commit
c0992a69
authored
May 18, 2020
by
rockyl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化图集打包的尺寸
parent
05a560fa
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
5 deletions
+17
-5
sheet-pack.js
src/utils/sheet-pack.js
+17
-5
No files found.
src/utils/sheet-pack.js
View file @
c0992a69
...
...
@@ -10,6 +10,10 @@ import generateUUID from "uuid/v4";
const
packExts
=
[
'.png'
];
//, '.jpg', '.jpeg', '.bmp'
let
canvas
=
document
.
createElement
(
'canvas'
);
let
canvasFinal
=
document
.
createElement
(
'canvas'
);
let
context
=
canvas
.
getContext
(
'2d'
);
let
contextFinal
=
canvasFinal
.
getContext
(
'2d'
);
export
async
function
packImages
(
assets
,
options
=
{})
{
const
padding
=
options
.
padding
||
1
;
...
...
@@ -60,16 +64,24 @@ export async function packImages(assets, options = {}) {
let
pack
=
new
MaxRectsBinPack
(
maxSize
,
maxSize
,
false
);
let
packedRects
=
pack
.
insertRects
(
remainRects
,
mode
);
//document.body.append(canvas);
canvas
.
width
=
canvas
.
height
=
maxSize
;
let
context
=
canvas
.
getContext
(
'2d'
);
//context.fillStyle = `rgba(${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)}, 1)`;
//context.strokeRect(0, 0, maxSize, maxSize);
let
maxWidth
=
0
,
maxHeight
=
0
;
for
(
let
rect
of
packedRects
)
{
if
(
rect
.
x
+
rect
.
width
>
maxWidth
)
{
maxWidth
=
rect
.
x
+
rect
.
width
;
}
if
(
rect
.
y
+
rect
.
height
>
maxHeight
)
{
maxHeight
=
rect
.
y
+
rect
.
height
;
}
context
.
drawImage
(
rect
.
image
,
rect
.
x
+
padding
,
rect
.
y
+
padding
);
}
canvasFinal
.
width
=
maxWidth
;
canvasFinal
.
height
=
maxHeight
;
contextFinal
.
drawImage
(
canvas
,
0
,
0
);
let
blob
=
await
new
Promise
(
resolve
=>
{
canvas
.
toBlob
(
function
(
blob
)
{
canvas
Final
.
toBlob
(
function
(
blob
)
{
resolve
(
blob
);
},
'image/png'
);
});
...
...
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