@antmjs/vantui
Version:
一套适用于Taro3及React的vantui组件库
37 lines • 1.24 kB
JavaScript
import { canvasToTempFilePath, hideLoading } from '@tarojs/taro';
export function rotateImage(params) {
return new Promise(function (resove) {
var imageHeight = params.imageHeight,
imageWidth = params.imageWidth,
rotate = params.rotate,
ctx = params.ctx,
imagePath = params.imagePath,
canvasId = params.canvasId;
var canvasWidth, canvasHeight;
if (rotate === 90 || rotate === 270) {
canvasWidth = imageHeight;
canvasHeight = imageWidth;
} else {
canvasWidth = imageWidth;
canvasHeight = imageHeight;
}
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
ctx.translate(canvasWidth / 2, canvasHeight / 2);
ctx.rotate(rotate * Math.PI / 180);
ctx.drawImage(imagePath, -imageWidth / 2, -imageHeight / 2, imageWidth, imageHeight);
ctx.translate(-canvasWidth / 2, -canvasHeight / 2);
ctx.rotate(-rotate * Math.PI / 180);
ctx.draw(false, function () {
canvasToTempFilePath({
canvasId: canvasId,
success: function success(res) {
resove(res.tempFilePath);
},
fail: function fail(err) {
hideLoading();
console.error('rotateImage fail', err);
}
});
});
});
}