当前位置: 首页 > article >正文

外层元素旋转,其包括在内的子元素一并旋转(不改变旋转中心),单元测试

思路:外层旋转后坐标,元素旋转后坐标,计算偏移坐标
在这里插入图片描述

在这里插入图片描述

<template>
  <div class="outbox">
    <label>角度: <input v-model.number="rotate" type="number" /></label><br>
    <div class="resizebox" :style="{
      width: `${resize.w}px`, 
      height: `${resize.h}px`, 
      transform: `translate(${resize.x}px, ${resize.y}px) rotate(${rotate}deg)`}"
    >
    </div>
    <div :class="['itxm', `itxm-${idx}`]" v-for="(item, idx) in points" :key="idx" :style="{
      width: `${item.w}px`, 
      height: `${item.h}px`, 
      transform: `translate(${item.x}px, ${item.y}px) rotate(${rotate}deg)`}">
      {{idx+1}}
    </div>
  </div>
</template>
<script>

export default {
  name: 'HelloWorld',
  data() {
    return {
      rotate: 15,
      resize: {
        x: 200,
        y: 200,
        w: 500,
        h: 300,
        r: 60
      },
      points: [
        {
          x: 200,
          y: 200,
          w: 50,
          h: 30,
          r: 0
        },
        {
          x: 650,
          y: 200,
          w: 50,
          h: 30,
          r: 0
        },
        {
          x: 300,
          y: 300,
          w: 50,
          h: 30,
          r: 0
        },
        {
          x: 200,
          y: 470,
          w: 50,
          h: 30,
          r: 0
        },
        {
          x: 650,
          y: 470,
          w: 50,
          h: 30,
          r: 0
        }
      ]
    };
  },
  watch: {
    rotate() {
      this.watchRotate();
    }
  },
  mounted() {
    this.watchRotate();
  },
  methods: {
    watchRotate() {
      const cx = this.resize.x+this.resize.w/2
      const cy = this.resize.y+this.resize.h/2

      this.points.forEach(v => {
        if (!v.oldx) v.oldx = v.x
        if (!v.oldy) v.oldy = v.y
        // resize旋转为矩形参考
        const rotateEnd = this.recaculateXY({x: v.oldx || v.x, y: v.oldy || v.y, cx, cy, rotate: this.rotate})
        const rotateThis = this.recaculateXY({x: v.x, y: v.y, cx: v.x+v.w/2, cy: v.y+v.h/2, rotate: this.rotate})
        // 外层旋转坐标偏移 - 自身旋转坐标偏移
        const rangex = rotateEnd.x - rotateThis.x
        const rangey = rotateEnd.y - rotateThis.y
        v.x += rangex
        v.y += rangey
      })

    },
    recaculateXY({x, y, cx, cy, rotate}) {
      // 矩阵公式
      const rad = (rotate * Math.PI) / 180;
      const rotatedX = (x - cx) * Math.cos(rad) - (y - cy) * Math.sin(rad) + cx
      const rotatedY = (x - cx) * Math.sin(rad) + (y - cy) * Math.cos(rad) + cy
      return {x: rotatedX, y: rotatedY}
    },
  },
};
</script>

<style scoped>
.outbox {
  position: relative;
}
.resizebox {
  width: 500px;
  height: 300px;
  border: 1px dotted blue;
  position: absolute;
  left: 0;
  top: 0;
  &::after {
    content: '';
    width: 100%;
    height: 1px;
    background: red;
    display: block;
    position: absolute;
    top: 50%;
  }
  &::before {
    content: '';
    width: 1px;
    height: 100%;
    background: red;
    display: block;
    position: absolute;
    left: 50%;
  }
}
.itxm {
  background: green;
  position: absolute;
  left: 0;
  top: 0;
}
.itxm-1 {
  background: orange;
}
.itxm-2 {
  background: rebeccapurple;
}
</style>

–end


http://www.kler.cn/a/581149.html

相关文章:

  • 爱普生可编程晶振SG-8200CJ特性与应用
  • Sentinel熔断降级
  • Seata简要说明
  • 《C#上位机开发从门外到门内》2-2:I2C总线协议及其应用详解
  • lua如何写出高性能的kong网关插件
  • ctf-web: php原生类利用 -- GHCTF Popppppp
  • 说一下spring的事务隔离级别?
  • 数据结构全解析:从线性到非线性,优缺点与应用场景深度剖析
  • bug-Ant中a-select的placeholder不生效(绑定默认值为undefined)
  • Git的必要配置
  • 基于MCAL的S32K3 GPIO外部中断使用
  • 怎么实现: 大语言模型微调案例
  • 从零开始实现大语言模型(十三):预训练大语言模型GPTModel
  • Netty基础—2.网络编程基础四
  • MoonSharp 文档三
  • Session、Cookie、Token的区别
  • 【每日学点HarmonyOS Next知识】状态变量、动画UI残留、Tab控件显示、ob前缀问题、文字背景拉伸
  • SICK Ranger3源码分析——断线重连
  • python之使用scapy扫描本机局域网主机,输出IP/MAC表
  • 算法面试题深度解析:LeetCode 2012.数组元素的美丽值求和计算与多方案对比