图片360度无死角自由旋转

前言

 
图片360度任意旋转,
 

一、设计思路

在这里插入图片描述

1.确定中心和起始点:当鼠标点击旋转图标时,中心点为左边图片中心,起始点为旋转图标中心;

2.确定结束点:当鼠标移动时,鼠标的每一次变换都会更新结束点的坐标;

3.通过坐标点计算出该三角形三条边的长度(勾股定理),再通过余弦函数算出弧度制,反余弦函数将弧度 制转换为角度制(思路详解中会详细介绍);

4.ie下通过滤镜,其它浏览器通过rotate实现最终效果;

 

二、思路详解

1.html部分

<div class="rotateImg">
  <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.tukexw.com%2Fimg%2F1d3d6d3bc7578c21.jpg&refer=http%3A%2F%2Fimg.tukexw.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621488806&t=9afe6c19464c4570f7ad05fc1d8f2daa">
  <div  @mousedown="rotate" class="rotate-char" >
    
  </div>
</div>
<style>
  .rotateImg{
    position:fixed;
    top:64px;
    left:256px;
    z-index:99999;
    background:#ccc;
  }
  .rotate-char{
    cursor:pointer;
    width:16px;
    height:16px;
    position:absolute;
    bottom:50%;
    margin-bottom:-8px;
    right:-36px;
    background:url('https://www.easyicon.net/api/resizeApi.php?id=1223185&size=16')
  }
</style>

2.旋转的方法

function rotate(){
	
      let rotate = document.querySelector(".rotate-char");
      let move = rotate.parentNode;
      let sum = 0;
      // 坐标定义,中心点,起始点,终点鼠标移动时,实时更新
      let centercoord = [move.offsetLeft+move.clientWidth/2,move.clientHeight/2+move.offsetTop],
          firstcoord = [centercoord[0]+rotate.offsetLeft+rotate.clientWidth/2-move.clientWidth/2,centercoord[1]],
          lastcoord = [];
      //鼠标按下后移动事件
      let moveFunc = () => {
        let deg = 0;
        let e = event || window.event,x = e.clientX,y=e.clientY;
        // 中心点 开始点 结束点
        lastcoord = [x,y];
        //结束点坐标随时更新
        deg = getAngle(centercoord,firstcoord,lastcoord);
        move.setAttribute("style","transform:rotate("+deg+"deg)")
      }
      //移动事件绑定
      window.document.addEventListener("mousemove",moveFunc);
      //鼠标抬起解绑移动事件
      let cancel = () => {
        window.document.removeEventListener("mousemove",moveFunc)
      };
      window.document.addEventListener("mouseup",cancel);
}

3.获取角度的方法

getAngle(cen,first,last){

      let c_f_x = first[0] - cen[0],
          c_f_y = first[1] - cen[1],
          l_c_x = last[0] - cen[0],
          l_c_y = last[1] - cen[1];
          
      //求导出逆时针还是顺时针旋转
      let dir = c_f_x * l_c_y - c_f_y * l_c_x;
      //算出三角形三条边的长度,pow可以将数据进行乘方运算
      let len_cf = Math.sqrt(Math.pow(cen[0] - first[0],2) + Math.pow(cen[1] - first[1],2)),
          len_cl = Math.sqrt(Math.pow(cen[0] - last[0],2) + Math.pow(cen[1] - last[1],2)),
          len_fl = Math.sqrt(Math.pow(first[0] - last[0],2) + Math.pow(first[1] - last[1],2));
      
      //算出三条边后通过余弦定理求出旋转角
      let cosA = (Math.pow(len_cf,2)+Math.pow(len_cl,2) - Math.pow(len_fl,2))/(len_cf*len_cl*2);
      //弧度制转为角度制
      let angle = Math.round(Math.acos(cosA) * 180 / Math.PI);
      
      // 判断逆时针,顺时针方向旋转
      if(dir < 0){
        //逆时针
        angle = -angle
      }
      return angle;
    },

  

ps:余弦定理,忘记的看这

在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万游引力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值