Graphics
Graphics in HTML
HTML provides various elements for displaying graphics, including images, vector drawings , and interactive visuals. The primary elements used for graphics in HTML are <canvas>, <svg>, and <img>.
The <canvas> Element
- Used to draw graphics dynamically with JavaScript.
- Supports rendering of shapes, animations, and charts.
script element is dangerous and is disabled by default. Consider implementing your own ProseScript element to have control over script rendering. <canvas id="myCanvas" width="200" height="100" style="border:1px solid red;"></canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(20, 20, 100, 50);
</script>
The <svg> Element
- Represents Scalable Vector Graphics
SVG. - Defines resolution-independent images that maintain quality at any size.
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
SVG follows XML standards, keep these guidelines in mind:- Every element must have a closing tag.
SVGis case-sensitive; lowercase is recommended for consistency.- All attribute values, including numbers, should be enclosed in quotes.
Understanding SVG Code
- The
svgelement acts as the container for the SVG graphic. - The
widthandheightattributes define the SVG's dimensions. - Since SVG is XML-based, the
xmlnsattribute ensures proper namespace binding. - The
<circle>element is used to create a circular shape. - The
cxandcyattributes determine the center of the circle. If unspecified, the default is (0,0). - The
rattribute sets the radius of the circle. - The
strokeandstroke-widthattributes define the outline color and thickness (e.g., green with a 4px border). - The
fillattribute specifies the circle’s interior color (yellow in this case). - Always close the
svgtag properly.
Additional SVG Example
<h1>My second SVG</h1>
<svg width="150" height="100" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="green" />
<circle cx="75" cy="50" r="40" fill="yellow" />
<text x="75" y="60" font-size="30" text-anchor="middle" fill="red">SVG</text>
Sorry, your browser does not support inline SVG.
</svg>
Explanation of SVG Code
- The
<svg>element serves as the main container, specifying dimensions and namespace. - The
<rect>element creates a rectangle that fills the entire SVG with a green background. - The
<circle>element draws a yellow circle positioned usingcx,cy, andrattributes. - The
<text>element displaysSVGinside the shape, positioned usingxandyattributes, with font size, alignment, and color settings. - The
SVGends with the</svg>closing tag.
Key Points
Purpose: Embeds vector-based graphics in web pages.Attributes:Usage: Supports various graphical elements like shapes, text, and images.
Comparison: <canvas> vs. <svg>
| Feature | <canvas> | <svg> |
|---|---|---|
| Type | Raster-based | Vector-based |
| Performance | Better for animations | Best for scalable graphics |
| Accessibility | Requires JavaScript | XML-based, easily accessible |
Using Images for Graphics
The <img> tag allows displaying static graphics on web pages.
<img src="img.jpg" alt="Sample Graphic" width="200">
Conclusion
HTML offers various elements for displaying graphics, such as <canvas>, <svg>, and <img>. The <canvas> element is used for dynamic, JavaScript-driven graphics, ideal for animations. The <svg> tag provides scalable vector graphics, maintaining quality at any size. For static images, the <img> tag is the simplest option.
Dialog
The <dialog> element generates a modal window on a webpage, allowing the display of additional content. It can be opened or closed through JavaScript.
Forms
HTML <forms> gather user input and send it to a server. They include elements like text fields, checkboxes, and buttons, with attributes for validation and submission. Combined with CSS and JavaScript, forms become interactive and user-friendly.