Remember that in the old days of Web front-end development, if you needed to draw or generate related graphics, using Flash was probably the only or most powerful way to do it, while in the recent hot technology HTML5 standard, HTML Canvas (Canvas) can be more convenient to help you achieve 2D graphics and graphics and various animation effects function.

First let’s take a look at what is HTML Canvas?

We can define Canvas in HTML using properties width and height. However, the implementation of Canvas related functions mainly relies on Javascript implementation, namely HTML5 Canvas API. We use javascript to access and control Canvas related areas, such as calling related drawing methods to dynamically generate the required animation or graphics.

Let’s look at the features of Canvas:

Interactivity: Canvas supports interaction and can respond well to user operations. We can monitor events related to keyboard, mouse and touch device through Javascript.

Animation: Any canvas drawing can be animated, from simple bouncing balls to complex HTML5 games

Flexibility: Developers can use Canvas to draw any content, such as lines, graphics, text, images, etc., with or without animation. You can also add audio or video browser support: almost all modern browsers support it, and it is supported by a wide variety of devices, such as desktops, tablets, smartphones, etc.

Popularity: Canvas is very popular right now, with many developers using it to create games and drawing applications

Web standards: you only need a browser to run it, not Flash or Silverlight, you need to install plug-ins

Once developed, it will run in any browser (except the older ones, of course)

You can use freely available development tools and their libraries.

What products or applications can we develop using HTML5 Canvas?

1. Visual data: all kinds of statistical charts, such as Baidu’s Echart

2. Scene show: Canvas can be used to achieve dynamic advertising effects, which can run harmoniously across platforms. For example, micro products in mobile phones. Good compatibility on mobile.

3 games: Canvas is more stereoscopic and delicate than Flash in web-based image display, making canvas the first choice for HTML5 small game development. At this stage, the business mode of H5 games is not very clear.

4 other embeddable content (mostly for active pages, special effects) : charts, audio, video, and many other elements can be better integrated with the Web and do not require any plug-ins.

5 Trend => Emulators: Emulator products can be implemented entirely in JavaScript, both visually and in terms of core functionality. Simulation of real hardware environment, such as mobile terminal various types of mobile phones.

6 Trend => Remote computer control: Canvas allows developers to better realize web-based data transmission and build a perfect visual control interface.

7 Trend => Graphic Editor: Photoshop graphic editor will be able to be 100% Web-based.

How to use HTML5 Canvas?

Using HTML5 Canvas is actually very simple. Each canvas has a context. You can use it to call the relevant canvas methods.

<canvas id=”mycanvas” width=”500″ height =”400″>

Your browser does not support HTML5 Canvas</p>

</canvas>

In the above code, we added a canvas tag to THE HTML. If the browser does not support canvas, the content of the <p> tag will be displayed. Of course, if you need to support older browsers, you can also use Flash or other methods to make an alternative solution.

var canvas = document.getElementById(‘mycanvas’),

The context = canvas. GetContext (” 2 d “);

The above code is used to fetch the 2D context from the canvas.

In the 2D structure of HTML5 Canvas, the coordinate (0,0) is in the upper left, which is different from the traditional coordinate. You need to pay attention, as shown below:

Let’s talk about the Canvas API:

Main properties and methods of Canvas:

Save (): Saves the status of the current environment

Restore (): Returns the previously saved path status and properties

createEvent()

GetContext (): Returns an object indicating the API necessary to access the drawing functionality

ToDateURL (): Returns the URL of the Canvas image

Color, style, and shadow properties and methods:

FillStyle: Sets or returns the color, gradient, or mode used to fill the painting

StrokeStyle: Sets or returns the color, gradient, or mode used for the stroke

ShadowColor: Sets or returns the color used for the shadow

ShadowBlur: Sets or returns the blur level used for shadows

ShadowOffsetX: Sets or returns the horizontal distance of the shadow from the shape

ShadowOffsetY: Sets or returns the vertical distance of the shadow from the shape

CreateLinearGradient (): Create a linear gradient (to apply to the canvas content)

CreatePattern (): Repeats the specified element in the specified direction

CreateRadialGradient (): Create a radial/circular gradient (to be used on the canvas content)

AddColorStop (): Specifies the color and stop position in the gradient object

Line style properties and methods

LineCap: Sets or returns the end point style of a line

LineJoin: Sets or returns the type of corner created when two lines intersect

LineWidth: Sets or returns the current line segment width

MiterLimit: Sets or returns the maximum miter length

Canvas API-path method

Fill (): Fill the current drawing (path)

Stroke (): Draws a defined path

BeginPath (): starts a path or resets the current path

MoveTo (): Moves the path to a specified point on the canvas without creating lines

ClosePath (): Creates a path from the current point back to the starting point

LineTo (): Adds a new point and creates a line from that point to the last specified point

Clip (): Cuts areas of arbitrary shape and size from the original canvas

QuadraticCurveTo (): Creates a quadratic Bezier curve

BezierCurveTo (): Creates three Bezier curves

Arc (): Create arcs/curves (used to create circles or partial circles)

ArcTo (): Creates an arc/curve between two tangents

IsPointInPath (): Returns a Boolean value if the specified point is in the current path

Canvas API- conversion method

Scale (): Scales the current drawing to a larger or smaller size

Rotate (): Rotates the current drawing

Translate (): remap the position (0,0) on the canvas

Transform (): Replaces the current transformation matrix of the drawing

SetTransform (): Resets the current transform to the identity matrix, and then runs transform ()

Canvas API- Text properties and methods

Font: Sets or returns the current font properties of the text content

TextAlign: Sets or returns the current alignment of text content

TextBaseline: sets or returns the current textBaseline used when drawing text

FillText (): Draws “filled” text on the canvas

StrokeText (): Draws text on the canvas (no fill)

MeasureText (): Returns an object containing the specified text width

Canvas API- image drawing method

DrawImage (): Draws an image, canvas, or video to the canvas

Canvas API- pixel manipulation methods and properties

Width: Returns the width of the ImageData object

Height: Returns the height of the ImageData object

Data: returns an object containing the ImageData of the specified ImageData object

CreateImageData (): Creates a new, blank i-Mago Data object

GetImageData (): Returns the ImageData object that copies pixel data for the specified rectangle on the canvas

PutImageData (): puts the ImageData(from the specified ImageData object) back onto the canvas

Canvas API- Image composition properties

GlobalAlpha: Sets or returns the current alpha or transparency value for the drawing

GlobalCompositeOperation: sets or returns a new image on how to draw to the existing image

Creating HTML5 canvas graphics quickly and efficiently?

Drawing graphics using Canvas directly is relatively tedious and troublesome, so in modern HTML5 Canvas, we use some ready-made third-party class libraries to help us achieve the function of drawing graphics quickly and cheaply:

KineticJS

Paper.js

EaselJS

Fabric.js

oCanvas

Echart.js