


Examples of javaScript canvas implementing brush size, color, and eraser
Canvas, translated as "canvas" in Chinese, has a new
The example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <p> <p> <!--<input type="button" id="open" value="open"></input> <input type="button" id= "save" value = "save"></input> <input type="button" id= "color" value="color"></input> --> <input type="button" value="size"></input> <input type="text" id="size" onchange="sizeChange()"></input> <input type="button" id="clear" value="clear"></input> <input type="button" id="eraser" value="eraser" onclick="doEraser()"></input> <select id = "shape" onchange="shapeChange()"> <option value = "99">shape</option> <option value = "1">rectangle</option> <option value = "0">circle</option> <option value = "2">line</option> </select> <input id="color" type="color"/> </p> <canvas id="myCanvas" style=" border:1px solid;" width="800" height="500"></canvas> </p> </body> <script language="JavaScript"> var shap = 99; //0 is circle; 1 is rectangle var orignalX, orignalY;//the coordinate of mouse down var lastX, lastY;//the coordinate of last mouse position var isMouseDown = false; // flag of mouse pressing down var myCanvas = document.getElementById("myCanvas"); var context = myCanvas.getContext('2d'); var width = myCanvas.width, height = myCanvas.height; var data;//storing last canvas' content context.strokeStyle = "black"; context.strokeWidth=1; context.lineWidth = 1; document.getElementById('color').onchange = function(){ context.strokeStyle = this.value }; function doEraser(){ context.strokeStyle = "white"; shap = 2; } function sizeChange(){ context.lineWidth = parseInt(document.getElementById('size').value); } function shapeChange(){ context.strokeStyle = "black"; var myselect = document.getElementById("shape"); var index=myselect.selectedIndex ; var myvalue = myselect.options[index].value; var mytext=myselect.options[index].text; shap = parseInt(myvalue); } function myCanvasMouseDown(event) { //event.preventDefault(); if(event.button == 0) { orignalX = event.offsetX; orignalY = event.offsetY; context.moveTo(orignalX,orignalY); data = context.getImageData(0, 0, width, height); isMouseDown = true; } } function myCanvasMouseMove(event) { if (isMouseDown){ //event.preventDefault(); switch(shap){ case 0: context.clearRect(0,0,width,height); context.putImageData(data,0,0); lastX = event.offsetX; lastY = event.offsetY; context.beginPath(); context.arc(orignalX+(lastX-orignalX)/2,orignalY+(lastY-orignalY)/2,Math.abs(lastX-orignalX)/2,0,Math.PI * 2,true); context.stroke(); context.closePath(); break; case 1: context.clearRect(0,0,width,height); context.putImageData(data,0,0); lastX = event.offsetX; lastY = event.offsetY; context.strokeRect(orignalX, orignalY, lastX-orignalX, lastY-orignalY); break; case 2: lastX = event.offsetX; lastY = event.offsetY; context.lineTo(lastX, lastY); //根据鼠标路径绘画 context.stroke(); //立即渲染 break; } } } function myCanvasMouseUp(event) { if (isMouseDown){ //event.preventDefault(); context.clearRect(0,0,width,height); context.putImageData(data,0,0); lastX = event.offsetX; lastY = event.offsetY; switch(shap){ case 0: context.beginPath(); context.arc(orignalX+(lastX-orignalX)/2,orignalY+(lastY-orignalY)/2,Math.abs(lastX-orignalX)/2,0,Math.PI * 2,true); context.stroke(); context.closePath(); break; case 1: context.beginPath(); context.strokeRect(orignalX, orignalY, lastX-orignalX, lastY-orignalY); context.closePath(); break; case 2: context.lineTo(lastX, lastY); //根据鼠标路径绘画 context.stroke(); //立即渲染 break; } isMouseDown = false; lastX = null; lastY = null; orignalX = null; orignalY = null; data = context.getImageData(0, 0, width, height); context.beginPath(); context.clearRect(0,0,width,height); context.putImageData(data,0,0); context.closePath(); } } myCanvas.addEventListener("mousedown", myCanvasMouseDown, false); myCanvas.addEventListener("mousemove", myCanvasMouseMove, false); myCanvas.addEventListener("mouseup", myCanvasMouseUp, false); </script> </html>
Related recommendations :
Commonly used drawing techniques in Canvas in HTML5
js+HTML5 use Detailed explanation of canvas drawing method
The above is the detailed content of Examples of javaScript canvas implementing brush size, color, and eraser. 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

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Understand the power and application of canvas in game development Overview: With the rapid development of Internet technology, web games are becoming more and more popular among players. As an important part of web game development, canvas technology has gradually emerged in game development, showing its powerful power and application. This article will introduce the potential of canvas in game development and demonstrate its application through specific code examples. 1. Introduction to canvas technology Canvas is a new element in HTML5, which allows us to use

Canvas code can be written inside the <body> tag of an HTML file, usually as part of the HTML document. The core of the Canvas code is to obtain and operate the context of the Canvas element. The reference to the Canvas element is obtained through document.getElementById('myCanvas') , and then use getContext('2d') to get the 2D drawing context.
