-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas_drawing_graphics.js
More file actions
62 lines (57 loc) · 2.13 KB
/
canvas_drawing_graphics.js
File metadata and controls
62 lines (57 loc) · 2.13 KB
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
// Setting up the <canvas> Element:
// First, you need to create a <canvas> element in your HTML file with an id attribute to reference it from JavaScript:
<canvas id="myCanvas" width="400" height="200"></canvas>
// Accessing the Canvas Element in JavaScript:
// You can access the <canvas> element using its id attribute and the getContext() method, which returns a drawing context:
let canvas = document.getElementById('myCanvas');
let ctx = canvas.getContext('2d');
// Drawing Shapes and Graphics:
// Once you have the canvas context (ctx), you can use its methods to draw various shapes, paths, and text.
// 1. Drawing Rectangles:
// Draw a filled rectangle
ctx.fillStyle = 'red';
ctx.fillRect(50, 50, 100, 50); // (x, y, width, height)
// Draw a stroked rectangle
ctx.strokeStyle = 'blue';
ctx.lineWidth = 2;
ctx.strokeRect(150, 50, 100, 50); // (x, y, width, height)
// 2. Drawing Paths (Lines and Shapes):
// Start a new path
ctx.beginPath();
ctx.moveTo(250, 50); // Move to (x, y)
ctx.lineTo(300, 100); // Draw a line to (x, y)
ctx.lineTo(200, 100); // Draw another line to (x, y)
ctx.closePath(); // Close the path to create a shape
// Fill the shape
ctx.fillStyle = 'green';
ctx.fill();
// Stroke the path (optional)
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
ctx.stroke();
// 3. Drawing Circles and Arcs:
// Draw a filled circle
ctx.beginPath();
ctx.arc(100, 150, 30, 0, Math.PI * 2); // (x, y, radius, startAngle, endAngle)
ctx.fillStyle = 'orange';
ctx.fill();
// Draw a stroked arc
ctx.beginPath();
ctx.arc(200, 150, 30, 0, Math.PI * 1.5); // (x, y, radius, startAngle, endAngle)
ctx.strokeStyle = 'purple';
ctx.lineWidth = 3;
ctx.stroke();
// 4. Drawing Text:
// Draw filled text
ctx.font = '24px Arial';
ctx.fillStyle = 'black';
ctx.fillText('Hello, Canvas!', 50, 180); // (text, x, y)
// Draw stroked text
ctx.font = 'bold 30px Times New Roman';
ctx.strokeStyle = 'blue';
ctx.lineWidth = 2;
ctx.strokeText('Stroked Text', 200, 180); // (text, x, y)
// 5. Clearing the Canvas:
// You can clear the entire canvas or a specific region using clearRect() method:
// Clear the entire canvas
ctx.clearRect(0, 0, canvas.width, canvas.height); // (x, y, width, height)