


A collection of HTML5 Canvas basic drawing example codes_html5 tutorial skills
Basic drawing
- var canvas = document.getElementById('canvas');
- if (canvas.getContext) {
- var context = canvas.getContext('2d');
- // Line width
- context.lineWidth = 4;
- // Brush color
- context.strokeStyle = 'red';
- // Fill color
- context.fillStyle = "red";
- // Line cap type
- context.lineCap = 'butt'; // round, square
- // Start path
- context.beginPath();
- // Starting point
- context.moveTo(10,10);
- // End point
- context.lineTo(150,50);
- // Drawing
- context.stroke();
- }
Rectangle
- var canvas = document.getElementById('canvas');
- if (canvas.getContext) {
- context.beginPath();
- context.strokeRect(10,10,70,40);
- // Another way of rectangle
- context.rect(10,10.70,40);
- context.stroke();
- // solid rectangle
- context.beginPath();
- context.fillRect(10,10,70,40);
- // Another way solid rectangle
- context.beginPath();
- context.rect(10,10,70,40);
- context.fill();
- }
Round
- var canvas = document.getElementById('canvas');
- if (canvas.getContext) {
- context.beginPath();
- // Circle center coordinate x, circle center coordinate Y, arc radius, starting angle, ending angle, whether counterclockwise
- // The fourth and fifth parameters are the radians to be passed in. If you draw an angle of 30, you need to convert it into radians 30 * Math.PI / 180
- context.arc(100,100,70,0,130 * Math.PI / 180, true);
- context.stroke();
- context.fill();
- }
Rounded corners
- var canvas = document.getElementById('canvas');
- if (canvas.getContext) {
- context.beginPath();
- context.moveTo(20,20);
- context.lineTo(70,20);
- // Draw arc p1.x p1.y p2.x, p2.y arc radius for a path,
- context.arcTo(120,30,120,70, 50);
- context.lineTo(120,120);
- context.stroke();
- // Erase canvas artboard
- context.beginPath();
- context.fillRect(10,10,200,100);
- // Erase area
- context.clearRect(30,30,50,50);
- }
Quadratic Bezier Curve
- var canvas = document.getElementById('canvas');
- if (canvas.getContext) {
- context.beginPath();
- context.moveTo(100,100);
- context.quadraticCurveTo(20,50,200,20);
- context.stroke();
- }
Cubic Bezier Curve
- var canvas = document.getElementById('canvas');
- if (canvas.getContext) {
- context.moveTo(68,130);
- var cX1 = 20;
- var cY1 = 10;
- var cX2 = 268;
- var cY2 = 10;
- var endX = 268;
- var endY = 170;
- context.bezierCurveTo(cX1, cY1, cX2, cY2, endX, endY);
- context.stroke();
- // 利用clip指定绘图区域,指定绘图区域之后,只能在绘图区域中进行绘图擦欧总
- // 绘制圆形
- context.arc(100,100,40,0, 360 * Math.PI/ 180 , true);
- // 限制区域
- context.clip();
- // 开始尝试绘制其他
- context.beginPath();
- context.fillStyle = 'lightblue';
- // 结果矩形并没有显示出来
- context.fillRect(0,0,300,150);
- }
画板进阶使用
- var canvas = document.getElementById('canvas');
- if (canvas.getContext) {
- var context = canvas.getContext('2d');
- /*
- * drawImage(image,dx,dy)
- * drawImage(image,dx,dy,dw,dh)
- * drawImage(image,sx,sy,sw,sh,dx,dy,dw,dh);
- * image drawing object
- * Coordinates of dx dy canvas
- * dw, dh indicates the position of the image in the canvas to be drawn
- * sw, sh represents the area of image to be drawn
- * sx,sy The starting position of the drawing to be drawn
- */
- var image = document.getElementById('img');
- context.drawImage(image, 0, 0);
- var img = new Image();
- img.src = 'images/1.jpg';
- img.onload = function(){
- // drawImage
- // Start drawing from 0,0 coordinates
- // context.drawImage(img,0,0);
- // Starting from 0, 0, draw the entire picture to 100,100 length and width
- // context.drawImage(img, 0, 0, 100, 100);
- // Screenshot, 50,50 to 100,100. Start drawing from 260,130 and place it in the 100,100 length and width area.
- // context.drawImage(img, 50, 50, 100,100, 260, 130, 100, 100);
- // Use getImageData and putImageData to draw pictures
- context.drawImage(img, 10, 10);
- // Get pixel data from the artboard
- // Start position, end position
- var imgData
-
= context.getImageData(50,50,100,100);
- // Draw the data to the specified position coordinates on the drawing board
- context.putImageData(imgData,10,260);
- // Draw part of the pixel data to the drawing board
- context.putImageData(imgData,200,260,50,50,100,100);
- imgData = context.getImageData(50,50,200,200);
// Create an empty object of specified size- imgData01 = context.createImageData(imgData);
- for (
i- = 0; i imgData01.width * imgData01.height * 4; i =4) { // Red pixels
- imgData01.data[i 0] = 255;
- imgData01.data[i 1] = 0;
- imgData01.data[i 2] = 0;
- imgData01.data[i 3] = 255;
- context.putImageData(imgData01, 10, 260);
- }
- }

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

H5, or HTML5, is the fifth version of HTML. It provides developers with a stronger tool set, making it easier to create complex web applications. The core functions of H5 include: 1) elements that allow drawing graphics and animations on web pages; 2) semantic tags such as, etc. to make the web page structure clear and conducive to SEO optimization; 3) new APIs such as GeolocationAPI support location-based services; 4) Cross-browser compatibility needs to be ensured through compatibility testing and Polyfill library.


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool