Canvas draws basic geometry
Canvas element and 2D context
- Canvas width and height: The width and height attributes on the Canvas element.
<canvas width="512" height="512"></canvas> Copy the code
- Style width height: The CSS style property of the Canvas element.
canvas { width: 256px; height: 256px; } Copy the code
- Canvas width and height default, equal to style width and height.
- The width and height of the canvas determine the coordinate range of the visible area.
Canvas separates the width and height of the Canvas from the width and height of the style: it is easier to adapt to different display devices.
Second, Canvas coordinate system
A browser window-like coordinate system: by default, the upper-left corner is the origin of the coordinates, X-axis horizontally to the right, Y-axis vertically down.
Rotation or 3d corresponds to left-handed system.
3. Canvas draws geometric graphics
- To obtain
Canvas
context// Get the Canvas element from the DOM API const canvas = document.querySelector('canvas'); // Get the Canvas context using the getContext method const context = canvas.getContext('2d'); Copy the code
- with
Canvas
Context drawing graph- state-set
API
To set or change the current drawing state. Such as: color, line width, coordinate transformation and so on. - Draw the instruction
API
, used to draw geometric shapes of different shapes.// Draw a 100*100 square in the center of the Canvas const rectSize = [100.100]; context.fillStyle = 'red'; context.beginPath(); context.rect((canvas.width - rectSize[0) /2, canvas.height - rectSize[1] /2.100.100); context.fill(); Copy the code
- state-set
The method of drawing graphics to the center of a canvas
- Change the coordinates of the vertices of the graph to be drawn.
Multi-vertex graph needs to calculate the position of each vertex before drawing, which is very troublesome.
- through
translate
Translation.
Just get the offset from the center point to the upper left corner. However, it is necessary to restore the state of the canvas before drawing any subsequent graphics.
Method to restore the state of the canvas
- Reverse translation recovery method
/ / translation context.translate(-rectSize[0] /2, -rectSize[1] /2); / / recovery context.translate(rectSize[0] /2, rectSize[1] /2); Copy the code
restore
Method of instruction// Temporary state context.save(); / / translation context.translate(-rectSize[0] /2, -rectSize[1] /2); // ... / / recovery context.restore(); Copy the code
5 steps to draw a Canvas rectangle
- through
getContext
To obtainCanvas
Object; - through
fillStyle
.translate
Etc. Set drawing state; - call
beginPath
Command to start drawing a rectangle; - Call drawing instruction (Canvas MDN official document);
Rect draws rectangles; lineTo draws straight lines; polygon arc draws arcs, circles, ellipses, and fan quadraticCurveTo; bezierCurveTo draws Bezier curves
- call
fill
Command to actually output the drawn content to the canvas.
Iv. Canvas draws hierarchy diagram
The mapping between Hierarchy Data and drawing instructions is established, that is, converting JSON Data into graphic information.
- Process the data
Data conversion tool library D3-Hierarchy
- Through the data
- Drawing graphics
Use the clearRect command to erase the previous graph and draw the new graph.
V. Advantages and disadvantages of Canvas
- Advantages: Easy to operate, efficient rendering capability.
Canvas is a very simple and easy to use graphics system, which can easily draw a variety of complex geometric graphics. Canvas is more towards the rendering layer and can provide the underlying graphics rendering API to complete rendering with high performance.
- Disadvantages: Internal graphic elements are not easy to control.
Canvas is an independent Canvas element in HTML, and the graphics drawn are only pixels in Canvas for the browser, so it is difficult to directly extract the graphics for operation.