Dragging Elements
Right now, Markes
and Labels
can be dragged around the screen when enabled.
Both components accept a position
and onChange
callback prop.
We also provide a useCoordState
hook, that's just a wrapper on useState with a few convienience features.
It automatically normalizes's any Vec2ish
value passed as initial prop and types it accordingly.
You could, however, just use useState
and pass a Vec2
directly if you prefer.
With useCoordState
- tsx
const [point, setPoint] = useCoordState(Vec2.of(0, 0)); // â Vec2(0, 0) <Marker position={point} onChange={setPoint} label={"đĨ"} />; const [point, setPoint] = useCoordState([0, 0]); // â Vec2(0, 0) <Marker position={point} onChange={setPoint} label={"đĨ"} />; const [point, setPoint] = useCoordState({ x: 0, y: 0 }); // â Vec2(0, 0) <Marker position={point} onChange={setPoint} label={"đĨ"} />;
With useState
- tsx
const [point, setPoint] = useState(Vec2.of(0, 0)); // â Vec2(0, 0) <Marker position={point} onChange={setPoint} label={"đĨ"} />; const [point, setPoint] = useState<[number, number]>([0, 0]); // â [0, 0] <Marker position={point} onChange={setPoint} label={"đĨ"} />; const [point, setPoint] = useState({ x: 0, y: 0 }); // â { x: 0, y: 0 } <Marker position={point} onChange={setPoint} label={"đĨ"} />;
Note that even in the â cases, the Marker will still probably work, but the point
state will change to a Vec2 after the first onChange
callback
So if you're using Typescript, it will complain about the type mismatch.
You could solve make it type safe again by doing something like this. (is it worth it though? đ¤)
- tsx
const [point, setPoint] = useState<Vec2ish>([0, 0]); // â [0, 0] <Marker position={point} onChange={setPoint} label={"đĨ"} />; // after onChange is triggered point; // â Vec2(0, 0)
Or this
- tsx
const [point, setPoint] = useState<[number, number]>([0, 0]); // â [0, 0] <Marker position={point} onChange={({ x, y }) => [x, y]} label={"đĨ"} />; // after onChange is triggered point; // â [0, 0]
Example
For finer control, you can also leverage other HTML native events, such as onMouse*
, onPointer*
, etc.