hookehuyr

fix 保存图片时,按照旋转角度切图

...@@ -11,12 +11,12 @@ import dayjs from "dayjs"; ...@@ -11,12 +11,12 @@ import dayjs from "dayjs";
11 11
12 const tileSize = 512; 12 const tileSize = 512;
13 13
14 -export function TileCutter(imageURL, bounds, zoomLevel) { 14 +export function TileCutter(imageURL, bounds, zoomLevel, imageRotation = 0) {
15 const img = new Image(); 15 const img = new Image();
16 img.crossOrigin = "Anonymous"; // 避免跨域问题 16 img.crossOrigin = "Anonymous"; // 避免跨域问题
17 img.src = imageURL; 17 img.src = imageURL;
18 img.onload = () => { 18 img.onload = () => {
19 - sliceImageToTiles(img, bounds, zoomLevel); 19 + sliceImageToTiles(img, bounds, zoomLevel, imageRotation);
20 }; 20 };
21 } 21 }
22 22
...@@ -27,7 +27,7 @@ export function TileCutter(imageURL, bounds, zoomLevel) { ...@@ -27,7 +27,7 @@ export function TileCutter(imageURL, bounds, zoomLevel) {
27 * @param {L.LatLngBounds} bounds - 图片边界范围,包含西南角和东北角的经纬度 27 * @param {L.LatLngBounds} bounds - 图片边界范围,包含西南角和东北角的经纬度
28 * @param {number} zoomLevel - 地图缩放级别 28 * @param {number} zoomLevel - 地图缩放级别
29 */ 29 */
30 -function sliceImageToTiles(image, bounds, zoomLevel) { 30 +function sliceImageToTiles(image, bounds, zoomLevel, imageRotation) {
31 // 创建画布及上下文 31 // 创建画布及上下文
32 const canvas = document.createElement("canvas"); 32 const canvas = document.createElement("canvas");
33 const ctx = canvas.getContext("2d"); 33 const ctx = canvas.getContext("2d");
...@@ -89,6 +89,11 @@ function sliceImageToTiles(image, bounds, zoomLevel) { ...@@ -89,6 +89,11 @@ function sliceImageToTiles(image, bounds, zoomLevel) {
89 // 清空画布 89 // 清空画布
90 ctx.clearRect(0, 0, tileSize * scaleFactor, tileSize * scaleFactor); 90 ctx.clearRect(0, 0, tileSize * scaleFactor, tileSize * scaleFactor);
91 91
92 + // 旋转画布
93 + ctx.translate(tileSize / 2, tileSize / 2); // 移动原点到画布中心
94 + ctx.rotate(imageRotation * Math.PI / 180); // 旋转画布
95 + ctx.translate(-tileSize / 2, -tileSize / 2); // 移回原点
96 +
92 // 绘制图片到画布 97 // 绘制图片到画布
93 ctx.drawImage( 98 ctx.drawImage(
94 image, 99 image,
...@@ -96,6 +101,9 @@ function sliceImageToTiles(image, bounds, zoomLevel) { ...@@ -96,6 +101,9 @@ function sliceImageToTiles(image, bounds, zoomLevel) {
96 tileImgX, tileImgY, tileImgWidth, tileImgHeight // 目标画布区域(保持实际位置和比例) 101 tileImgX, tileImgY, tileImgWidth, tileImgHeight // 目标画布区域(保持实际位置和比例)
97 ); 102 );
98 103
104 + // 恢复画布旋转
105 + ctx.setTransform(1, 0, 0, 1, 0, 0);
106 +
99 // 将画布内容转换为Blob对象 107 // 将画布内容转换为Blob对象
100 canvas.toBlob((blob) => { 108 canvas.toBlob((blob) => {
101 if (!blob) { 109 if (!blob) {
......
1 <!-- 1 <!--
2 * @Date: 2025-01-22 11:40:12 2 * @Date: 2025-01-22 11:40:12
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-03-04 16:07:51 4 + * @LastEditTime: 2025-03-04 17:53:13
5 * @FilePath: /map-demo/src/views/mapCutter.vue 5 * @FilePath: /map-demo/src/views/mapCutter.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -361,7 +361,7 @@ function cutTiles() { ...@@ -361,7 +361,7 @@ function cutTiles() {
361 return; 361 return;
362 } 362 }
363 // TAG: 切割瓦片等级 363 // TAG: 切割瓦片等级
364 - TileCutter(imageURL.value, bounds.value, map_zoom.value); // 传入图片、地图范围、缩放级别 364 + TileCutter(imageURL.value, bounds.value, map_zoom.value, imageRotation.value); // 传入图片、地图范围、缩放级别和旋转角度
365 } 365 }
366 366
367 // ✅ 1. 位置移动 367 // ✅ 1. 位置移动
......