Showing
1 changed file
with
21 additions
and
14 deletions
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-01-22 11:45:30 | 2 | * @Date: 2025-01-22 11:45:30 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-02-27 09:41:24 | 4 | + * @LastEditTime: 2025-03-12 09:44:10 |
| 5 | * @FilePath: /map-demo/src/utils/TileCutter.js | 5 | * @FilePath: /map-demo/src/utils/TileCutter.js |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | */ | 7 | */ |
| ... | @@ -67,11 +67,9 @@ function sliceImageToTiles(image, bounds, zoomLevel, imageRotation) { | ... | @@ -67,11 +67,9 @@ function sliceImageToTiles(image, bounds, zoomLevel, imageRotation) { |
| 67 | // 设置画布缩放 | 67 | // 设置画布缩放 |
| 68 | ctx.scale(scaleFactor, scaleFactor); | 68 | ctx.scale(scaleFactor, scaleFactor); |
| 69 | 69 | ||
| 70 | - const zip = new JSZip(); // 创建一个 JSZip 实例,用来打包所有瓦片 | 70 | + const zip = new JSZip(); |
| 71 | + let tileIndex = 0; | ||
| 71 | 72 | ||
| 72 | - let tileIndex = 0; // 瓦片索引,用来给每个瓦片命名 | ||
| 73 | - | ||
| 74 | - // 计算每个瓦片的经纬度范围 | ||
| 75 | for (let tileX = tileStartX; tileX <= tileEndX; tileX++) { | 73 | for (let tileX = tileStartX; tileX <= tileEndX; tileX++) { |
| 76 | for (let tileY = tileStartY; tileY <= tileEndY; tileY++) { | 74 | for (let tileY = tileStartY; tileY <= tileEndY; tileY++) { |
| 77 | // 计算当前瓦片的经纬度范围 | 75 | // 计算当前瓦片的经纬度范围 |
| ... | @@ -89,20 +87,29 @@ function sliceImageToTiles(image, bounds, zoomLevel, imageRotation) { | ... | @@ -89,20 +87,29 @@ function sliceImageToTiles(image, bounds, zoomLevel, imageRotation) { |
| 89 | // 清空画布 | 87 | // 清空画布 |
| 90 | ctx.clearRect(0, 0, tileSize * scaleFactor, tileSize * scaleFactor); | 88 | ctx.clearRect(0, 0, tileSize * scaleFactor, tileSize * scaleFactor); |
| 91 | 89 | ||
| 92 | - // 旋转画布 | 90 | + // 保存当前画布状态 |
| 93 | - ctx.translate(tileSize / 2, tileSize / 2); // 移动原点到画布中心 | 91 | + ctx.save(); |
| 94 | - ctx.rotate(imageRotation * Math.PI / 180); // 旋转画布 | 92 | + |
| 95 | - ctx.translate(-tileSize / 2, -tileSize / 2); // 移回原点 | 93 | + // 计算图片在瓦片中的中心点 |
| 94 | + const imgCenterX = tileImgX + tileImgWidth / 2; | ||
| 95 | + const imgCenterY = tileImgY + tileImgHeight / 2; | ||
| 96 | + | ||
| 97 | + // 将原点移动到图片中心 | ||
| 98 | + ctx.translate(imgCenterX, imgCenterY); | ||
| 99 | + | ||
| 100 | + // 应用旋转 | ||
| 101 | + ctx.rotate(imageRotation * Math.PI / 180); | ||
| 96 | 102 | ||
| 97 | - // 绘制图片到画布 | 103 | + // 绘制图片,需要考虑旋转后的偏移 |
| 98 | ctx.drawImage( | 104 | ctx.drawImage( |
| 99 | image, | 105 | image, |
| 100 | - 0, 0, imgWidth, imgHeight, // 源图像区域(使用完整图片) | 106 | + 0, 0, imgWidth, imgHeight, |
| 101 | - tileImgX, tileImgY, tileImgWidth, tileImgHeight // 目标画布区域(保持实际位置和比例) | 107 | + -tileImgWidth / 2, -tileImgHeight / 2, |
| 108 | + tileImgWidth, tileImgHeight | ||
| 102 | ); | 109 | ); |
| 103 | 110 | ||
| 104 | - // 恢复画布旋转 | 111 | + // 恢复画布状态 |
| 105 | - ctx.setTransform(1, 0, 0, 1, 0, 0); | 112 | + ctx.restore(); |
| 106 | 113 | ||
| 107 | // 将画布内容转换为Blob对象 | 114 | // 将画布内容转换为Blob对象 |
| 108 | canvas.toBlob((blob) => { | 115 | canvas.toBlob((blob) => { | ... | ... |
-
Please register or login to post a comment