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