# rotate3d

# rotateX

# rotateY

# rotateXYZ

import { gsap } from 'gsap/all'
import math from './math';

class Rotate3d {

  /**
   * constructor
   * @param targets {Object}
   * @param speed {Number}
   * @param wrapWidth {Number}
   * @param wrapHeight {Number}
   * @param wrap {Object}
   */
  constructor(targets, speed = 2, wrapWidth = window.innerWidth, wrapHeight = window.innerHeight, wrap) {

    this.targets = [];
    this.wrap = wrap;

    this.speed = speed;

    this.wrapWidth = wrapWidth;
    this.wrapHeight = wrapHeight;

    /**
     * オブジェクトの位置を動かすためのパラメータの基準値
     * これにランダム値とかで加工する
     *
     * @type {{
     *   x: {
     *    range: number, 変化の範囲
     *    speed: number  変化のスピード
     *   },
     *   y: {range: number, speed: number},
     *   z: {range: number, speed: number}
     * }}
     */
    this.position = {
      x: {
        range: .25,
        speed: 1,
        offset: 0,
      },
      y: {
        range: .25,
        speed: 1,
        offset: 0,
      },
      z: {
        range: .25,
        speed: 1,
      },
    };

    // 中心座標
    this.center = {
      x: this.wrapWidth / 2 - targets[0].offsetWidth / 2,
      y: this.wrapHeight / 2 - targets[0].offsetHeight / 2,
    };

    // マウスの座標
    this.mouse = {
      x: 0,
      y: 0,
      ease: .1,
    };

    /**
     * init element
     * 各オブジェクトの初期値などを設定
     */
    targets.forEach((el, i) => {

      /**
       * @type {{
       *   el: HTMLElement,
       *   x: number,
       *   y: number,
       *   z: number
       *   scale: number,
       *   speedZ: number,
       *   speedY: number,
       *   speedX: number,
       * }}
       */
      const obj = {
        el: el,
        x: math.random(-this.wrapWidth * this.position.x.range, this.wrapWidth * this.position.x.range),
        y: math.random(-this.wrapHeight * this.position.y.range, this.wrapHeight * this.position.y.range),
        z: math.random(-this.wrapHeight * this.position.z.range, this.wrapHeight * this.position.z.range),
        speedX: math.random(1, 4) * this.position.x.speed * this.speed,
        speedY: math.random(1, 4) * this.position.y.speed * this.speed,
        speedZ: math.random(1, 4) * this.position.z.speed * this.speed,
        scale: math.random(1, 2),
      };

      this.targets.push(obj);

    });

    // event
    this.wrap.addEventListener('mousemove', e => this.getMousePosition(e));

  }

  /**
   * get mouse current position
   * @param e
   */
  getMousePosition(e) {

    this.mouse.x = e.offsetX;
    this.mouse.y = e.offsetY;

  }

  /**
   * animation start
   */
  play() {

    const offsetX = math.map(this.mouse.y, 0, this.wrapWidth, -1.25, 1.25);
    const offsetY = math.map(this.mouse.x, 0, this.wrapHeight, -1.25, 1.25);

    this.position.x.offset += (offsetX - this.position.x.offset) * this.mouse.ease;
    this.position.y.offset += (offsetY - this.position.y.offset) * this.mouse.ease;

    this.targets.forEach((el, i) => {

      // オブジェクトの座標を更新
      math.rotateX(el, math.angleToRadian(el.speedX * this.position.x.offset));
      math.rotateY(el, math.angleToRadian(el.speedY * this.position.y.offset));
      math.rotateZ(el, math.angleToRadian(el.speedZ));

      gsap.set(el.el, {
        scale: el.scale,
        x: el.x + this.center.x,
        y: el.y + this.center.y,
        z: el.z,
      });

    });

    requestAnimationFrame(() => this.play());

  }

}

export default Rotate3d;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
Last Updated: 1/16/2020, 8:19:31 AM