


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);
- }
- }

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.

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

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 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.
