aboutsummaryrefslogtreecommitdiff
path: root/js/lib/heart_renderer.js
blob: 4680fb93a187864e09149584e9e6cdfd5532f7ef (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
'use strict';

((window) => {

  const kColorIteratorLimit = 256;

  /**
   * Renders a Heart, has its own canvas, which will be placed in the element
   * set by calling #render.
   *
   * @class HeartRenderer
   * @param {object} config configuration, extends any member of the renderer
   */
  const HeartRenderer = class HeartRenderer {

    constructor(config) {

      /**
       * The instance of the heart renderer being used
       *
       * @memberof HeartRenderer
       * @instance
       * @name canvas
       * @type HTMLCanvasElement
       * @default A brand new canvas
       */
      this.canvas = window.document.createElement('canvas');

      /**
       * The maximum fps that will be used
       *
       * @memberof HeartRenderer
       * @instance
       * @name fps
       * @type Number
       * @default 60
       */
      this.fps = 60;

      /**
       * The size of the heart as a percentage of the canvas smallest dimension
       *
       * @memberof HeartRenderer
       * @instance
       * @name heartSize
       * @type Number
       * @default 40
       */
      this.heartSize = 40;

      /**
       * The max size of the heart as a percentage of the canvas smallest dimension
       *
       * @memberof HeartRenderer
       * @instance
       * @name maxHeartSize
       * @type Number
       * @default 75
       */
      this.maxHeartSize = 75;

      /**
       * The min size of the heart as a percentage of the canvas smallest dimension
       *
       * @memberof HeartRenderer
       * @instance
       * @name minHeartSize
       * @type Number
       * @default 10
       */
      this.minHeartSize = 10;

      this._ticking = false; // Lock for wheel event.
      this._resizeMagnitude = 0.1; // Multiplies the wheel delta to resize the heart
      this._resizeSpeed = 1; // How many percent points per frame we'll resize to match target
      this._trackingSpeed = 10; // How many pixels per frame will we move to match target
      this._following = null; // The status of mouse follow.
      this._center = null; // The actual center
      this._targetHeartSize = this.heartSize;
      this._targetCenter = {
        x: 0,
        y: 0
      }; // the target coordinates
      this._animating = false; // The status of the animation.
      this._previousFrameTime = Date.now(); // The timestamp of the last frame for fps control
      this._cursorTimeout = 500; // Timeout to hide the cursor in milliseconds
      this._currentColor = { // The current color that will be painted
        red: 100,
        blue: 0,
        green: 50
      };
      this._colorSpeed = {
        red: 0.1,
        blue: 0.2,
        green: 0.15
      };
      this._colorDirection = {
        red: 1,
        blue: 1,
        green: 1
      };

      this._detectWheel();
      this.startFollowingMouse();

      Object.assign(this, config);
    }

    /**
     * Attaches the canvas to an HTML element
     *
     * @memberof HeartRenderer
     * @function render
     * @instance
     * @param {HTMLElement} element the element where we will attach our canvas
     */
    render(element) {

      element.appendChild(this.canvas);

      this._targetCenter = {
        x: Math.round(this.canvas.width / 2),
        y: Math.round(this.canvas.height / 2)
      }; // the target coordinates
      this.resize();
    }

    /**
     * Resizes the canvas
     *
     * @memberof HeartRenderer
     * @function render
     * @instance
     */
    resize() {

      if (this.canvas.parentElement) {
        this.canvas.width = this.canvas.parentElement.offsetWidth;
        this.canvas.height = this.canvas.parentElement.offsetHeight;
      }
    }

    /**
     * Follows the mouse
     *
     * @memberof HeartRenderer
     * @function startFollowingMouse
     * @instance
     */
    startFollowingMouse() {

      if (!this._following) {
        this._following = this._setCenterFromMouse.bind(this);
        this.canvas.addEventListener('mousemove', this._following);
      }
    }

    /**
     * Stop following the mouse
     *
     * @memberof HeartRenderer
     * @function stopFollowingMouse
     * @instance
     */
    stopFollowingMouse() {

      if (this._following) {
        this.canvas.removeEventListener('mouseover', this._following);
        this._following = null;
        this._targetCenter = {
          x: Math.round(this.canvas.width / 2),
          y: Math.round(this.canvas.height / 2)
        }; // the target coordinates
      }
    }

    /**
     * Gets the context from the current canvas and starts the animation process
     *
     * @memberof HeartRenderer
     * @function activate
     * @instance
     */
    activate() {

      const context = this.canvas.getContext('2d');
      this._startAnimating(context);
    }

    /**
     * Stops the animation process
     *
     * @memberof HeartRenderer
     * @function deactivate
     * @instance
     */
    deactivate() {

      this._stopAnimating();
    }

    // Starts the animation loop
    _startAnimating(context) {

      this._frameDuration = 1000 / this.fps;
      this._animating = true;

      this._animate(context);
    }

    // Stops the animation on the next frame.
    _stopAnimating() {

      this._animating = false;
    }

    // Runs the animation step controlling the FPS
    _animate(context) {

      if (!this._animating) {
        return;
      }

      window.requestAnimationFrame(this._animate.bind(this, context));

      const currentFrameTime = Date.now();
      const delta = currentFrameTime - this._previousFrameTime;

      if (delta > this._frameDuration) {
        this._previousFrameTime = Date.now();
        this._animateStep(context, delta);
      }
    }

    // The actual animation processing function.
    _animateStep(context, delta) {

      this._updateColor(delta);
      this._drawHeart(context, delta);
    }

    // Updates the current color
    _updateColor(delta) {

      const red = this._updateColorComponent('red', delta);
      const green = this._updateColorComponent('green', delta);
      const blue = this._updateColorComponent('blue', delta);

      this._currentColor.red = red;
      this._currentColor.green = green;
      this._currentColor.blue = blue;
    }

    // Updates a single color component.
    _updateColorComponent(component, delta) {

      let color = Math.round(this._currentColor[component] + (delta * this._colorSpeed[component] * this._colorDirection[component]));
      if (color >= kColorIteratorLimit) {
        this._colorDirection[component] = -1;
        color = kColorIteratorLimit;
      }

      if (color <= 0) {
        this._colorDirection[component] = 1;
        color = 0;
      }

      return color;
    }

    // Draws a heart
    _drawHeart(context, delta) {

      const canvasHeight = this.canvas.height;
      const canvasWidth = this.canvas.width;

      const referenceDimension = canvasWidth < canvasHeight ? canvasWidth : canvasHeight;

      this.heartSize += Math.sign(this._targetHeartSize - this.heartSize) * this._resizeSpeed;

      const heartSize = Math.round(referenceDimension * this.heartSize * .01);
      const radius = heartSize / 2;

      if (!this._center) {
        this._center = {};
        this._center.x = Math.round(canvasWidth / 2);
        this._center.y = Math.round(canvasHeight / 2);
      }

      const deltaY = this._targetCenter.y - this._center.y;
      const deltaX = this._targetCenter.x - this._center.x;
      const angle = Math.atan2(deltaY, deltaX);

      // Move towards the target
      this._center.x += Math.cos(angle) * this._trackingSpeed;
      this._center.y += Math.sin(angle) * this._trackingSpeed;

      const canvasCenterX = this._center.x;
      const canvasCenterY = this._center.y;
      const centerX = -radius;
      const centerY = -radius;


      // translate and rotate, adjusting for weight of the heart.
      context.translate(canvasCenterX, canvasCenterY + radius / 4);
      context.rotate(-45 * Math.PI / 180);

      // Fill the ventricles of the heart
      context.fillStyle = `rgb(${this._currentColor.red}, ${this._currentColor.green}, ${this._currentColor.blue})`;
      context.fillRect(centerX, centerY, heartSize, heartSize);

      // Left atrium
      context.beginPath();
      context.arc(centerX + radius, centerY, radius, 0, 2 * Math.PI, false);
      context.fill();
      context.closePath();

      // Right atrium
      context.beginPath();
      context.arc(centerX + heartSize, centerY + radius, radius, 0, 2 * Math.PI, false);
      context.fill();
      context.closePath();

      context.setTransform(1, 0, 0, 1, 0, 0);
    }

    // Sets the center from mouse
    _setCenterFromMouse(event) {

      this._showCursor();
      this._targetCenter.x = event.offsetX;
      this._targetCenter.y = event.offsetY;
      setTimeout(this._hideCursor.bind(this), this._cursorTimeout);
    }

    // Binds the wheel event to resize the heart
    _detectWheel() {

      this.canvas.addEventListener('wheel', this._onWheel.bind(this));
    }

    // Handle the mouse wheel movement
    _onWheel(event) {

      if (!this._ticking) {
        this._ticking = true;
        window.requestAnimationFrame(this._resizeHeartFromDelta.bind(this, event.deltaY));
      }
    }

    // Use delta to resize the heart
    _resizeHeartFromDelta(delta) {

      let heartSize = this.heartSize + (this._resizeMagnitude * delta);

      if (heartSize > this.maxHeartSize) {
        heartSize = this.maxHeartSize;
      }

      if (heartSize < this.minHeartSize) {
        heartSize = this.minHeartSize;
      }

      this._targetHeartSize = heartSize;
      this._ticking = false;
    }

    // Apply a class to show the cursor.
    _showCursor() {

      this.canvas.classList.add('mouse-moving');
    }

    // Remove class to hide the cursor.
    _hideCursor() {

      this.canvas.classList.remove('mouse-moving');
    }
  };


  window.HeartRenderer = HeartRenderer;
})(window);