Skip to content

Commit

Permalink
feat: loading for canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
firejune committed Nov 7, 2023
1 parent e2f8ea2 commit f50fac7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
Binary file removed public/canvas-loading-dark.png
Binary file not shown.
Binary file removed public/canvas-loading-light.png
Binary file not shown.
13 changes: 8 additions & 5 deletions src/app/[username]/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const defaultBoxMargin = 3
const defaultBorderRadius = 2
const scaleFactor = 3

const empty =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='

const Canvas = ({
data,
username,
Expand All @@ -22,16 +25,15 @@ const Canvas = ({
options: { boxMargin = defaultBoxMargin, showWeekDays = true, showFooter = true, ...options } = {},
}: ChartProps) => {
const canvasRef = useRef<HTMLCanvasElement>(null)
const imgRef = useRef<HTMLImageElement>(null)
const [url, setUrl] = useState(`/canvas-loading-${scheme}.png`)
const [url, setUrl] = useState(empty)
const [scale, setScale] = useState(1)

const textWidth = showWeekDays ? 28 + boxMargin : 0
const footerHeight = showFooter ? textHeight : 0
const graphHeight = footerHeight + (boxSize + boxMargin) * 8 + canvasMargin
const height = graphHeight + canvasMargin + 5
const width = data.length * (boxSize + boxMargin) + canvasMargin + textWidth
const handleResize = () => imgRef.current && setScale(imgRef.current.offsetWidth / width)
const handleResize = () => canvasRef.current && setScale(canvasRef.current.offsetWidth / width)

useEffect(() => {
if (canvasRef.current) {
Expand All @@ -58,12 +60,13 @@ const Canvas = ({

return (
<>
<img useMap="#info" width={width} height={height} src={url} alt="" ref={imgRef} />
<img useMap="#info" width={width} height={height} src={url} alt="" />

<map name="info">
{data.map((week, x) => (
<Fragment key={x}>
{week.map((day, y) => {
const left = canvasMargin + (boxSize + boxMargin) * x
const left = canvasMargin + textWidth + (boxSize + boxMargin) * x
const top = canvasMargin + textHeight + (boxSize + boxMargin) * y
const starts = [left * scale, top * scale]
const ends = [(left + boxSize) * scale, (top + boxSize) * scale]
Expand Down
4 changes: 1 addition & 3 deletions src/app/[username]/loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import differenceInCalendarWeeks from 'date-fns/differenceInCalendarWeeks'
const FALSY = ['0', 'false', 'hide', 'hidden', 'none']
const DATE_FORMAT = 'yyyy-MM-dd'
import Chart, { Contrib } from './Chart'
import Canvas from './Canvas'

export default function ChartPage() {
const params = useParams()
Expand Down Expand Up @@ -43,9 +42,8 @@ export default function ChartPage() {
...(searchParams.get('margin') ? { boxMargin: Number(searchParams.get('margin')) } : {}),
}

const Element = params.element === 'canvas' ? Canvas : Chart
return (
<Element
<Chart
data={graphEntries}
username={params.username as string}
scheme={searchParams.get('scheme') as 'light' | 'dark'}
Expand Down

0 comments on commit f50fac7

Please sign in to comment.