Read: 12 - Docs for the HTML
Article: Easily create stunning animated charts with chart.Js
- Chart.js is a free open-source JavaScript library for data visualization(to create charts).
- Setting up:
- Import the script on html page
<script src='Chart.min.js'></script>. - Create a canvas element in our HTML in which Chart.js.
<canvas id="" width="" height=""></canvas> - Write a script that will retrieve the context of the canvas.
-
Inside the same script tags we need to create our data.
Article: Basic usage of canvas
- Import the script on html page
- The
<canvas>element can be styled just like any normal image (margin, border, background). - You should always provide fallback content to display CanvasIn older browsers.
-
The
<canvas>element has a method calledgetContext()(takes one parameter), used to obtain the rendering context and its drawing functions.ex:var ctx = canvas.getContext('2d');.Article: Drawing shapes with canvas
<canvas>only supports two primitive shapes: rectangles and paths (lists of points connected by lines).Three functions that draw rectangles on the canvas:
fillRect(x, y, width, height)-> Draws filled rectangle.strokeRect(x, y, width, height)-> Draws a rectangular outline.clearRect(x, y, width, height)-> Clears the specified rectangular area, making it fully transparent. ch side.
-
The
clearRect()function erases anumxnumpixel square from the center.Drawing paths:
- First Step:
beginPath()-> Creates a new path. - Path methods -> Methods to set different paths for objects.
closePath()-> Adds a straight line to the path, going to the start of the current sub-path.stroke()-> Draws the shape by stroking its outline.fill()-> Draws a solid shape by filling the path’s content area.
- First Step:
-
Note: When you call
fill(), any open shapes are closed automatically, so you don’t have to callclosePath().Drawing a triangle:
-
the
moveTo()function-> it is like lifting a pen or pencil from one spot on a piece of paper and placing it on the next. -
moveTo(x, y) Moves the pen to the coordinates specified by x and y.Lines:
-
lineTo(x, y)-> For drawing straight lines, Draws a line from the current drawing position to the position specified by x and y.Arcs
arc()orarcTo()-> To draw arcs or circles.arc(x, y, radius, startAngle, endAngle, counterclockwise)-> Draws an arc which is centered at (x, y) position with radius r starting at startAngle and ending at endAngle going in the given direction indicated by counterclockwise (defaulting to clockwise).-
arcTo(x1, y1, x2, y2, radius)-> Draws an arc with the given control points and radius, connected to the previous point by a straight line. -
Note: Angles in the arc function are measured in radians, not degrees.
Bezier and quadratic curves:
quadraticCurveTo(cp1x, cp1y, x, y)Draws a quadratic Bézier curve from the current pen position to the end point specified by x and y, using the control point specified by cp1x and cp1y.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)Draws a cubic Bézier curve from the current pen position to the end point specified by x and y, using the control points specified by (cp1x, cp1y) and (cp2x, cp2y). —Rectangles:
In addition to the three methods….
rect(x, y, width, height)-> Draws a rectangle whose top-left corner is specified by (x, y) with the specified width and height. —-
we can Make combinations of previous functions
Path2D objects
- the Path2D object, available in recent versions of browsers.
-
Path2D()-> constructor returns a newly instantiated Path2D object.Article: Applying styles and colors
two important properties to apply colors to a shape:
fillStyle = color-> Sets the style used when filling shapes.strokeStyle = color-> Sets the style for shapes’ outlines.
-
By default, the stroke and fill color are set to black (CSS color value #000000).
Transparency
- drawing a semi-transparent (or translucent) shapes, can be done by either setting the globalAlpha property or by assigning a semi-transparent color to the stroke and/or fill style
-
globalAlpha = transparencyValue-> The value must be between 0.0 (fully transparent) to 1.0 (fully opaque)-which is defualt value.Line styles properties:
lineWidth = valuelineCap = type-> Sets the appearance of the ends of lines; it’s values butt, round and square.lineJoin = type-> Sets the appearance of the “corners” where lines meet.Values: round, bevel and miter -miterLimit = value-> Establishes a limit on the miter when two lines join at a sharp angle, to let you control how thick the junction becomes. -getLineDash()-> Returns the current line dash pattern array containing an even number of non-negative numbers.setLineDash(segments)-> Sets the current line dash pattern.-
lineDashOffset = value-> Specifies where to start a dash array on a line.Gradients
createLinearGradient(x1, y1, x2, y2)-> Creates a linear gradient object with a starting point of (x1, y1) and an end point of (x2, y2).createRadialGradient(x1, y1, r1, x2, y2, r2)-> Creates a radial gradient. The parameters represent two circles, one with its center at (x1, y1) and a radius of r1, and the other with its center at (x2, y2) with a radius of r2.-
createConicGradient(angle, x, y)-> Creates a conic gradient object with a starting angle of angle in radians, at the position (x, y).Patterns
createPattern(image, type)- The type values:
repeat-> Tiles the image in both vertical and horizontal directions.repeat-x-> Tiles the image horizontally but not vertically.repeat-y-> Tiles the image vertically but not horizontally.-
no-repeat-> Doesn’t tile the image. It’s used only once.Shadows
shadowOffsetX = float-> Indicates the horizontal distance the shadow should extend from the object. This value isn’t affected by the transformation matrix. The default is 0.shadowOffsetY = float-> same but for vertical distance.shadowBlur = float-> Indicates the size of the blurring effect; this value doesn’t correspond to a number of pixels and is not affected by the current transformation matrix. The default value is 0.-
shadowColor = color-> A standard CSS color value indicating the color of the shadow effect; by default, it is fully-transparent black.Article: Drawing text
Drawing text 2methods:
fillText(text, x, y [, maxWidth])-> Fills a given text at the given (x,y) position. Optionally with a maximum width to draw.strokeText(text, x, y [, maxWidth])-> Strokes a given text at the given (x,y) position. Optionally with a maximum width to draw.Styling text properties:
font = value-> This string uses the same syntax as the CSS font property. The default font is 10px sans-serif.textAlign = value-> Possible values: start, end, left, right or center. The default value is start.textBaseline = value-> Baseline alignment setting. Possible values: top, hanging, middle, alphabetic, ideographic, bottom. The default value is alphabetic.direction = value-> Directionality. Possible values: ltr, rtl, inherit. The default value is inherit.