Simple drawing board <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>