JS Graphics
In this page:
What JavaScript Graphics Can Do
Beyond styling existing HTML elements, JavaScript graphics tools let you draw completely custom visuals -- charts, game sprites, data visualizations, generated patterns -- directly onto a drawing surface, pixel by pixel, under full programmatic control.
उदाहरण: What JavaScript Graphics Can Do
<canvas id="chart" width="200" height="100"></canvas>
<script>
const ctx = document.getElementById("chart").getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(10, 10, 50, 50); // pixel-level drawing, not just styled HTML
</script>
The Canvas Element as a Drawing Surface
A <canvas> element is essentially a blank rectangular area in the page with no visual content of its own -- it becomes useful only once JavaScript retrieves its drawing context and starts issuing drawing commands against it.
उदाहरण: The Canvas Element as a Drawing Surface
<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
const canvas = document.getElementById("myCanvas"); // blank until drawn on
const ctx = canvas.getContext("2d");
ctx.fillRect(20, 20, 100, 50);
</script>
Rendering Data Visually
Beyond static shapes, canvas is commonly used to render data-driven visuals -- bar charts built from an array of values, simple line graphs plotting trends over time -- computing pixel positions from the underlying data each time it changes.
उदाहरण: Rendering Data Visually
<canvas id="chart" width="200" height="100"></canvas>
<script>
// Declare the constant `data`, set to `[30, 80, 45, 60]`
const data = [30, 80, 45, 60];
// Declare the constant `ctx`, set to `document.getElementById("chart").getContext("2d")`
const ctx = document.getElementById("chart").getContext("2d");
// Call `data.forEach((value, i) => ctx.fillRect(i * 40, 100 - value, 30, value))`
data.forEach((value, i) => ctx.fillRect(i * 40, 100 - value, 30, value));
</script>
Hand-Rolled Drawing vs Using a Library
Writing raw Canvas API calls for a simple shape or a one-off visualization is reasonable, but for common chart types (bar, line, pie, scatter) with axes, labels, legends, tooltips, and animations, a library like Chart.js handles an enormous amount of complexity you would otherwise have to build yourself.
उदाहरण: Hand-Rolled Drawing vs Using a Library
<canvas id="c1" width="100" height="100"></canvas>
<script>
// Call `document.getElementById("c1").getContext("2d").fillRect(10, 10, 50, 50)`
document.getElementById("c1").getContext("2d").fillRect(10, 10, 50, 50);
</script>
<!-- For real charts with axes/legends/tooltips, use a library like Chart.js instead -->
Choosing Between Canvas and Other Graphics Approaches
Canvas is best suited for pixel-based, frequently redrawn, or animated graphics (games, live data visualizations); SVG is often a better fit for graphics that need to stay resolution-independent and individually interactive, since SVG shapes remain real, selectable DOM elements unlike canvas pixels.
उदाहरण: Choosing Between Canvas and Other Graphics Approaches
<canvas id="pixels" width="50" height="50"></canvas>
<svg width="50" height="50"><circle cx="25" cy="25" r="20" fill="green"/></svg>
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: