@checksub_team/peaks_timeline 2.3.0-alpha.1 → 2.3.0-alpha.2

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.
@@ -51,64 +51,102 @@ define(['../utils', 'konva'], function(Utils, Konva) {
51
51
 
52
52
  function WaveformShape(options) {
53
53
  const shape = new Konva.Shape({
54
- fill: options.source.color
54
+ fill: options.color,
55
+ listening: false
55
56
  });
56
57
 
57
58
  Object.assign(this, shape);
58
59
 
59
- this._layer = options.layer;
60
60
  this._view = options.view;
61
- this._source = options.source;
62
61
  this._height = options.height;
63
- this._url = options.url + '-scaled';
62
+ this._waveformDataFunc = options.waveformDataFunc;
64
63
 
65
- this.sceneFunc(this._sceneFunc);
64
+ this.sceneFunc(function(context) {
65
+ var waveformPoints = this._waveformDataFunc ? this._waveformDataFunc() : null;
66
66
 
67
- this.hitFunc(this._waveformShapeHitFunc);
67
+ this._sceneFunc(context, waveformPoints);
68
+ });
68
69
  }
69
70
 
70
71
  WaveformShape.prototype = Object.create(Konva.Shape.prototype);
71
72
 
72
- WaveformShape.prototype._sceneFunc = function(context) {
73
- var width = this._view.getWidth();
74
- var waveformData = this._layer.getLoadedData(this._url).data;
73
+ WaveformShape.prototype._sceneFunc = function(context, waveformPoints) {
74
+ if (!waveformPoints) {
75
+ return;
76
+ }
75
77
 
76
- var startPixel = 0, startOffset = 0, endPixel = width, targetSpeed = 1.0;
78
+ var xPoints = [];
79
+ var minByChannel = [];
80
+ var maxByChannel = [];
81
+ var channels = 0;
77
82
 
78
- if (this._source) {
79
- targetSpeed = this._source.targetSpeed || 1.0;
83
+ this._forEachWaveformPoint(waveformPoints, function(point) {
84
+ if (!point || !point.min || !point.max) {
85
+ return;
86
+ }
80
87
 
81
- startPixel = Math.floor(
82
- (this._view.timeToPixels(this._source.mediaStartTime) + Math.max(
83
- this._view.getFrameOffset() - this._view.timeToPixels(this._source.startTime),
84
- 0
85
- )) * targetSpeed
86
- );
88
+ if (channels === 0) {
89
+ channels = point.min.length;
90
+ for (var c = 0; c < channels; c++) {
91
+ minByChannel[c] = [];
92
+ maxByChannel[c] = [];
93
+ }
94
+ }
87
95
 
88
- startOffset = this._view.timeToPixels(this._source.mediaStartTime);
89
-
90
- endPixel = Math.min(
91
- Math.ceil(
92
- (this._view.timeToPixels(this._source.mediaEndTime) - Math.max(
93
- this._view.timeToPixels(this._source.endTime)
94
- - this._view.getFrameOffset()
95
- - this._view.getWidth(),
96
- 0
97
- )) * targetSpeed
98
- ),
99
- waveformData.length
100
- );
96
+ xPoints.push(point.x);
97
+
98
+ for (var i = 0; i < channels; i++) {
99
+ minByChannel[i].push(point.min[i]);
100
+ maxByChannel[i].push(point.max[i]);
101
+ }
102
+ });
103
+
104
+ if (channels === 0 || xPoints.length === 0) {
105
+ return;
106
+ }
107
+
108
+ this._drawWaveformFromPoints(context, xPoints, minByChannel, maxByChannel, channels, this._height);
109
+ };
110
+
111
+ WaveformShape.prototype._forEachWaveformPoint = function(waveformPoints, callback) {
112
+ if (!waveformPoints) {
113
+ return;
114
+ }
115
+
116
+ // Support iterators ({ next() }) without requiring Symbol.iterator.
117
+ if (typeof waveformPoints.next === 'function') {
118
+ while (true) {
119
+ var result = waveformPoints.next();
120
+
121
+ if (!result || result.done) {
122
+ break;
123
+ }
124
+ callback(result.value);
125
+ }
126
+ return;
127
+ }
128
+
129
+ // Support ES6 iterables if available.
130
+ if (typeof Symbol !== 'undefined' && waveformPoints[Symbol.iterator]) {
131
+ var iterator = waveformPoints[Symbol.iterator]();
132
+
133
+ while (true) {
134
+ var iterResult = iterator.next();
135
+
136
+ if (iterResult.done) {
137
+ break;
138
+ }
139
+ callback(iterResult.value);
140
+ }
141
+ return;
101
142
  }
102
143
 
103
- this._drawWaveform(
104
- context,
105
- waveformData,
106
- startPixel,
107
- startOffset,
108
- endPixel,
109
- targetSpeed,
110
- this._height
111
- );
144
+ // Fallback for arrays.
145
+ if (Array.isArray(waveformPoints)) {
146
+ for (var i = 0; i < waveformPoints.length; i++) {
147
+ callback(waveformPoints[i]);
148
+ }
149
+ }
112
150
  };
113
151
 
114
152
  /**
@@ -126,10 +164,14 @@ define(['../utils', 'konva'], function(Utils, Konva) {
126
164
  * @param {Number} height The height of the waveform area, in pixels.
127
165
  */
128
166
 
129
- WaveformShape.prototype._drawWaveform = function(context, waveformData,
130
- startPixel, startOffset, endPixel, targetSpeed, height) {
131
- var channels = waveformData.channels;
132
-
167
+ WaveformShape.prototype._drawWaveformFromPoints = function(
168
+ context,
169
+ xPoints,
170
+ minByChannel,
171
+ maxByChannel,
172
+ channels,
173
+ height
174
+ ) {
133
175
  var waveformTop = 0;
134
176
  var waveformHeight = Math.floor(height / channels);
135
177
 
@@ -138,81 +180,35 @@ define(['../utils', 'konva'], function(Utils, Konva) {
138
180
  waveformHeight = height - (channels - 1) * waveformHeight;
139
181
  }
140
182
 
141
- this._drawChannel(
183
+ this._drawChannelFromPoints(
142
184
  context,
143
- waveformData.channel(i),
144
- startPixel,
145
- startOffset,
146
- endPixel,
185
+ xPoints,
186
+ minByChannel[i],
187
+ maxByChannel[i],
147
188
  waveformTop,
148
- waveformHeight,
149
- targetSpeed
189
+ waveformHeight
150
190
  );
151
191
 
152
192
  waveformTop += waveformHeight;
153
193
  }
154
194
  };
155
195
 
156
- WaveformShape.prototype._drawChannel = function(context, channel,
157
- startPixel, startOffset, endPixel, top, height, targetSpeed) {
158
- var x, val;
159
-
196
+ WaveformShape.prototype._drawChannelFromPoints = function(context, xPoints, minValues, maxValues, top, height) {
160
197
  var amplitudeScale = this._view.getAmplitudeScale();
161
198
 
162
199
  context.beginPath();
163
200
 
164
- for (x = startPixel; x < endPixel; x++) {
165
- val = channel.min_sample(x);
166
-
167
- context.lineTo(x / targetSpeed - startOffset + 0.5, top + scaleY(val, height, amplitudeScale) + 0.5);
201
+ for (var i = 0; i < xPoints.length; i++) {
202
+ context.lineTo(xPoints[i], top + scaleY(minValues[i], height, amplitudeScale) + 0.5);
168
203
  }
169
204
 
170
- for (x = endPixel - 1; x >= startPixel; x--) {
171
- val = channel.max_sample(x);
172
-
173
- context.lineTo(x / targetSpeed - startOffset + 0.5, top + scaleY(val, height, amplitudeScale) + 0.5);
205
+ for (var j = xPoints.length - 1; j >= 0; j--) {
206
+ context.lineTo(xPoints[j], top + scaleY(maxValues[j], height, amplitudeScale) + 0.5);
174
207
  }
175
208
 
176
209
  context.closePath();
177
-
178
210
  context.fillShape(this);
179
211
  };
180
212
 
181
- WaveformShape.prototype._waveformShapeHitFunc = function(context) {
182
- if (!this._source) {
183
- return;
184
- }
185
-
186
- var frameOffset = this._view.getFrameOffset();
187
- var viewWidth = this._view.getWidth();
188
-
189
- var startPixels = this._view.timeToPixels(this._source.startTime);
190
- var endPixels = this._view.timeToPixels(this._source.endTime);
191
-
192
- var offsetY = 10;
193
- var hitRectHeight = this._height;
194
-
195
- if (hitRectHeight < 0) {
196
- hitRectHeight = 0;
197
- }
198
-
199
- var hitRectLeft = startPixels - frameOffset;
200
- var hitRectWidth = endPixels - startPixels;
201
-
202
- if (hitRectLeft < 0) {
203
- hitRectWidth -= -hitRectLeft;
204
- hitRectLeft = 0;
205
- }
206
-
207
- if (hitRectLeft + hitRectWidth > viewWidth) {
208
- hitRectWidth -= hitRectLeft + hitRectWidth - viewWidth;
209
- }
210
-
211
- context.beginPath();
212
- context.rect(hitRectLeft, offsetY, hitRectWidth, hitRectHeight);
213
- context.closePath();
214
- context.fillStrokeShape(this);
215
- };
216
-
217
213
  return WaveformShape;
218
214
  });