Canvas element in HTML5
The canvas element is used to draw graphics on web pages.
What is Canvas?
HTML 5’s canvas element uses JavaScript to draw images on the web page.
The canvas is a rectangular area that you can control every pixel of.
canvas has multiple ways to draw paths, rectangles, circles, characters, and add images.
Create Canvas element
Add canvas element to HTML 5 page, specify the element's id, width and height:
<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 the id to find the canvas element:
var c=document.getElementById("myCanvas");
Then, create the context object:
var cxt=c.getContext("2d");
getContext("2d") The object is a built-in HTML 5 object with many Ways to draw paths, rectangles, circles, characters, and add images.
Example: Hover the mouse over the rectangle to see the coordinates. Try it yourself. The code is as follows:
<!DOCTYPE HTML> <html> <head> <style type="text/css"> body { margin: 0px; font-size: 70%; font-family: verdana, helvetica, arial, sans-serif; } #coordiv { float: left; width: 199px; height: 99px; border: 1px solid #c3c3c3 } </style> <script type="text/javascript"> function cnvs_getCoordinates(e) { x=e.clientX; y=e.clientY; document.getElementById("xycoordinates").innerHTML="Coordinates: (" + x + "," + y + ")"; } function cnvs_clearCoordinates() { document.getElementById("xycoordinates").innerHTML=""; } </script> </head> <body> <p>把鼠标悬停在下面的矩形上可以看到坐标:</p> <div id="coordiv" onmousemove="cnvs_getCoordinates(event)" onmouseout="cnvs_clearCoordinates()"></div> <div id="xycoordinates"></div> </body> </html>
The above is the content of the Canvas element in HTML5. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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

AI Hentai Generator
Generate AI Hentai for free.

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 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 Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

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