UNPKG

3.16 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var Graphic_1 = require("./../Graphic");
14var Module_1 = require("./../Module");
15var Tool_1 = require("./../Tool");
16var Spinner = (function (_super) {
17 __extends(Spinner, _super);
18 /* LIFECYCLE */
19 function Spinner() {
20 var _this = _super.call(this) || this;
21 /**
22 * Shape lines of the spinner
23 */
24 _this.lines = [];
25 _this.setProps({
26 lines: 16,
27 lineWidth: 10,
28 centerSpacing: 15,
29 lineHeight: 2,
30 speed: 1000,
31 color: Tool_1.Color.white
32 });
33 _this.connect(_this.signals.propChange, ["lineWidth", "lines"], _this.onLineChange.bind(_this))
34 .connect(_this.signals.propChange, "speed", _this.onSpeedChange.bind(_this));
35 return _this;
36 }
37 Spinner.prototype.initialize = function (props) {
38 _super.prototype.initialize.call(this, props);
39 this.onSpeedChange();
40 this.onLineChange();
41 };
42 /* EVENTS */
43 /**
44 * Update the alpha of each lines
45 * @param tick - Tick provided by the timer
46 * @param value - Value of the timer
47 * @param ratio - Ratio of the timer
48 */
49 Spinner.prototype.updateLinesAlpha = function (tick, value, ratio) {
50 var _this = this;
51 var length = this.lines.length;
52 this.lines.forEach(function (line, i) { return line.props.fillAlpha = 1 - (ratio + (i / _this.props.lines)) % 1; });
53 };
54 /**
55 * When lines attributes has changed
56 */
57 Spinner.prototype.onLineChange = function () {
58 var angleFactor = 360 / this.props.lines;
59 this.lines.forEach(function (lines) { return lines.kill(); });
60 this.lines = [];
61 for (var i = 0; i < this.props.lines; i++) {
62 this.lines.push(this.spawn(new Module_1.Shape(), this.props.centerSpacing, 0, {
63 offsetX: this.props.centerSpacing,
64 offsetY: 0,
65 fill: this.props.color,
66 fillAlpha: (i / this.props.lines) % 1,
67 width: this.props.lineWidth,
68 height: this.props.lineHeight,
69 angle: angleFactor * i
70 }));
71 }
72 this.lines.reverse();
73 this.props.width = this.container.width;
74 this.props.height = this.container.height;
75 };
76 Spinner.prototype.onSpeedChange = function () {
77 this.timers.addTimer("spin", this.props.speed, null, {
78 recurrence: -1,
79 update: this.updateLinesAlpha.bind(this)
80 });
81 };
82 return Spinner;
83}(Graphic_1.Graphic));
84exports.Spinner = Spinner;