Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: added rounded bars #183

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/waveform-playlist/js/bars.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ playlist = WaveformPlaylist.init({
},
barWidth: 3,
barGap: 1,
rounded: true,
});

playlist.load([
Expand Down
6 changes: 6 additions & 0 deletions src/Playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ export default class {
this.barGap = width;
}

setRounded(rounded) {
this.rounded = rounded;
}

setAnnotations(config) {
const controlWidth = this.controls.show ? this.controls.width : 0;
this.annotationList = new AnnotationList(
Expand Down Expand Up @@ -931,6 +935,7 @@ export default class {
colors: this.colors,
barWidth: this.barWidth,
barGap: this.barGap,
rounded: this.rounded,
};

return _defaults({}, data, defaults);
Expand Down Expand Up @@ -977,6 +982,7 @@ export default class {
height: collapsed ? this.collapsedWaveHeight : this.waveHeight,
barGap: this.barGap,
barWidth: this.barWidth,
rounded: this.rounded,
})
);
});
Expand Down
3 changes: 2 additions & 1 deletion src/Track.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,8 @@ export default class {
scale,
data.height,
data.barWidth,
data.barGap
data.barGap,
data.rounded
),
})
);
Expand Down
2 changes: 2 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function init(options = {}, ee = EventEmitter()) {
collapsedWaveHeight: 30,
barWidth: 1,
barGap: 0,
rounded: false,
state: "cursor",
zoomLevels: [512, 1024, 2048, 4096],
annotationList: {
Expand Down Expand Up @@ -85,6 +86,7 @@ export function init(options = {}, ee = EventEmitter()) {
playlist.setAnnotations(config.annotationList);
playlist.setBarGap(config.barGap);
playlist.setBarWidth(config.barWidth);
playlist.setRounded(config.rounded);
playlist.isAutomaticScroll = config.isAutomaticScroll;
playlist.isContinuousPlay = config.isContinuousPlay;
playlist.linkedEndpoints = config.linkedEndpoints;
Expand Down
38 changes: 35 additions & 3 deletions src/render/CanvasHook.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { drawCorner } from "../utils/drawCorner";
/*
* virtual-dom hook for drawing to the canvas element.
*/
class CanvasHook {
constructor(peaks, offset, bits, color, scale, height, barWidth, barGap) {
constructor(
peaks,
offset,
bits,
color,
scale,
height,
barWidth,
barGap,
rounded
) {
this.peaks = peaks;
// http://stackoverflow.com/questions/6081483/maximum-size-of-a-canvas-element
this.offset = offset;
Expand All @@ -12,9 +23,10 @@ class CanvasHook {
this.height = height;
this.barWidth = barWidth;
this.barGap = barGap;
this.rounded = rounded;
}

static drawFrame(cc, h2, x, minPeak, maxPeak, width, gap) {
static drawFrame(cc, h2, x, minPeak, maxPeak, width, gap, rounded) {
const min = Math.abs(minPeak * h2);
const max = Math.abs(maxPeak * h2);

Expand All @@ -26,6 +38,16 @@ class CanvasHook {
if (gap !== 0) {
cc.fillRect(x + width, 0, gap, h2 * 2);
}
if (rounded) {
//Draw Top-Left radius
drawCorner(cc, x, h2 - max, x + width, h2 - max - 0.1, width);
//Draw Top-Right radius
drawCorner(cc, x + width, h2 - max, x, h2 - max - 0.1, width);
//Draw Bottom-Left radius
drawCorner(cc, x, h2 + min, x + width, h2 + min + 0.1, width);
//Draw Bottom-Right radius
drawCorner(cc, x + width, h2 + min, x, h2 + min + 0.1, width);
}
}

hook(canvas, prop, prev) {
Expand All @@ -46,6 +68,7 @@ class CanvasHook {
const maxValue = 2 ** (this.bits - 1);
const width = this.barWidth;
const gap = this.barGap;
const rounded = this.rounded;
const barStart = width + gap;

cc.clearRect(0, 0, canvas.width, canvas.height);
Expand All @@ -57,7 +80,16 @@ class CanvasHook {
for (let pixel = 0; pixel < len; pixel += barStart) {
const minPeak = this.peaks[(pixel + this.offset) * 2] / maxValue;
const maxPeak = this.peaks[(pixel + this.offset) * 2 + 1] / maxValue;
CanvasHook.drawFrame(cc, h2, pixel, minPeak, maxPeak, width, gap);
CanvasHook.drawFrame(
cc,
h2,
pixel,
minPeak,
maxPeak,
width,
gap,
rounded
);
}

cc.restore();
Expand Down
8 changes: 8 additions & 0 deletions src/utils/drawCorner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const drawCorner = (cc, x1, y1, x2, y2, width) => {
cc.beginPath();
cc.moveTo(x1, y1);
cc.arcTo(x1, y2, x2, y1, width / 2);
cc.lineTo(x1, y1);
cc.closePath();
cc.fill();
};