HTML — Part-6
HTML Graphics
There are four ways to draw things on the web: Canvas, SVG, CSS, and direct DOM animation. Canvas differ from the other three
HTML Canvas Graphics
- The Canvas API provides a means for drawing graphics via JavaScript’s context object and the HTML
<canvas>
element. - It can be used for animation, game graphics, data visualization, photo manipulation, and real-time video processing.
- The
<canvas>
element is only a container for graphics. You must use JavaScript to actually draw the graphics. - A canvas is a rectangular area on an HTML page.
- By default, a canvas has no border and no content and the default size of the canvas is 300pixels X 150 pixels.
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;">
</canvas>
Note: Always specify an
id
attribute (to be referred to in a script), and awidth
andheight
attribute to define the size of the canvas. To add a border, use thestyle
attribute.
HTMLCanvasElement
The HTMLCanvasElement
interface provides properties and methods for manipulating the layout and presentation of <canvas>
elements.
Properties: —
- HTMLCanvasElement.height —
- is a positive
integer
- the default value of
150
2. HTMLCanvasElement.width —
- is a positive
integer
- the default value of
300
3.HTMLCanvasElement.mozOpaque —
- Is a boolean value
- It lets the canvas know whether or not translucency will be a factor.
Methods : -
- HTMLCanvasElement.captureStream()
- Returns a
CanvasCaptureMediaStreamTrack
that is a real-time video capture of the surface of the canvas.
2. HTMLCanvasElement.getContext()
- Returns a drawing context on the canvas
- A drawing context lets you draw on the canvas.
- Calling getContext with
"2d"
returns aCanvasRenderingContext2D
object, whereas calling it with"webgl"
(or"experimental-webgl"
) returns aWebGLRenderingContext
object.
3. HTMLCanvasElement.toDataURL()
- Returns a data-URL containing a representation of the image in the format specified by the
type
parameter (defaults topng
). - The returned image is in a resolution of 96dpi.
4. HTMLCanvasElement.toBlob()
- Creates a
Blob
object representing the image contained in the canvas
5. HTMLCanvasElement.transferControlToOffscreen()
- Transfers control to an
OffscreenCanvas
object, either on the main thread or on a worker.
Events: -
- webglcontextcreationerror
- Fired if the user agent is unable to create a
WebGLRenderingContext
orWebGL2RenderingContext
context.
2. webglcontextlost
- Fired if the user agent detects that the drawing buffer associated with a
WebGLRenderingContext
orWebGL2RenderingContext
object has been lost.
3. webglcontextrestored
- Fired if the user agent restores the drawing buffer for a
WebGLRenderingContext
orWebGL2RenderingContext
object.
Example: -
// Javascript
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 150, 100);
So whats happeing here?
- The
Document.getElementById()
method gets a reference to the HTML<canvas>
element. - Next, the
HTMLCanvasElement.getContext()
method gets that element's context—the thing onto which the drawing will be rendered. - The actual drawing is done using the
CanvasRenderingContext2D
interface which is retured by the getContext(‘2d’) method call. - The
fillStyle
property makes the rectangle green. - The
fillRect()
method places its top-left corner at (10, 10), and gives it a size of 150 units wide by 100 tall.
Coordinate System
- Canvas has the origin in the upper left corner with the y axis going down. This is traditional for computer graphics, but if you want a different origin you can do that with transforms
Paths
- Canvas only directly supports the rectangle shape. To draw any other shape you must draw it yourself using a path.
- In Canvas you must first define a path with
beginPath()
, then you can fill it, stroke it, or use it as a clip - You define each line segment with functions like
moveTo()
,lineTo()
, andbezierCurveTo()
.
HTML SVG Graphics
- SVG defines vector-based graphics in XML format.
- The HTML
<svg>
element is a container for SVG graphics.
SVG Rectangle
<svg width=”400" height=”100">
<rect width=”400" height=”100" style=”fill:rgb(0,0,255);stroke-width:10;stroke:rgb(0,0,0)” />
</svg>
SVG Star
<svg width="300" height="200">
<polygon points="100,10 40,198 190,78 10,78 160,198"
style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" />
</svg>
Difference betwwen SVG and Canvas:-
- SVG is a language for describing 2D graphics in XML whereas Canvas draws 2D graphics, on the fly (with a JavaScript).
- In SVG, each drawn shape is remembered as an object. If attributes of an SVG object are changed, the browser can automatically re-render the shape whereas Canvas is rendered pixel by pixel. In canvas, once the graphic is drawn, it is forgotten by the browser. If its position should be changed, the entire scene needs to be redrawn.
Sources: -