Skip to content

Commit

Permalink
Feature waveborder (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
zolero authored Oct 7, 2023
1 parent eb54b6d commit d6be618
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
26 changes: 24 additions & 2 deletions dist/wfplayer.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wfplayer",
"version": "2.2.7",
"version": "2.2.8",
"description": "WFPlayer.js is an audio waveform generator",
"main": "dist/wfplayer.js",
"types": "types/wfplayer.d.ts",
Expand Down Expand Up @@ -45,4 +45,4 @@
"dependencies": {
"option-validator": "^2.0.6"
}
}
}
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export default class WFPlayer extends Emitter {
waveScale: 0.8,
waveSize: 1,
pixelRatio: Math.ceil(window.devicePixelRatio),
waveBorder: false,
waveBorderWidth: 1,
waveBorderColor: "rgba(255, 255, 255, 0.1)"
};
}

Expand Down Expand Up @@ -83,6 +86,9 @@ export default class WFPlayer extends Emitter {
waveScale: 'number',
waveSize: 'number',
pixelRatio: 'number',
waveBorder: 'boolean',
waveBorderWidth: 'number',
waveBorderColor: 'string',
};
}

Expand Down
27 changes: 23 additions & 4 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ function drawWave(data) {
waveScale,
waveSize,
scrollable,
waveBorder,
waveBorderWidth,
waveBorderColor,
} = data;

const middle = height / 2;
Expand All @@ -128,13 +131,29 @@ function drawWave(data) {
}
if (stepIndex >= step && xIndex < waveWidth) {
const waveX = gridGap * padding + xIndex;

if (waveBorder) {
ctx.strokeStyle = waveBorderColor; // Set the border color
ctx.lineWidth = waveBorderWidth; // Set the border width

// Draw the border around the rectangle
ctx.strokeRect(
waveX,
(1 + min * waveScale) * middle,
waveSize,
Math.max(1, (max - min) * middle * waveScale)
);
}

// Fill the rectangle with the desired color
ctx.fillStyle = progress && cursorX >= waveX ? progressColor : waveColor;
ctx.fillRect(
waveX,
(1 + min * waveScale) * middle,
waveSize,
Math.max(1, (max - min) * middle * waveScale),
waveX + (waveBorder ? waveBorderWidth / 2 : 0), // Offset the x-coordinate to accommodate the border
(1 + min * waveScale) * middle + (waveBorder ? waveBorderWidth / 2 : 0), // Offset the y-coordinate
waveSize - (waveBorder ? waveBorderWidth : 0), // Adjust the width for the border
Math.max(1, (max - min) * middle * waveScale) - (waveBorder ? waveBorderWidth : 0) // Adjust the height for the border
);

xIndex += waveSize;
stepIndex = 0;
min = 1;
Expand Down
3 changes: 3 additions & 0 deletions types/wfplayer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type Option = {
waveScale?: number;
waveSize?: number;
pixelRatio?: number;
waveBorder?: boolean;
waveBorderWidth?: number;
waveBorderColor?: string;
};

type Config = {
Expand Down

0 comments on commit d6be618

Please sign in to comment.