Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Hooke
/
map-demo
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
hookehuyr
2025-03-12 10:01:43 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
d5e0db02f7b2ca65b494dca554f81693400f7f6a
d5e0db02
1 parent
eda8d104
fix 切图旋转功能优化
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
14 deletions
src/utils/TileCutter.js
src/utils/TileCutter.js
View file @
d5e0db0
/*
* @Date: 2025-01-22 11:45:30
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-0
2-27 09:41:24
* @LastEditTime: 2025-0
3-12 09:44:10
* @FilePath: /map-demo/src/utils/TileCutter.js
* @Description: 文件描述
*/
...
...
@@ -67,11 +67,9 @@ function sliceImageToTiles(image, bounds, zoomLevel, imageRotation) {
// 设置画布缩放
ctx
.
scale
(
scaleFactor
,
scaleFactor
);
const
zip
=
new
JSZip
();
// 创建一个 JSZip 实例,用来打包所有瓦片
const
zip
=
new
JSZip
();
let
tileIndex
=
0
;
let
tileIndex
=
0
;
// 瓦片索引,用来给每个瓦片命名
// 计算每个瓦片的经纬度范围
for
(
let
tileX
=
tileStartX
;
tileX
<=
tileEndX
;
tileX
++
)
{
for
(
let
tileY
=
tileStartY
;
tileY
<=
tileEndY
;
tileY
++
)
{
// 计算当前瓦片的经纬度范围
...
...
@@ -89,20 +87,29 @@ function sliceImageToTiles(image, bounds, zoomLevel, imageRotation) {
// 清空画布
ctx
.
clearRect
(
0
,
0
,
tileSize
*
scaleFactor
,
tileSize
*
scaleFactor
);
// 旋转画布
ctx
.
translate
(
tileSize
/
2
,
tileSize
/
2
);
// 移动原点到画布中心
ctx
.
rotate
(
imageRotation
*
Math
.
PI
/
180
);
// 旋转画布
ctx
.
translate
(
-
tileSize
/
2
,
-
tileSize
/
2
);
// 移回原点
// 保存当前画布状态
ctx
.
save
();
// 计算图片在瓦片中的中心点
const
imgCenterX
=
tileImgX
+
tileImgWidth
/
2
;
const
imgCenterY
=
tileImgY
+
tileImgHeight
/
2
;
// 将原点移动到图片中心
ctx
.
translate
(
imgCenterX
,
imgCenterY
);
// 应用旋转
ctx
.
rotate
(
imageRotation
*
Math
.
PI
/
180
);
// 绘制图片
到画布
// 绘制图片
,需要考虑旋转后的偏移
ctx
.
drawImage
(
image
,
0
,
0
,
imgWidth
,
imgHeight
,
// 源图像区域(使用完整图片)
tileImgX
,
tileImgY
,
tileImgWidth
,
tileImgHeight
// 目标画布区域(保持实际位置和比例)
0
,
0
,
imgWidth
,
imgHeight
,
-
tileImgWidth
/
2
,
-
tileImgHeight
/
2
,
tileImgWidth
,
tileImgHeight
);
// 恢复画布
旋转
ctx
.
setTransform
(
1
,
0
,
0
,
1
,
0
,
0
);
// 恢复画布
状态
ctx
.
restore
(
);
// 将画布内容转换为Blob对象
canvas
.
toBlob
((
blob
)
=>
{
...
...
Please
register
or
login
to post a comment