


html5 javascript to create simple drawing board with pictures_javascript skills
See the picture:
The code is as follows:
<script> <br><br>var c;//The obtained 2d drawing board<br>var painting = false;//Determine whether painting is in progress, that is, whether the left mouse button is long pressed<br> var canvas;//Artboard<br>$(function(){ <br><br>$(".eraseSeries").hide();//Initial state radio button group is hidden<br><br>canvas= document.getElementById("myCanvas"); <br>c=canvas.getContext("2d"); <br>c.lineCap="round";//Set the corners of the handwriting, otherwise the handwriting will be broken<br>c .strokeStyle="black";//The color of the handwriting<br>c.lineWidth=5;//The thickness of the handwriting<br>$("#color").change(function(){//When the color of the handwriting changes <br>if(eraseFlag==true)//In the erase state<br>{ <br>$("#erase").trigger("click");//Automatically trigger the eraser's click event to return To the brush state<br>} <br>c.strokeStyle=$(this).val();//Set the brush state<br>c.lineWidth=$(this).val(); <br><br> }); <br><br>$("#fontSize").change(function(){//The brush thickness changes<br>if(eraseFlag==true)//Same as above<br>{ <br>$ ("#erase").trigger("click"); <br>} <br>c.lineWidth=$(this).val(); <br>c.strokeStyle=$("#color").val (); <br>//eraseFlag=false; <br>}); <br><br>$(".eraseSeries").click(function(){//The eraser size changes<br>var size= $('input[name="eraseSize"]:checked').val();//Get the selected value of the eraser radio button group<br>sizeE=size;//Transfer the value to the global variable, sizeE needs to be used to control the position of the eraser style <br>c.lineWidth=size; <br>$("#eraseImg").css({"width" :size "px","height":size "px"} );//The size of the eraser style changes<br>}); <br><br>$("#erase").toggle(function(){//The click flip event of the rubber button<br>c.save( );//Keep the last set state <br>eraseFlag=true; <br>c.strokeStyle="white"; <br><br>$("#erase").text("Brush");/ /Change the text on the button<br>$(".eraseSeries").show('fast');//Eraser radio group appears<br>// $("#eraseImg").show(); <br>sizeE=5; <br><br><br>},function(){ <br>eraseFlag=false; <br>$("#erase").text("Eraser"); <br>$( ".eraseSeries").hide('fast'); <br>c.restore();//Restore the last brush state (including color, thickness, etc.) <br>}); <br><br><br>//setInterval(paint,2); <br><br>}); <br><br>var p_x;//Last mouse position<br>var p_y; <br>var p_x_now;//Current Momentary mouse position<br>var p_y_now; <br>var eraseFlag=false; <br>var sizeE;//Eraser size<br><br>$(document).mousedown(function(e){//Mouse pressed Trigger event<br><br><br>// alert(sizeE); <br>p_x= e.clientX;//Get the position and set it as the last mouse position<br>p_y= e.clientY; <br>painting = true;//Brush start flag<br><br>}); <br>$(document).mousemove(function(e){//Mouse movement trigger event<br>if(eraseFlag==true&& e .clientY>30)//The eraser is active, and the position of the mouse Y is greater than 30, that is, the mouse is in the drawing board<br>{ <br><br>//The eraser image moves with the mouse<br>$( "#eraseImg").animate({left: e.clientX-sizeE "px",top: e.clientY-sizeE "px"},0).show('fast'); <br>} <br>else <br>{ <br>$("#eraseImg").hide('fast'); <br>} <br>if(painting==true)//The brush is active <br>{ <br>/ /alert(1); <br>p_x_now= e.clientX;//The mouse position at the current moment<br>p_y_now= e.clientY; <br>c.beginPath();//Start path<br>//Curve It is composed of very small straight lines, and the computer operation speed is very fast. This is a way of iteratively drawing curves with straight lines<br>c.moveTo(p_x-5-canvas.offsetLeft,p_y-5-canvas.offsetTop) ;//Move to the starting point<br>c.lineTo(p_x_now-5-canvas.offsetLeft,p_y_now-5-canvas.offsetTop);//Draw a straight line from the starting point to the end point<br><br>c.stroke( ); <br>c.closePath();//Closed path, this is very important. If the path is not closed, <br>// then as long as the canvas color changes, all previously painted colors will change <br> p_x = p_x_now;//After one iteration, the current instant coordinate value is assigned to the last mouse coordinate value <br>p_y = p_y_now; <br>} <br><br>}); <br><br>$( document).mouseup(function(e){//Mouse release trigger event<br><br>painting=false;//Freeze brush<br>}); <br><br></script>
5
10
15
20
30

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.

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.
