html canvas is an html rectangular area on which graphics can be drawn. Each pixel can be controlled. A canvas can be added to the page through the canvas element. The syntax is "".
The operating environment of this article: Windows 10 system, html5 version, Dell G3 computer.
The canvas is an HTML area on which graphics can be drawn.
The canvas element is used to draw graphics on web pages.
The canvas element of HTML5 uses JavaScript to draw images on the web page.
The canvas is a rectangular area that you can control every pixel of.
canvas has a variety of methods for drawing paths, rectangles, circles, characters, and adding images.
Create Canvas element
Add canvas element to HTML5 page.
Specify the id, width and height of the element:
<canvas id="myCanvas" width="200" height="100"></canvas>
Draw through JavaScript
The canvas element itself has no drawing capabilities. All drawing work must be done inside JavaScript:
<script type="text/javascript"> var c=document.getElementById("myCanvas"); var cxt=c.getContext("2d"); cxt.fillStyle="#FF0000"; cxt.fillRect(0,0,150,75); </script>
JavaScript uses id to find the canvas element:
var c=document.getElementById("myCanvas");
Then, create the context object:
var cxt=c.getContext("2d");
getContext("2d" ) object is a built-in HTML5 object with multiple methods for drawing paths, rectangles, circles, characters, and adding images.
The following two lines of code draw a red rectangle:
cxt.fillStyle="#FF0000"; cxt.fillRect(0,0,150,75);
The fillStyle method dyes it red, and the fillRect method specifies the shape, position, and size.
Recommended tutorial: "html video tutorial"
The above is the detailed content of What is html canvas. For more information, please follow other related articles on the PHP Chinese website!