How to draw graphics using H5's CanvasAPI
This time I will bring you H5CanvasAPI how to draw graphics and how to use Canvas to make graphics? What are the precautions for making graphics with Canvas? The following is a practical case, let’s take a look.
Canvas element
The following html code defines a canvas element.
<!DOCTYPE html> <html> <head> <title>Canvas快速入门</title> <meta charset="utf-8"/> </head> <body> <div> <canvas id="mainCanvas" width="640" height="480"></canvas> </div> </body> </html>
Access the canvas element through the following Javascript statement:
//DOM写法 window.onload = function () { var canvas = document.getElementById("mainCanvas"); var context = canvas.getContext("2d"); }; //jQuery写法 $(document).ready(function () { var canvas = $("#mainCanvas"); var context = canvas.get(0).getContext("2d"); }); //接下来就可以调用context的方法来调用绘图API
2. Basic API
2.1 Coordinate system
Canvas 2D rendering context uses planar Cartesian coordinates System, the upper left corner is the origin (0,0), and 1 unit of the coordinate system is equivalent to 1 pixel of the screen.
//绘制一个填充矩形 context.fillRect(x, y, width, height) //绘制一个边框矩形 context.strokeRect(x, y, width, height) //清除一个矩形区域 context.clearRect(x, y, width, height)
2.2.2 Lines
There are some differences between drawing lines and drawing graphics. Lines are actually called paths. To draw a simple path, you must first call the beginPath method, then call moveTo to set the starting point coordinates of the path, then call lineTo to set the end point coordinates of the line segment (can be set multiple times), and then call closePath to complete the path drawing. Finally, call stroke to draw the outline (or call fill to fill the path). The following is an example:
//示例 context.beginPath(); //开始路径 context.moveTo(40, 40); //移动到点(40,40) context.lineTo(300, 40); //画线到点(300,30) context.lineTo(40, 300); //画线到点(40,300) context.closePath(); //结束路径 context.stroke(); //绘制轮廓 //或者填充用context.fill();
2.2.3 Circle
Canvas actually does not have a special method for drawing a circle. You can simulate a circle by drawing an arc. Since an arc is a path, the API for drawing arcs should be included between beginPath and closePath.
2.3 Style
2.3.1 Modify line color
var color; //指定RGB值 color = "rgb(255, 0, 0)"; //指定RGBA值(最后一个参数为alpha值,取值0.0~1.0) color = "rgba(255, 0, 0, 1)"; //指定16进制码 color = "#FF0000"; //用单词指定 color = "red"; //设置填充颜色 context.fillStyle = color; //设置边框颜色 context.strokeStyle = color;
2.3.2 Modify line width
//指定线宽值 var value= 3; //设置边框颜色 context.linewidth = value;
2.4 Draw text
//Specify font style
context.font = "italic 30px bold";
//Draw text at point (40,40)
context.fillText("Hello world!", 40, 40);
I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
How to count the number of table sums in bootstrap
How to use JS to switch between hiding and displaying Switch icons simultaneously
#How to use JS to disable and enable buttons
The above is the detailed content of How to draw graphics using H5's CanvasAPI. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
