收藏
回答

寻找精通 js 的先生帮忙给看看,解决不了了?

由于画布需要缩小到30%,导致旋转角度出现问题,有高人给看看指点一下,多谢多谢。


<!--wxml -->
<view style="width:414px;height:500px;position: absolute;" bindtap="cancel">
    <view id="test" style="background-color:#1ea5c7;width: {{1080}}px;height: {{1080}}px;position: relative;transform: scale({{0.3}});transform-origin:center;left:50%;margin-left:-{{540}}px;top:50%;margin-top: -{{540}}px;" catchtap="">
        <view class='contentWarp'>
            <block wx:for="{{itemList}}" wx:key="index">
                <view class='touchWrap' style='transform: scale({{item.scale}});top:{{item.top}}px;left:{{item.left}}px; z-index:{{item.active?100:1}}'>
                    <view class='imgWrap {{item.active? "touchActive":""}}' style="transform: rotate({{item.angle}}deg); border: {{item.active?0*item.oScale:0}}rpx #fff dashed;">
                        <image src='{{item.image}}' data-id='{{item.id}}' style='width:{{item.width}}px;height:{{item.height}}px;' bindtouchstart='WraptouchStart' bindtouchmove='WraptouchMove' bindtouchend='WraptouchEnd'></image>
                        <image class='o' src='../../images/o.png' style='transform: scale({{item.oScale}});transform-origin:center;' data-id='{{item.id}}' bindtouchstart='oTouchStart' bindtouchmove='oTouchMove' bindtouchend='WraptouchEnd'></image>
                    </view>
                </view>
            </block>
        </view>
    </view>
</view>

let index = 0,
  items = [],
  flag = true,
  itemId = 1;

Page({
  data: {
    itemList: [],
  },

  onLoad: function (options) {
    items = this.data.itemList;
    this.setDropItem({
      url: '/images/2.png'
    });
  },

  // 初始化数据
  setDropItem(imgData) {
    let data = {}
    wx.getImageInfo({
      src: imgData.url,
      success: res => {
        data.width = res.width; //宽度
        data.height = res.height; //高度
        data.image = imgData.url; //地址
        data.id = ++itemId; //id
        data.top = 300; //top定位
        data.left =300; //left定位
        //圆心坐标
        // data.x=0;
        // data.y=0;
        data.x = data.left + data.width / 2;
        data.y = data.top + data.height / 2;
        data.scale = 1; //scale缩放
        data.oScale = 1; //方向缩放
        data.rotate = 0; //旋转角度
        data.active = true; //选中状态
        items[items.length] = data;
        this.setData({
          itemList: items
        })
      }
    })
  },

  // 放大旋转初始
  oTouchStart(e) {
    //找到点击的那个图片对象,并记录
    //获取作为移动前角度的坐标
    items[index].tx = e.touches[0].clientX;
    items[index].ty = e.touches[0].clientY;
    //移动前的角度
    items[index].anglePre = this.countDeg(items[index].x, items[index].y, items[index].tx, items[index].ty)
    //获取图片半径

  },

  // 放大旋转移动
  oTouchMove: function (e) {
    //记录移动后的位置
    items[index]._tx = e.touches[0].clientX;
    items[index]._ty = e.touches[0].clientY;

    //移动后位置的角度
    items[index].angleNext = this.countDeg(items[index].x, items[index].y, items[index]._tx, items[index]._ty)
    //角度差
    items[index].new_rotate = items[index].angleNext - items[index].anglePre;

    //叠加的角度差
    items[index].rotate += items[index].new_rotate;
    items[index].angle = items[index].rotate; //赋值

    //用过移动后的坐标赋值为移动前坐标
    items[index].tx = e.touches[0].clientX;
    items[index].ty = e.touches[0].clientY;
    items[index].anglePre = this.countDeg(items[index].x, items[index].y, items[index].tx, items[index].ty)
    console.log(items[index].anglePre)
    //赋值setData渲染
    this.setData({
      itemList: items
    })

  },

  /*
   *参数1和2为图片圆心坐标
   *参数3和4为手点击的坐标
   *返回值为手点击的坐标到圆心的角度
   */
  countDeg: function (cx, cy, pointer_x, pointer_y) {
    var ox = pointer_x - cx;
    var oy = pointer_y - cy;
    var to = Math.abs(ox / oy);
    var angle = Math.atan(to) / (2 * Math.PI) * 360;
    // console.log("ox.oy:", ox, oy)
    if (ox < 0 && oy < 0) //相对在左上角,第四象限,js中坐标系是从左上角开始的,这里的象限是正常坐标系  
    {
      angle = -angle;
    } else if (ox <= 0 && oy >= 0) //左下角,3象限  
    {
      angle = -(180 - angle)
    } else if (ox > 0 && oy < 0) //右上角,1象限  
    {
      angle = angle;
    } else if (ox > 0 && oy > 0) //右下角,2象限  
    {
      angle = 180 - angle;
    }
    return angle;
  }
})

附上代码片段

https://developers.weixin.qq.com/s/YVXlXWmQ7XJf


回答关注问题邀请回答
收藏
登录 后发表内容