@checksub_team/peaks_timeline 1.4.34 → 1.4.37

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,216 +1,216 @@
1
- /**
2
- * @file
3
- *
4
- * Defines the {@link WaveformShape} class.
5
- *
6
- * @module waveform-shape
7
- */
8
-
9
- define(['./utils', 'konva'], function(Utils, Konva) {
10
- 'use strict';
11
-
12
- /**
13
- * Scales the waveform data for drawing on a canvas context.
14
- *
15
- * @param {Number} amplitude The waveform data point amplitude.
16
- * @param {Number} height The height of the waveform, in pixels.
17
- * @param {Number} scale Amplitude scaling factor.
18
- * @returns {Number} The scaled waveform data point.
19
- */
20
-
21
- function scaleY(amplitude, height, scale) {
22
- var range = 256;
23
- var offset = 128;
24
-
25
- var scaledAmplitude = (amplitude * scale + offset) * height / range;
26
-
27
- return height - Utils.clamp(height - scaledAmplitude, 0, height);
28
- }
29
-
30
- /**
31
- * Waveform shape options.
32
- *
33
- * @typedef {Object} WaveformShapeOptions
34
- * @global
35
- * @property {String} color Waveform color.
36
- * @property {WaveformOverview|WaveformZoomView} view The view object
37
- * that contains the waveform shape.
38
- * @property {Segment?} segment If given, render a waveform image
39
- * covering the segment's time range. Otherwise, render the entire
40
- * waveform duration.
41
- */
42
-
43
- /**
44
- * Creates a Konva.Shape object that renders a waveform image.
45
- *
46
- * @class
47
- * @alias WaveformShape
48
- *
49
- * @param {WaveformShapeOptions} options
50
- */
51
-
52
- function WaveformShape(options) {
53
- Konva.Shape.call(this, {
54
- fill: options.color
55
- });
56
-
57
- this._layer = options.layer;
58
- this._view = options.view;
59
- this._source = options.source;
60
- this._height = options.height;
61
- this._url = options.url + '-scaled';
62
-
63
- this.sceneFunc(this._sceneFunc);
64
-
65
- this.hitFunc(this._waveformShapeHitFunc);
66
- }
67
-
68
- WaveformShape.prototype = Object.create(Konva.Shape.prototype);
69
-
70
- WaveformShape.prototype.setWaveformColor = function(color) {
71
- this.fill(color);
72
- };
73
-
74
- WaveformShape.prototype._sceneFunc = function(context) {
75
- var width = this._view.getWidth();
76
- var waveformData = this._layer.getLoadedData(this._url).data;
77
-
78
- var startPixels = 0, startOffset = 0;
79
-
80
- if (this._source) {
81
- startPixels = this._view.timeToPixels(this._source.mediaStartTime) + Math.max(
82
- this._view.getFrameOffset() - this._view.timeToPixels(this._source.startTime),
83
- 0
84
- );
85
-
86
- startOffset = this._view.timeToPixels(this._source.mediaStartTime);
87
- }
88
-
89
- var endPixels = width;
90
-
91
- if (this._source) {
92
- endPixels = Math.min(
93
- this._view.timeToPixels(this._source.mediaEndTime) - Math.max(
94
- this._view.timeToPixels(this._source.endTime)
95
- - this._view.getFrameOffset()
96
- - this._view.getWidth(),
97
- 0
98
- ),
99
- waveformData.length
100
- );
101
- }
102
-
103
- this._drawWaveform(
104
- context,
105
- waveformData,
106
- startPixels,
107
- startOffset,
108
- endPixels,
109
- this._height
110
- );
111
- };
112
-
113
- /**
114
- * Draws a waveform on a canvas context.
115
- *
116
- * @param {Konva.Context} context The canvas context to draw on.
117
- * @param {WaveformData} waveformData The waveform data to draw.
118
- * @param {Number} frameOffset The start position of the waveform shown
119
- * in the view, in pixels.
120
- * @param {Number} startPixels The start position of the waveform to draw,
121
- * in pixels.
122
- * @param {Number} endPixels The end position of the waveform to draw,
123
- * in pixels.
124
- * @param {Number} width The width of the waveform area, in pixels.
125
- * @param {Number} height The height of the waveform area, in pixels.
126
- */
127
-
128
- WaveformShape.prototype._drawWaveform = function(context, waveformData,
129
- startPixels, startOffset, endPixels, height) {
130
- var channels = waveformData.channels;
131
-
132
- var waveformTop = 0;
133
- var waveformHeight = Math.floor(height / channels);
134
-
135
- for (var i = 0; i < channels; i++) {
136
- if (i === channels - 1) {
137
- waveformHeight = height - (channels - 1) * waveformHeight;
138
- }
139
-
140
- this._drawChannel(
141
- context,
142
- waveformData.channel(i),
143
- startPixels,
144
- startOffset,
145
- endPixels,
146
- waveformTop,
147
- waveformHeight
148
- );
149
-
150
- waveformTop += waveformHeight;
151
- }
152
- };
153
-
154
- WaveformShape.prototype._drawChannel = function(context, channel,
155
- startPixels, startOffset, endPixels, top, height) {
156
- var x, val;
157
-
158
- var amplitudeScale = this._view.getAmplitudeScale();
159
-
160
- context.beginPath();
161
-
162
- for (x = startPixels; x < endPixels; x++) {
163
- val = channel.min_sample(x);
164
-
165
- context.lineTo(x - startOffset + 0.5, top + scaleY(val, height, amplitudeScale) + 0.5);
166
- }
167
-
168
- for (x = endPixels - 1; x >= startPixels; x--) {
169
- val = channel.max_sample(x);
170
-
171
- context.lineTo(x - startOffset + 0.5, top + scaleY(val, height, amplitudeScale) + 0.5);
172
- }
173
-
174
- context.closePath();
175
-
176
- context.fillShape(this);
177
- };
178
-
179
- WaveformShape.prototype._waveformShapeHitFunc = function(context) {
180
- if (!this._source) {
181
- return;
182
- }
183
-
184
- var frameOffset = this._view.getFrameOffset();
185
- var viewWidth = this._view.getWidth();
186
-
187
- var startPixels = this._view.timeToPixels(this._source.startTime);
188
- var endPixels = this._view.timeToPixels(this._source.endTime);
189
-
190
- var offsetY = 10;
191
- var hitRectHeight = this._height;
192
-
193
- if (hitRectHeight < 0) {
194
- hitRectHeight = 0;
195
- }
196
-
197
- var hitRectLeft = startPixels - frameOffset;
198
- var hitRectWidth = endPixels - startPixels;
199
-
200
- if (hitRectLeft < 0) {
201
- hitRectWidth -= -hitRectLeft;
202
- hitRectLeft = 0;
203
- }
204
-
205
- if (hitRectLeft + hitRectWidth > viewWidth) {
206
- hitRectWidth -= hitRectLeft + hitRectWidth - viewWidth;
207
- }
208
-
209
- context.beginPath();
210
- context.rect(hitRectLeft, offsetY, hitRectWidth, hitRectHeight);
211
- context.closePath();
212
- context.fillStrokeShape(this);
213
- };
214
-
215
- return WaveformShape;
216
- });
1
+ /**
2
+ * @file
3
+ *
4
+ * Defines the {@link WaveformShape} class.
5
+ *
6
+ * @module waveform-shape
7
+ */
8
+
9
+ define(['./utils', 'konva'], function(Utils, Konva) {
10
+ 'use strict';
11
+
12
+ /**
13
+ * Scales the waveform data for drawing on a canvas context.
14
+ *
15
+ * @param {Number} amplitude The waveform data point amplitude.
16
+ * @param {Number} height The height of the waveform, in pixels.
17
+ * @param {Number} scale Amplitude scaling factor.
18
+ * @returns {Number} The scaled waveform data point.
19
+ */
20
+
21
+ function scaleY(amplitude, height, scale) {
22
+ var range = 256;
23
+ var offset = 128;
24
+
25
+ var scaledAmplitude = (amplitude * scale + offset) * height / range;
26
+
27
+ return height - Utils.clamp(height - scaledAmplitude, 0, height);
28
+ }
29
+
30
+ /**
31
+ * Waveform shape options.
32
+ *
33
+ * @typedef {Object} WaveformShapeOptions
34
+ * @global
35
+ * @property {String} color Waveform color.
36
+ * @property {WaveformOverview|WaveformZoomView} view The view object
37
+ * that contains the waveform shape.
38
+ * @property {Segment?} segment If given, render a waveform image
39
+ * covering the segment's time range. Otherwise, render the entire
40
+ * waveform duration.
41
+ */
42
+
43
+ /**
44
+ * Creates a Konva.Shape object that renders a waveform image.
45
+ *
46
+ * @class
47
+ * @alias WaveformShape
48
+ *
49
+ * @param {WaveformShapeOptions} options
50
+ */
51
+
52
+ function WaveformShape(options) {
53
+ Konva.Shape.call(this, {
54
+ fill: options.color
55
+ });
56
+
57
+ this._layer = options.layer;
58
+ this._view = options.view;
59
+ this._source = options.source;
60
+ this._height = options.height;
61
+ this._url = options.url + '-scaled';
62
+
63
+ this.sceneFunc(this._sceneFunc);
64
+
65
+ this.hitFunc(this._waveformShapeHitFunc);
66
+ }
67
+
68
+ WaveformShape.prototype = Object.create(Konva.Shape.prototype);
69
+
70
+ WaveformShape.prototype.setWaveformColor = function(color) {
71
+ this.fill(color);
72
+ };
73
+
74
+ WaveformShape.prototype._sceneFunc = function(context) {
75
+ var width = this._view.getWidth();
76
+ var waveformData = this._layer.getLoadedData(this._url).data;
77
+
78
+ var startPixels = 0, startOffset = 0;
79
+
80
+ if (this._source) {
81
+ startPixels = this._view.timeToPixels(this._source.mediaStartTime) + Math.max(
82
+ this._view.getFrameOffset() - this._view.timeToPixels(this._source.startTime),
83
+ 0
84
+ );
85
+
86
+ startOffset = this._view.timeToPixels(this._source.mediaStartTime);
87
+ }
88
+
89
+ var endPixels = width;
90
+
91
+ if (this._source) {
92
+ endPixels = Math.min(
93
+ this._view.timeToPixels(this._source.mediaEndTime) - Math.max(
94
+ this._view.timeToPixels(this._source.endTime)
95
+ - this._view.getFrameOffset()
96
+ - this._view.getWidth(),
97
+ 0
98
+ ),
99
+ waveformData.length
100
+ );
101
+ }
102
+
103
+ this._drawWaveform(
104
+ context,
105
+ waveformData,
106
+ startPixels,
107
+ startOffset,
108
+ endPixels,
109
+ this._height
110
+ );
111
+ };
112
+
113
+ /**
114
+ * Draws a waveform on a canvas context.
115
+ *
116
+ * @param {Konva.Context} context The canvas context to draw on.
117
+ * @param {WaveformData} waveformData The waveform data to draw.
118
+ * @param {Number} frameOffset The start position of the waveform shown
119
+ * in the view, in pixels.
120
+ * @param {Number} startPixels The start position of the waveform to draw,
121
+ * in pixels.
122
+ * @param {Number} endPixels The end position of the waveform to draw,
123
+ * in pixels.
124
+ * @param {Number} width The width of the waveform area, in pixels.
125
+ * @param {Number} height The height of the waveform area, in pixels.
126
+ */
127
+
128
+ WaveformShape.prototype._drawWaveform = function(context, waveformData,
129
+ startPixels, startOffset, endPixels, height) {
130
+ var channels = waveformData.channels;
131
+
132
+ var waveformTop = 0;
133
+ var waveformHeight = Math.floor(height / channels);
134
+
135
+ for (var i = 0; i < channels; i++) {
136
+ if (i === channels - 1) {
137
+ waveformHeight = height - (channels - 1) * waveformHeight;
138
+ }
139
+
140
+ this._drawChannel(
141
+ context,
142
+ waveformData.channel(i),
143
+ startPixels,
144
+ startOffset,
145
+ endPixels,
146
+ waveformTop,
147
+ waveformHeight
148
+ );
149
+
150
+ waveformTop += waveformHeight;
151
+ }
152
+ };
153
+
154
+ WaveformShape.prototype._drawChannel = function(context, channel,
155
+ startPixels, startOffset, endPixels, top, height) {
156
+ var x, val;
157
+
158
+ var amplitudeScale = this._view.getAmplitudeScale();
159
+
160
+ context.beginPath();
161
+
162
+ for (x = startPixels; x < endPixels; x++) {
163
+ val = channel.min_sample(x);
164
+
165
+ context.lineTo(x - startOffset + 0.5, top + scaleY(val, height, amplitudeScale) + 0.5);
166
+ }
167
+
168
+ for (x = endPixels - 1; x >= startPixels; x--) {
169
+ val = channel.max_sample(x);
170
+
171
+ context.lineTo(x - startOffset + 0.5, top + scaleY(val, height, amplitudeScale) + 0.5);
172
+ }
173
+
174
+ context.closePath();
175
+
176
+ context.fillShape(this);
177
+ };
178
+
179
+ WaveformShape.prototype._waveformShapeHitFunc = function(context) {
180
+ if (!this._source) {
181
+ return;
182
+ }
183
+
184
+ var frameOffset = this._view.getFrameOffset();
185
+ var viewWidth = this._view.getWidth();
186
+
187
+ var startPixels = this._view.timeToPixels(this._source.startTime);
188
+ var endPixels = this._view.timeToPixels(this._source.endTime);
189
+
190
+ var offsetY = 10;
191
+ var hitRectHeight = this._height;
192
+
193
+ if (hitRectHeight < 0) {
194
+ hitRectHeight = 0;
195
+ }
196
+
197
+ var hitRectLeft = startPixels - frameOffset;
198
+ var hitRectWidth = endPixels - startPixels;
199
+
200
+ if (hitRectLeft < 0) {
201
+ hitRectWidth -= -hitRectLeft;
202
+ hitRectLeft = 0;
203
+ }
204
+
205
+ if (hitRectLeft + hitRectWidth > viewWidth) {
206
+ hitRectWidth -= hitRectLeft + hitRectWidth - viewWidth;
207
+ }
208
+
209
+ context.beginPath();
210
+ context.rect(hitRectLeft, offsetY, hitRectWidth, hitRectHeight);
211
+ context.closePath();
212
+ context.fillStrokeShape(this);
213
+ };
214
+
215
+ return WaveformShape;
216
+ });