Skip to content

Commit

Permalink
Merge pull request #14 from camiha/develop
Browse files Browse the repository at this point in the history
feat: add border API
  • Loading branch information
camiha authored Dec 23, 2023
2 parents e4c5076 + 6fdd81c commit 8e27ddb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ If you want to set styling globally, please refer to the StyleOption section. (I
##### PieStyle
| Property | Type | Description |
|-----------------|---------------------|---------------------------------------------------|
| `border` | `boolean` | if set `true`, set border for each pie segment |
| `borderColor` | `string` | |
| `borderWidth` | `number` | |
| `theme` | `PieTheme[]` | Array of theme options for the pie segments. |

##### LabelStyle
Expand Down Expand Up @@ -146,6 +149,9 @@ const option = {
defaultColor: "#000",
},
pie: {
border: false,
borderColor: '#000',
borderWidth: 2,
theme: [
{
bg: "#e0e7ff",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-hook-roulette",
"version": "0.0.7",
"version": "0.0.8",
"description": "Minimal roulette wheel component. built with React Hooks.",
"homepage": "https://github.com/camiha/React-Hook-Roulette",
"type": "module",
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export const defaultOptions: DefaultRouletteOptions = {
defaultColor: "#000",
},
pie: {
border: false,
borderColor: "#000",
borderWidth: 2,
theme: [
{
bg: "#e0e7ff",
Expand Down
48 changes: 48 additions & 0 deletions src/functions/draw-canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const drawPie = ({
pie,
segmentStartAngle,
anglePerSegment,
style,
}: DrawPie): void => {
const startDeg = degreesToCanvasRadians(segmentStartAngle);
const endDeg = degreesToCanvasRadians(segmentStartAngle + anglePerSegment);
Expand All @@ -114,6 +115,29 @@ export const drawPie = ({
true,
);
context.fill();

if (
style?.pie?.border == null ||
style?.pie?.borderWidth == null ||
style?.pie?.borderColor == null
) {
return;
}
// has border
context.beginPath();
context.strokeStyle = style.pie.borderColor;
context.lineWidth = style.pie.borderWidth;
context.moveTo(geometry.center.x, geometry.center.y);
context.lineTo(
geometry.center.x + geometry.radius * Math.cos(startDeg),
geometry.center.y + geometry.radius * Math.sin(startDeg),
);
context.moveTo(geometry.center.x, geometry.center.y);
context.lineTo(
geometry.center.x + geometry.radius * Math.cos(endDeg),
geometry.center.y + geometry.radius * Math.sin(endDeg),
);
context.stroke();
};

export interface DrawRoulette {
Expand Down Expand Up @@ -181,6 +205,30 @@ export const drawRoulette = ({
});
segmentEndAngle = segmentStartAngle;
}

// has border

if (
style?.pie?.border == null ||
style?.pie?.borderWidth == null ||
style?.pie?.borderColor == null
) {
return;
}

const lineWidth = style.pie.borderWidth;
context.beginPath();
context.strokeStyle = style.pie.borderColor;
context.lineWidth = lineWidth;
context.arc(
geometry.center.x,
geometry.center.y,
geometry.radius - lineWidth / 2,
0,
Math.PI * 2,
true,
);
context.stroke();
};

interface DrawCanvas {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/use-roulette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const useRoulette = ({
...options.style?.arrow,
},
pie: {
...defaultOptions.style.pie,
theme: [
...(options.style?.pie?.theme || defaultOptions.style.pie.theme),
],
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ interface PieTheme {
}

interface PieStyle {
border: boolean;
borderColor: string;
borderWidth: number;
theme: PieTheme[];
}

Expand Down

0 comments on commit 8e27ddb

Please sign in to comment.