Improve SeekBar interface
continuous-integration/drone/push Build was killed Details
continuous-integration/drone Build was killed Details

This commit is contained in:
Rob Watson 2022-02-16 20:43:39 +01:00
parent 691099da3a
commit e40e794721
1 changed files with 35 additions and 9 deletions

View File

@ -49,8 +49,7 @@ export const SeekBar: React.FC<Props> = ({
ctx.fillRect(0, 0, canvas.width, canvas.height);
// seek bar
const pixelRatio = canvas.width / canvas.clientWidth;
const offset = offsetPixels * pixelRatio;
const offset = offsetCanvas(canvas);
const width = canvas.width - offset * 2;
ctx.fillStyle = 'black';
ctx.fillRect(offset, InnerMargin, width, canvas.height - InnerMargin * 2);
@ -68,23 +67,33 @@ export const SeekBar: React.FC<Props> = ({
// helpers
const emitPositionEvent = (evt: MouseEvent<HTMLCanvasElement>) => {
const canvas = evt.currentTarget;
const { x } = mouseEventToCanvasPoint(evt);
const emitPositionEvent = (x: number, canvas: HTMLCanvasElement) => {
const pixelRatio = canvas.width / canvas.clientWidth;
const offset = offsetPixels * pixelRatio;
const ratio = (x - offset) / (canvas.width - offset * 2);
onPositionChanged(ratio * duration);
};
const offsetCanvas = (canvas: HTMLCanvasElement): number => {
return Math.round(offsetPixels * (canvas.width / canvas.clientWidth));
};
// handlers
const handleMouseDown = (evt: MouseEvent<HTMLCanvasElement>) => {
if (mode != Mode.Normal) return;
const canvas = evt.currentTarget;
const offset = offsetCanvas(canvas);
const { x } = mouseEventToCanvasPoint(evt);
if (x < offset || x > evt.currentTarget.width - offset) {
return;
}
setMode(Mode.Dragging);
emitPositionEvent(evt);
emitPositionEvent(x, canvas);
};
const handleMouseUp = () => {
@ -94,18 +103,35 @@ export const SeekBar: React.FC<Props> = ({
};
const handleMouseMove = (evt: MouseEvent<HTMLCanvasElement>) => {
const { y } = mouseEventToCanvasPoint(evt);
const canvas = evt.currentTarget;
const offset = offsetCanvas(canvas);
const coords = mouseEventToCanvasPoint(evt);
const { y } = coords;
let { x } = coords;
// TODO: improve mouse detection around knob.
if (y > InnerMargin && y < LogicalHeight - InnerMargin) {
if (
x >= offset &&
x < canvas.width - offset &&
y > InnerMargin &&
y < LogicalHeight - InnerMargin
) {
setCursor('cursor-pointer');
} else {
setCursor('cursor-auto');
}
if (x < offset) {
x = offset;
}
if (x > canvas.width - offset) {
x = canvas.width - offset;
}
if (mode == Mode.Normal) return;
emitPositionEvent(evt);
emitPositionEvent(x, canvas);
};
const handleMouseEnter = () => {