reveal effects

curtain

dummy image
/**
 *  RevealCurtain
 */
class RevealCurtain {

  /**
   * Constructor
   *
   * @param target {Element} DOM
   * @param direction {String} アニメーションの方向を決定します
   */
  constructor(target, direction, speed = 1) {

    this.target = target;
    this.direction = direction;
    this.speed = speed;

    this.height = 0;
    this.width = 0;

    this._init();

  }

  _init() {

    /**
     * set CSS position prop
     * @type {string}
     */
    const position = getComputedStyle(this.target).position;

    if (position !== 'fixed' && position !== 'absolute' && position !== 'relative') {

      this.target.style.position = 'relative';

    }

    this._getSize();

    this._wrapTarget();

    this._createMask();

  }


  /**
   * create wrap target div
   * @type {string}
   * @private
   */
  _wrapTarget() {

    this.target.innerHTML = '<div class="block_inner" style="opacity: 0">' + this.target.innerHTML + '</div>';

  }

  /**
   * set original size
   * @private
   */
  _getSize() {

    this.height = this.target.clientHeight;
    this.width = this.target.clientWidth;

  }

  /**
   * initialize rect size
   * @returns {string}
   */
  _initRect() {

    if (this.direction === 'lr') {

      return `rect(0px 0px ${this.height}px 0px)`;

    } else if (this.direction === 'rl') {

      return `rect(0px ${this.width}px ${this.height}px ${this.width}px)`;
    } else if (this.direction === 'tb') {

      return `rect(0px ${this.width}px 0px 0px)`;

    } else if (this.direction === 'bt') {

      return `rect(${this.height}px ${this.width}px ${this.height}px 0px)`;

    }

  }

  /**
   * create mask element
   * @type {HTMLElement}
   */
  _createMask() {

    this.mask = createDOMElement('div', 'block_mask');
    this.mask.style.position = 'absolute';
    this.mask.style.zIndex = 99;
    this.mask.style.top = 0;
    this.mask.style.left = 0;
    this.mask.style.right = 0;
    this.mask.style.bottom = 0;
    this.mask.style.backgroundColor = '#282828';

    TweenMax.set(this.mask, {

      clip: this._initRect(this.direction),

    });

    /**
     * insert mask
     */
    this.target.insertAdjacentElement('afterbegin', this.mask);

  }

  /**
   * get animation rect position
   * @param end
   * @returns {string}
   */
  _getRect(end) {

    const rect = {

      top: 0,
      right: 0,
      bottom: 0,
      left: 0,

    };

    if (this.direction === 'lr') {

      rect.bottom = this.height;
      rect.right = this.width;

      if (end) rect.left = this.width;

    } else if (this.direction === 'rl') {

      rect.bottom = this.height;
      rect.right = this.width;

      if (end) rect.right = 0;

    } else if (this.direction === 'tb') {

      rect.right = this.width;
      rect.bottom = this.height;

      if (end) rect.top = this.height;

    } else if (this.direction === 'bt') {

      rect.right = this.width;
      rect.bottom = this.height;

      if (end) rect.bottom = 0;

    }

    return `rect(${rect.top}px ${rect.right}px ${rect.bottom}px ${rect.left}px)`;

  }

  // public
  // - - - -- - - - - -  - - - -
  /**
   * アニメーション実行
   */
  anim() {

    const tl       = new TimelineMax({}),
          fromRect = this._getRect(false),
          toRect   = this._getRect(true);

    tl.to(this.mask, .75, {

      clip: fromRect,
      ease: Power1.easeOut,

    }).add(() => {

      this.mask.nextElementSibling.style.opacity = 1;

    }).to(this.mask, .65, {

      clip: toRect,
      ease: Power3.easeOut,
      delay: .5,

    });

  }

}

export default RevealCurtain;

/**
 *
 * @param type
 * @param className
 * @param content
 * @returns {HTMLElement}
 */
function createDOMElement(type, className, content) {
  const el = document.createElement(type);
  el.className = className || '';
  el.innerHTML = content || '';
  return el;
}
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

clip

dummy image
class RevealClip {

  /**
   * Constructor
   *
   * @param target {Element} target element
   * @param direction {String} animation direction
   */
  constructor(target, direction, speed = 1) {

    this.target = target;
    this.direction = direction;
    this.speed = speed;

    this.height = 0;
    this.width = 0;

    this._init();

  }

  /**
   * set original size
   * @private
   */
  _getSize() {

    this.height = this.target.clientHeight;
    this.width = this.target.clientWidth;

    this._setSize();

  }

  /**
   * set wrapper size
   * @private
   */
  _setSize() {

    this.target.style.height = this.height + 'px';
    this.target.style.width = this.width + 'px';

  }

  /**
   * initialize rect position
   * @returns {string}
   */
  _getInitRect() {

    if (this.direction === 'lr') {

      return `rect(0px 0px ${this.height}px 0px)`;

    } else if (this.direction === 'rl') {

      return `rect(0px ${this.width}px ${this.height}px ${this.width}px)`;
    } else if (this.direction === 'tb') {

      return `rect(0px ${this.width}px 0px 0px)`;

    } else if (this.direction === 'bt') {

      return `rect(${this.height}px ${this.width}px ${this.height}px 0px)`;

    }

  }

  /**
   * initialize
   * @private
   */
  _init() {

    /**
     * get position prop
     * @type {string}
     */
    const position = getComputedStyle(this.target).position;

    /**
     * set position prop
     */
    if (position !== 'fixed' && position !== 'absolute' && position !== 'relative') {

      this.target.style.position = 'relative';

    }

    this._getSize();

    /**
     * create inner div
     * @type {string}
     */
    this.target.innerHTML = '<div class="reveal-curtain__inner" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0; z-index: 9;">' + this.target.innerHTML + '</div>';

    TweenMax.set(this.target.firstElementChild, {

      clip: this._getInitRect(),

    });

  }

  /**
   * get rect animation position
   * @returns {string}
   */
  _getRect() {

    let rect = {
      top: 0,
      right: 0,
      bottom: 0,
      left: 0,
    };

    if (this.direction === 'lr') {

      rect.bottom = this.height;
      rect.right = this.width;

    } else if (this.direction === 'rl') {

      rect.bottom = this.height;
      rect.right = this.width;

    } else if (this.direction === 'tb') {

      rect.right = this.width;
      rect.bottom = this.height;

    } else if (this.direction === 'bt') {

      rect.right = this.width;
      rect.bottom = this.height;

    }

    return `rect(${rect.top}px ${rect.right}px ${rect.bottom}px ${rect.left}px)`;

  }

  /**
   * animation start to
   */
  animTo() {

    TweenMax.to(this.target.firstElementChild, this.speed, {

      clip: this._getRect(),
      ease: Expo.easeOut,

    });

  }

  /**
   * animation start from
   */
  animFrom() {

    TweenMax.to(this.target.firstElementChild, this.speed, {

      clip: this._getInitRect(),
      ease: Expo.easeOut,

    });

  }

  /**
   * animation start from to
   */
  animFromTo() {

    const tl = new TimelineMax({});

    tl
      .to(this.target.firstElementChild, this.speed, {

        clip: this._getInitRect(),
        ease: Expo.easeOut,

      })
      .to(this.target.firstElementChild, this.speed, {
        clip: this._getRect(),
        ease: Expo.easeOut,
      });

  }

}
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196