From 9d14ff250fd681383fa1ba1d03aa0faa3b7cc3f4 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sat, 10 Jul 2021 13:13:40 +0200 Subject: [PATCH] Implement knob rotation --- Dashboard/src/Components/Knob.tsx | 65 +++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/Dashboard/src/Components/Knob.tsx b/Dashboard/src/Components/Knob.tsx index db1123d..d925433 100644 --- a/Dashboard/src/Components/Knob.tsx +++ b/Dashboard/src/Components/Knob.tsx @@ -1,6 +1,8 @@ import React, { useRef } from "react"; import styled from "styled-components"; +const SvgHeight = 180; + const KnobSVG = styled.svg` .cls-1 { fill: #545353; @@ -23,24 +25,71 @@ const KnobSVG = styled.svg` const Knob = () => { const overlayRef = useRef(null); - let i = 20; + + let isMouseDown = false; + + const move = (e: React.MouseEvent) => { + const relativePosX = e.nativeEvent.offsetX - SvgHeight / 2; + const relativePosY = e.nativeEvent.offsetY - SvgHeight / 2; + // console.log(`X:${relativePosX}|Y:${relativePosY}`); + + const angleDegree = Math.atan2(relativePosY, relativePosX) * (180 / Math.PI); + // console.log(angleDegree); + + let angle = 0; + angle = angleDegree + 85; + if (angle < 0) { + angle = 360 + angle; + } else if (angle > 360) { + angle = 360 - angle; + } + // console.log(angle); + + if (overlayRef.current !== null) { + overlayRef.current.style.transformOrigin = "50% 50%"; + overlayRef.current.style.transform = `rotate(${angle}deg)`; + } + }; + + const mouseDownHandler: React.MouseEventHandler = (e) => { + if (e.button !== 2) { + move(e); + isMouseDown = true; + } + }; + + const mouseUpHandler: React.MouseEventHandler = () => { + isMouseDown = false; + }; + + const mouseMoveHandler: React.MouseEventHandler = (e) => { + if (isMouseDown) { + move(e); + } + }; return (
{ + height={`${SvgHeight}px`} + // onClick={() => { + // if (overlayRef.current !== null) { + // overlayRef.current.style.transformOrigin = "50% 50%"; + // overlayRef.current.style.transform = `rotate(${i}deg)`; + // i += 20; + // } + // }} + onContextMenu={() => { if (overlayRef.current !== null) { overlayRef.current.style.transformOrigin = "50% 50%"; - overlayRef.current.style.transform = `rotate(${i}deg)`; - i += 20; + overlayRef.current.style.transform = `rotate(${0}deg)`; } }} - onContextMenu={() => { - alert("off"); // absolute on/off - }} + onMouseDown={mouseDownHandler} + onMouseUp={mouseUpHandler} + onMouseMove={mouseMoveHandler} >