/
01-getting-started.tsx
63 lines (49 loc) · 1.57 KB
/
01-getting-started.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import NavBarComponent from '@/components/navbar-component';
import Head from 'next/head'
import {useEffect, useRef} from "react";
export default function Example01() {
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) {
console.log('canvas is missing!');
return;
}
const context = canvas.getContext('2d');
if (!context) {
console.log('context is missing!');
return;
}
canvas.width = (window.innerWidth * 0.5);
canvas.height = (window.innerHeight * 0.5);
const {width, height} = canvas;
let radius = 50;
let dr = 1;
const animate = () => {
context.clearRect(0, 0, width, height);
if (radius >= 50 || radius <= 20) {
dr *= -1;
}
radius += dr;
context.beginPath();
context.fillStyle = '#333';
context.arc(width / 2, height / 2, radius, 0, 2 * Math.PI);
context.fill();
requestAnimationFrame(animate);
};
animate();
}, []);
return (
<>
<Head>
<title>Canvas animation - Next.js</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
</Head>
<NavBarComponent/>
<div>
<h1>01 Getting Started</h1>
<canvas ref={canvasRef} className='border'></canvas>
</div>
</>
)
}