Created
February 18, 2021 17:43
-
-
Save tmkasun/5df0d8ba6690354cd6a305185f9d45cd to your computer and use it in GitHub Desktop.
How to use canvas-gauges with React using Reeact hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { LinearGauge, RadialGauge } from 'canvas-gauges' | |
import React, { useEffect, useRef } from 'react' | |
export default (props) => { | |
const { value, isRadial = false } = props; | |
const gaugeDOM = useRef(); | |
const gauge = useRef(); | |
useEffect(() => { | |
const options = Object.assign({}, props, { | |
renderTo: gaugeDOM.current | |
}) | |
gauge.current = isRadial ? new RadialGauge(options).draw() : new LinearGauge(options).draw(); | |
}, []); | |
useEffect(() => { | |
gauge.current.value = value; | |
gauge.current.update(); | |
}, [value]) | |
return ( | |
<canvas | |
ref={gaugeDOM} /> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by
https://github.com/1995parham/react-canvas-gauges
Using https://canvas-gauges.com/