Skip to content

Commit

Permalink
fix solving mode color display
Browse files Browse the repository at this point in the history
  • Loading branch information
ayan4m1 committed Jan 24, 2024
1 parent 90c2f4f commit 983791d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/components/coloree/colorPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { Fragment, useRef, useEffect } from 'react';
const diameter = 400;
const radius = diameter / 2;

export default function ColorPicker({ colors, displayColor, solving }) {
export default function ColorPicker({
colors,
colorChoices,
displayColor,
solving
}) {
const canvasRef = useRef();

useEffect(() => {
Expand All @@ -21,25 +26,30 @@ export default function ColorPicker({ colors, displayColor, solving }) {

let currentAngle = Math.PI / 2;

for (const { color, pct } of colors) {
const renderColors = solving
? colors.map(({ pct }, index) => ({
color: colorChoices[index] ?? '#666',
pct
}))
: colors;

for (const { color, pct } of renderColors) {
const angle = pct * Math.PI;

if (!solving) {
const path = new Path2D();
const path = new Path2D();

path.moveTo(width - 5, height / 2);
path.arc(
width - 5,
height / 2,
radius,
currentAngle,
currentAngle + angle
);
path.closePath();
path.moveTo(width - 5, height / 2);
path.arc(
width - 5,
height / 2,
radius,
currentAngle,
currentAngle + angle
);
path.closePath();

ctx.fillStyle = color;
ctx.fill(path);
}
ctx.fillStyle = color;
ctx.fill(path);

currentAngle += angle;

Expand All @@ -58,7 +68,7 @@ export default function ColorPicker({ colors, displayColor, solving }) {
ctx.resetTransform();
}
}
}, [colors]);
}, [colors, colorChoices, solving]);

return (
<Fragment>
Expand Down Expand Up @@ -88,6 +98,7 @@ export default function ColorPicker({ colors, displayColor, solving }) {

ColorPicker.propTypes = {
colors: PropTypes.arrayOf(PropTypes.object).isRequired,
colorChoices: PropTypes.arrayOf(PropTypes.string),
displayColor: PropTypes.string.isRequired,
solving: PropTypes.bool.isRequired
};
1 change: 1 addition & 0 deletions src/components/coloree/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default function ColoreeGame() {
<Col xs={6} className="d-flex justify-content-center">
<ColorPicker
colors={colors}
colorChoices={colorChoices}
displayColor={finalColor}
solving={solving}
/>
Expand Down

0 comments on commit 983791d

Please sign in to comment.