**css **
.container {
position: relative;
width: 100%;
height: 100%;
background: #000;
}
.img {
position: absolute;
top: 5%;
left: 50%;
transform: translateX(-50%);
overflow: hidden;
background: #eee;
}
.img image {
height:400px;
}
.imgcrop {
position: absolute;
left: -50000rpx;
top: -500000rpx;
}
.footer {
position: fixed;
width: 100%;
height: 110rpx;
color: red;
background: #000;
bottom: 1;
display: flex;
align-items: center;
justify-content: space-around;
margin-top: 130%;
}
.footer view {
width: 30%;
text-align: center;
}
.background {
width: 100%;
height: 100%;
position: absolute;
top: 0;
z-index: -1;
}
HTML
<view class="container">
<!-- 剪裁框与初始图片,剪裁框监听用户手势,获取移动缩放旋转值,images通过css样式显示变化 -->
<view class="img" style="width:{
{ width }}px; height:{
{height}}px" catchtouchstart="touchstartCallback" catchtouchmove="touchmoveCallback" catchtouchend="touchendCallback" >
<image style="transform: translate({
{stv.offsetX}}px, {
{stv.offsetY}}px) scale({
{stv.scale}}) rotate({
{ stv.rotate }}deg);width:{
{originImg.width}}px; height: {
{originImg.height}}px" src="{
{ originImg.url }}"></image>
</view>
<view class='footer'>
<view bindtap='uploadTap'>选择图片</view>
<view bindtap='rotate'>旋转</view>
<view bindtap='cropperImg'>打印</view>
</view>
<!-- canvas长宽设为初始图片设置的长款的两倍,使剪裁得到的图片更清晰,也不至于过大 -->
<canvas class='imgcrop' style="width:{
{canvasWidth}}px; height: {
{canvasHeight}}px" canvas-id='imgcrop'></canvas>
</view>
JS
注:77.png或者66.png更改成自己的图片URL,如果只需要一张图片去掉77.png或者66.png这段代码
const device = wx.getSystemInfoSync();
const app = getApp();
var mun = -1;
var initImgPaht='';
var widthRadio = '';
var heightRadio = '';
var twoPoint = {
x1: 0,
y1: 0,
x2: 0,
y2: 0
}
Component({
/**
* 组件的属性列表
*/
properties: {
ratio: {
type: Number,
observer: function (newVal, oldVal) {
this.setData({
width: device.windowWidth * 0.8,
height: device.windowWidth * 0.8 / newVal
})
}
},
url: {
type: String,
observer ( newVal, oldVal ) {
this.initImg( newVal )
}
}
},
/**
* 组件的初始数据
*/
data: {
width: device.windowWidth * 0.8, //剪裁框的宽度
height: device.windowWidth * 0.8 / (102 / 152), //剪裁框的长度
originImg: null, //存放原图信息
canvasWidth: device.windowWidth,
canvasHeight: device.windowWidth,
stv: {
offsetX: 0, //剪裁图片左上角坐标x
offsetY: 0, //剪裁图片左上角坐标y
zoom: false, //是否缩放状态
distance: 0, //两指距离
scale: 1, //缩放倍数
rotate: 0 //旋转角度
},
},
/**
* 组件的方法列表
*/
methods: {
uploadTap() {
let _this = this
wx.chooseImage({
count: 1, // 默认9
sizeType: ['original'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success(res) {
_this.initImg( res.tempFilePaths[0]);
}
})
},
rotate() {
let _this = this;
_this.setData({
'stv.rotate': _this.data.stv.rotate % 90 == 0 ? _this.data.stv.rotate = _this.data.stv.rotate + 90 : _this.data.stv.rotate = 0
})
if (_this.data.stv.rotate == 360) {
_this.data.stv.rotate = 0;
}
},
// canvas剪裁图片
cropperImg(