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

思路:外层旋转后坐标,元素旋转后坐标,计算偏移坐标

复制代码
<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

相关推荐
全栈陈序员19 小时前
前端文件下载常用方式详解
前端·javascript·chrome·ajax·css3·html5·safari
普宁彭于晏21 小时前
CSS3相关知识点
前端·css·笔记·学习·css3
站在风口的猪11081 天前
《前端面试题:CSS预处理器(Sass、Less等)》
前端·css·html·less·css3·sass·html5
lexiangqicheng1 天前
es6+和css3新增的特性有哪些
前端·es6·css3
站在风口的猪11082 天前
《前端面试题:CSS3新特性》
前端·css·html·css3·html5
喵手2 天前
CSS3 渐变、阴影和遮罩的使用
前端·css·css3
十年砍柴---小火苗2 天前
原生js操作元素类名(classList,classList.add...)
javascript·css·css3
站在风口的猪11082 天前
《前端面试题:CSS对浏览器兼容性》
前端·css·html·css3·html5
站在风口的猪11082 天前
《前端面试题:前端布局全面解析(圣杯布局、双飞翼布局等)》
前端·css·html·css3·html5
站在风口的猪11083 天前
《前端面试题:HTML5、CSS3、ES6新特性》
前端·css3·html5