@checksub_team/peaks_timeline 1.4.34 → 1.4.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,238 +1,238 @@
1
- /**
2
- * @file
3
- *
4
- * Defines the {@link TimelineAxis} class.
5
- *
6
- * @module timeline-axis
7
- */
8
-
9
- define([
10
- './utils',
11
- 'konva'
12
- ], function(Utils, Konva) {
13
- 'use strict';
14
-
15
- var LEFT_PADDING = 4;
16
-
17
- /**
18
- * Creates the timeline axis shapes and adds them to the given view layer.
19
- *
20
- * @class
21
- * @alias TimelineAxis
22
- *
23
- * @param {WaveformOverview|TimelineZoomView} view
24
- * @param {Object} options
25
- * @param {String} options.axisGridlineColor
26
- * @param {String} options.axisLabelColor
27
- */
28
-
29
- function TimelineAxis(peaks, view, options) {
30
- var self = this;
31
-
32
- peaks.on('playhead.moved', this._onPlayheadMoved.bind(this));
33
- peaks.on('playhead.hidden', this._onPlayheadHidden.bind(this));
34
-
35
- self._axisGridlineColor = options.axisGridlineColor;
36
- self._axisLabelColor = options.axisLabelColor;
37
-
38
- self._backLayer = new Konva.Layer({
39
- listening: false
40
- });
41
-
42
- self._frontLayer = new Konva.Layer({
43
- listening: false
44
- });
45
-
46
- self._axisShape = new Konva.Shape({
47
- sceneFunc: function(context) {
48
- self.drawAxis(context, view);
49
- }
50
- });
51
- self._backLayer.add(self._axisShape);
52
-
53
- self._timesShape = new Konva.Shape({
54
- sceneFunc: function(context) {
55
- self.drawTimes(context, view);
56
- }
57
- });
58
- self._frontLayer.add(self._timesShape);
59
- }
60
-
61
- TimelineAxis.prototype._onPlayheadMoved = function(playheadX, playheadWidth) {
62
- this._maskStart = playheadX;
63
- this._maskEnd = playheadX + playheadWidth;
64
- this._frontLayer.draw();
65
- };
66
-
67
- TimelineAxis.prototype._onPlayheadHidden = function() {
68
- this._maskStart = null;
69
- this._maskEnd = null;
70
- this._frontLayer.draw();
71
- };
72
-
73
- TimelineAxis.prototype.draw = function() {
74
- this._backLayer.draw();
75
- this._frontLayer.draw();
76
- };
77
-
78
- TimelineAxis.prototype.addBackToStage = function(stage) {
79
- stage.add(this._backLayer);
80
- };
81
-
82
- TimelineAxis.prototype.addFrontToStage = function(stage) {
83
- stage.add(this._frontLayer);
84
- };
85
-
86
- /**
87
- * Returns number of seconds for each x-axis marker, appropriate for the
88
- * current zoom level, ensuring that markers are not too close together
89
- * and that markers are placed at intuitive time intervals (i.e., every 1,
90
- * 2, 5, 10, 20, 30 seconds, then every 1, 2, 5, 10, 20, 30 minutes, then
91
- * every 1, 2, 5, 10, 20, 30 hours).
92
- *
93
- * @param {WaveformOverview|WaveformZoomView} view
94
- * @returns {Number}
95
- */
96
-
97
- TimelineAxis.prototype.getAxisLabelScale = function(view) {
98
- var baseSecs = 1; // seconds
99
- var steps = [0.1, 0.5, 1, 2, 5, 10, 20, 30];
100
- var minSpacing = 60;
101
- var index = 0;
102
-
103
- var secs;
104
-
105
- for (;;) {
106
- secs = baseSecs * steps[index];
107
- var pixels = view.timeToPixels(secs);
108
-
109
- if (pixels < minSpacing) {
110
- if (++index === steps.length) {
111
- baseSecs *= 60; // seconds -> minutes -> hours
112
- index = 0;
113
- }
114
- }
115
- else {
116
- break;
117
- }
118
- }
119
-
120
- return secs;
121
- };
122
-
123
- /**
124
- * Draws the time axis and labels onto a view.
125
- *
126
- * @param {Konva.Context} context The context to draw on.
127
- * @param {WaveformOverview|WaveformZoomView} view
128
- */
129
-
130
- TimelineAxis.prototype.drawAxis = function(context, view) {
131
- var currentFrameStartTime = view.pixelsToTime(view.getFrameOffset());
132
-
133
- // Time interval between axis markers (seconds)
134
- var axisLabelIntervalSecs = this.getAxisLabelScale(view);
135
-
136
- // Time of first axis marker (seconds)
137
- var firstAxisLabelSecs = Utils.roundUpToNearest(currentFrameStartTime, axisLabelIntervalSecs);
138
-
139
- // Distance between waveform start time and first axis marker (seconds)
140
- var axisLabelOffsetSecs = firstAxisLabelSecs - currentFrameStartTime;
141
-
142
- // Distance between waveform start time and first axis marker (pixels)
143
- var axisLabelOffsetPixels = view.timeToPixels(axisLabelOffsetSecs);
144
-
145
- context.setAttr('strokeStyle', this._axisGridlineColor);
146
- context.setAttr('lineWidth', 1);
147
-
148
- // Set text style
149
- context.setAttr('font', '11px sans-serif');
150
- context.setAttr('fillStyle', this._axisLabelColor);
151
- context.setAttr('textAlign', 'left');
152
- context.setAttr('textBaseline', 'bottom');
153
-
154
- var secs = firstAxisLabelSecs;
155
- var x;
156
-
157
- var width = view.getWidth();
158
- var height = view.getHeight();
159
-
160
- for (;;) {
161
- // Position of axis marker (pixels)
162
- x = axisLabelOffsetPixels + view.timeToPixels(secs - firstAxisLabelSecs);
163
- if (x >= width) {
164
- break;
165
- }
166
-
167
- context.beginPath();
168
- context.moveTo(x + 0.5, 0);
169
- context.lineTo(x + 0.5, height);
170
- context.stroke();
171
-
172
- secs += axisLabelIntervalSecs;
173
- }
174
- };
175
-
176
- TimelineAxis.prototype.drawTimes = function(context, view) {
177
- var currentFrameStartTime = view.pixelsToTime(view.getFrameOffset());
178
-
179
- // Time interval between axis markers (seconds)
180
- var axisLabelIntervalSecs = this.getAxisLabelScale(view);
181
-
182
- // Time of first axis marker (seconds)
183
- var firstAxisLabelSecs = Utils.roundUpToNearestPositive(
184
- currentFrameStartTime - 1,
185
- axisLabelIntervalSecs
186
- );
187
-
188
- // Distance between waveform start time and first axis marker (seconds)
189
- var axisLabelOffsetSecs = firstAxisLabelSecs - currentFrameStartTime;
190
-
191
- // Distance between waveform start time and first axis marker (pixels)
192
- var axisLabelOffsetPixels = view.timeToPixels(axisLabelOffsetSecs);
193
-
194
- // Set text style
195
- context.setAttr('font', '11px sans-serif');
196
- context.setAttr('fillStyle', this._axisLabelColor);
197
- context.setAttr('textAlign', 'left');
198
- context.setAttr('textBaseline', 'bottom');
199
-
200
- var secs = firstAxisLabelSecs;
201
- var x;
202
-
203
- var width = view.getWidth();
204
-
205
- for (;;) {
206
- // Position of axis marker (pixels)
207
- x = axisLabelOffsetPixels + view.timeToPixels(secs - firstAxisLabelSecs);
208
-
209
- if (x >= width) {
210
- break;
211
- }
212
-
213
- var label = Utils.formatTime(secs, false);
214
- var labelHeight = context.measureText(label).actualBoundingBoxAscent
215
- - context.measureText(label).actualBoundingBoxDescent;
216
- var labelWidth = context.measureText(label).width;
217
- var labelX = x + LEFT_PADDING;
218
- var labelY = labelHeight;
219
-
220
- if (!this._labelIsMasked(labelX, labelX + labelWidth)) {
221
- context.fillText(label, labelX, labelY);
222
- }
223
-
224
- secs += axisLabelIntervalSecs;
225
- }
226
- };
227
-
228
- TimelineAxis.prototype._labelIsMasked = function(labelStart, labelEnd) {
229
- if (this._maskStart && this._maskEnd) {
230
- if (labelEnd > this._maskStart && labelStart < this._maskEnd) {
231
- return true;
232
- }
233
- }
234
- return false;
235
- };
236
-
237
- return TimelineAxis;
238
- });
1
+ /**
2
+ * @file
3
+ *
4
+ * Defines the {@link TimelineAxis} class.
5
+ *
6
+ * @module timeline-axis
7
+ */
8
+
9
+ define([
10
+ './utils',
11
+ 'konva'
12
+ ], function(Utils, Konva) {
13
+ 'use strict';
14
+
15
+ var LEFT_PADDING = 4;
16
+
17
+ /**
18
+ * Creates the timeline axis shapes and adds them to the given view layer.
19
+ *
20
+ * @class
21
+ * @alias TimelineAxis
22
+ *
23
+ * @param {WaveformOverview|TimelineZoomView} view
24
+ * @param {Object} options
25
+ * @param {String} options.axisGridlineColor
26
+ * @param {String} options.axisLabelColor
27
+ */
28
+
29
+ function TimelineAxis(peaks, view, options) {
30
+ var self = this;
31
+
32
+ peaks.on('playhead.moved', this._onPlayheadMoved.bind(this));
33
+ peaks.on('playhead.hidden', this._onPlayheadHidden.bind(this));
34
+
35
+ self._axisGridlineColor = options.axisGridlineColor;
36
+ self._axisLabelColor = options.axisLabelColor;
37
+
38
+ self._backLayer = new Konva.Layer({
39
+ listening: false
40
+ });
41
+
42
+ self._frontLayer = new Konva.Layer({
43
+ listening: false
44
+ });
45
+
46
+ self._axisShape = new Konva.Shape({
47
+ sceneFunc: function(context) {
48
+ self.drawAxis(context, view);
49
+ }
50
+ });
51
+ self._backLayer.add(self._axisShape);
52
+
53
+ self._timesShape = new Konva.Shape({
54
+ sceneFunc: function(context) {
55
+ self.drawTimes(context, view);
56
+ }
57
+ });
58
+ self._frontLayer.add(self._timesShape);
59
+ }
60
+
61
+ TimelineAxis.prototype._onPlayheadMoved = function(playheadX, playheadWidth) {
62
+ this._maskStart = playheadX;
63
+ this._maskEnd = playheadX + playheadWidth;
64
+ this._frontLayer.draw();
65
+ };
66
+
67
+ TimelineAxis.prototype._onPlayheadHidden = function() {
68
+ this._maskStart = null;
69
+ this._maskEnd = null;
70
+ this._frontLayer.draw();
71
+ };
72
+
73
+ TimelineAxis.prototype.draw = function() {
74
+ this._backLayer.draw();
75
+ this._frontLayer.draw();
76
+ };
77
+
78
+ TimelineAxis.prototype.addBackToStage = function(stage) {
79
+ stage.add(this._backLayer);
80
+ };
81
+
82
+ TimelineAxis.prototype.addFrontToStage = function(stage) {
83
+ stage.add(this._frontLayer);
84
+ };
85
+
86
+ /**
87
+ * Returns number of seconds for each x-axis marker, appropriate for the
88
+ * current zoom level, ensuring that markers are not too close together
89
+ * and that markers are placed at intuitive time intervals (i.e., every 1,
90
+ * 2, 5, 10, 20, 30 seconds, then every 1, 2, 5, 10, 20, 30 minutes, then
91
+ * every 1, 2, 5, 10, 20, 30 hours).
92
+ *
93
+ * @param {WaveformOverview|WaveformZoomView} view
94
+ * @returns {Number}
95
+ */
96
+
97
+ TimelineAxis.prototype.getAxisLabelScale = function(view) {
98
+ var baseSecs = 1; // seconds
99
+ var steps = [0.1, 0.5, 1, 2, 5, 10, 20, 30];
100
+ var minSpacing = 60;
101
+ var index = 0;
102
+
103
+ var secs;
104
+
105
+ for (;;) {
106
+ secs = baseSecs * steps[index];
107
+ var pixels = view.timeToPixels(secs);
108
+
109
+ if (pixels < minSpacing) {
110
+ if (++index === steps.length) {
111
+ baseSecs *= 60; // seconds -> minutes -> hours
112
+ index = 0;
113
+ }
114
+ }
115
+ else {
116
+ break;
117
+ }
118
+ }
119
+
120
+ return secs;
121
+ };
122
+
123
+ /**
124
+ * Draws the time axis and labels onto a view.
125
+ *
126
+ * @param {Konva.Context} context The context to draw on.
127
+ * @param {WaveformOverview|WaveformZoomView} view
128
+ */
129
+
130
+ TimelineAxis.prototype.drawAxis = function(context, view) {
131
+ var currentFrameStartTime = view.pixelsToTime(view.getFrameOffset());
132
+
133
+ // Time interval between axis markers (seconds)
134
+ var axisLabelIntervalSecs = this.getAxisLabelScale(view);
135
+
136
+ // Time of first axis marker (seconds)
137
+ var firstAxisLabelSecs = Utils.roundUpToNearest(currentFrameStartTime, axisLabelIntervalSecs);
138
+
139
+ // Distance between waveform start time and first axis marker (seconds)
140
+ var axisLabelOffsetSecs = firstAxisLabelSecs - currentFrameStartTime;
141
+
142
+ // Distance between waveform start time and first axis marker (pixels)
143
+ var axisLabelOffsetPixels = view.timeToPixels(axisLabelOffsetSecs);
144
+
145
+ context.setAttr('strokeStyle', this._axisGridlineColor);
146
+ context.setAttr('lineWidth', 1);
147
+
148
+ // Set text style
149
+ context.setAttr('font', '11px sans-serif');
150
+ context.setAttr('fillStyle', this._axisLabelColor);
151
+ context.setAttr('textAlign', 'left');
152
+ context.setAttr('textBaseline', 'bottom');
153
+
154
+ var secs = firstAxisLabelSecs;
155
+ var x;
156
+
157
+ var width = view.getWidth();
158
+ var height = view.getHeight();
159
+
160
+ for (;;) {
161
+ // Position of axis marker (pixels)
162
+ x = axisLabelOffsetPixels + view.timeToPixels(secs - firstAxisLabelSecs);
163
+ if (x >= width) {
164
+ break;
165
+ }
166
+
167
+ context.beginPath();
168
+ context.moveTo(x + 0.5, 0);
169
+ context.lineTo(x + 0.5, height);
170
+ context.stroke();
171
+
172
+ secs += axisLabelIntervalSecs;
173
+ }
174
+ };
175
+
176
+ TimelineAxis.prototype.drawTimes = function(context, view) {
177
+ var currentFrameStartTime = view.pixelsToTime(view.getFrameOffset());
178
+
179
+ // Time interval between axis markers (seconds)
180
+ var axisLabelIntervalSecs = this.getAxisLabelScale(view);
181
+
182
+ // Time of first axis marker (seconds)
183
+ var firstAxisLabelSecs = Utils.roundUpToNearestPositive(
184
+ currentFrameStartTime - 1,
185
+ axisLabelIntervalSecs
186
+ );
187
+
188
+ // Distance between waveform start time and first axis marker (seconds)
189
+ var axisLabelOffsetSecs = firstAxisLabelSecs - currentFrameStartTime;
190
+
191
+ // Distance between waveform start time and first axis marker (pixels)
192
+ var axisLabelOffsetPixels = view.timeToPixels(axisLabelOffsetSecs);
193
+
194
+ // Set text style
195
+ context.setAttr('font', '11px sans-serif');
196
+ context.setAttr('fillStyle', this._axisLabelColor);
197
+ context.setAttr('textAlign', 'left');
198
+ context.setAttr('textBaseline', 'bottom');
199
+
200
+ var secs = firstAxisLabelSecs;
201
+ var x;
202
+
203
+ var width = view.getWidth();
204
+
205
+ for (;;) {
206
+ // Position of axis marker (pixels)
207
+ x = axisLabelOffsetPixels + view.timeToPixels(secs - firstAxisLabelSecs);
208
+
209
+ if (x >= width) {
210
+ break;
211
+ }
212
+
213
+ var label = Utils.formatTime(secs, false);
214
+ var labelHeight = context.measureText(label).actualBoundingBoxAscent
215
+ - context.measureText(label).actualBoundingBoxDescent;
216
+ var labelWidth = context.measureText(label).width;
217
+ var labelX = x + LEFT_PADDING;
218
+ var labelY = labelHeight;
219
+
220
+ if (!this._labelIsMasked(labelX, labelX + labelWidth)) {
221
+ context.fillText(label, labelX, labelY);
222
+ }
223
+
224
+ secs += axisLabelIntervalSecs;
225
+ }
226
+ };
227
+
228
+ TimelineAxis.prototype._labelIsMasked = function(labelStart, labelEnd) {
229
+ if (this._maskStart && this._maskEnd) {
230
+ if (labelEnd > this._maskStart && labelStart < this._maskEnd) {
231
+ return true;
232
+ }
233
+ }
234
+ return false;
235
+ };
236
+
237
+ return TimelineAxis;
238
+ });