


Sample code to create a drawing program using HTML5 canvas and JavaScript
The following editor will bring you a simple example of creating a drawing program using html5canvas and JavaScript . The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
This article will guide you to create a simple drawing program using canvas and JavaScript.
First prepare the container Canvas element, and then everything will be done in JavaScript.
XML/HTML Code复制内容到剪贴板 <canvas id="canvasInAPerfectWorld" width="490" height="220"></canvas>
Get the drawing environment, the context object provides methods and properties for drawing on the canvas
context = document.getElementById('canvasInAPerfectWorld').getContext("2d");
Start the drawing process
First we need to store the drawing path point coordinates. The addClick function adds the coordinate point value to the array.
var clickX = new Array(); var clickY = new Array(); var clickDrag = new Array();//存储路径点 var paint;//是否绘制,mousedown时置为true function addClick(x, y, dragging) { clickX.push(x); clickY.push(y); clickDrag.push(dragging); }
The redraw function will redraw the entire canvas every time it is called. First, we clear the content on the canvas and set the line color, thickness, and line connection method. Then
Draw a path between the two points, and draw the coordinate points in the array in sequence
function redraw(){ context.clearRect(0, 0, context.canvas.width, context.canvas.height); // 清除画布内容 context.strokeStyle = "#df4b26";//设置线条颜色 context.lineJoin = "round";//当两条线条交汇时,创建圆形边角 context.lineWidth = 5;//线条粗细 for(var i=0; i < clickX.length; i++) { context.beginPath();//开始一条路径,或重置当前的路径 if(clickDrag[i] && i){ context.moveTo(clickX[i-1], clickY[i-1]); }else{ context.moveTo(clickX[i]-1, clickY[i]); } context.lineTo(clickX[i], clickY[i]); context.closePath(); context.stroke();//绘制路径 } }
Events required for the drawing process
1 mousedown event
When drawing this click on the canvas, the execution of this event will be triggered. The addClick function is called and paint is set to true.
$('#canvas').mousedown(function(e){ var mouseX = e.pageX - this.offsetLeft; var mouseY = e.pageY - this.offsetTop; paint = true; addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop); redraw(); });
##2 mousemove event
After the paint set in mousedown is true, the mousemove event execution is triggered when the mouse moves, all points moved by the mouse are recorded, and redraw is continuously called to redraw the canvas.$('#canvas').mousemove(function(e){ if(paint){ addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true); redraw(); } });
3 mouseup event
mouseup Click and release the mouse or drag and release, indicating that the path is drawn. Set paint to false.$('#canvas').mouseup(function(e){ paint = false; });
4 mouseleave event
mouseleaveThe mouse leaves the canvas element and sets paint to false.$('#canvas').mouseleave(function(e){ paint = false; });
The above is the detailed content of Sample code to create a drawing program using HTML5 canvas and JavaScript. 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 HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

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