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

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

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

在这里插入图片描述

<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

相关文章:

  • 线程同步:多线程编程的核心机制
  • 确定一个数字是否为 2 的幂
  • 为什么 Young GC 比 Full GC 快
  • 北京迅为iTOP-RK3568开发板OpenHarmony系统南向驱动开发实操-HDF驱动配置LED
  • Muduo库的简介与使用
  • Java常用算法
  • 使用Fluent-bit将容器标准输入和输出的日志发送到Kafka
  • 序列化和反序列化TCP粘包问题
  • 性能调优与抓包分析:TCP三次握手、CDN优化与Wireshark实战
  • 【Git】配置Git
  • 软考计算机知识-流水线
  • 【云原生】动态资源分配(DRA)深度洞察报告
  • 技术速递|Visual Studio Code 2025年2月更新(v1.98)
  • 网络华为HCIA+HCIP数据链路层协议-以太网协议
  • 手写一个简易版的tomcat
  • 解释VLA和具身智能之间的关系
  • 神经网络中常用语言特性(python)(待完善)
  • 视创云展:打造沉浸式体验环境,助力企业线上营销
  • 【multisim设计一个数控脉宽脉冲信号发生器电路图】2022-9-14
  • Linux》Ubuntu》Docker >>安装中文版GitLab compose
  • 河南一县政府党组成员签订抵制违规吃喝问题承诺书,现场交给县长
  • 从能源装备向应急装备蓝海拓展,川润股份发布智能综合防灾应急仓
  • 制造四十余年血腥冲突后,库尔德工人党为何自行解散?
  • 兰州大学教授安成邦加盟复旦大学中国历史地理研究所
  • 中央结算公司:减免境外央行类机构账户开户费用
  • 前四月全国铁路完成固定资产投资1947亿元,同比增长5.3%