Use html5 canvas and JavaScript to create a simple example of a drawing program, html5canvas
This article will guide you through creating a simple drawing program using canvas and JavaScript.
First, prepare the Canvas element of the container. Then, everything will be in JavaScript.
Copy XML/HTML Code to clipboard
- <Canvas id = "canvasInAPerfectWorld" width = "490" height = "220"> </canvas>
Obtain the drawing environment. The context object provides methods and attributes for drawing on the canvas.
Copy XML/HTML Code to clipboard
- Context = document. getElementById ('canvasinaperfectworld'). getContext ("2d ");
Start the Drawing Process
First, we need to store the coordinate of the drawing path. addClick function adds the coordinate point value to the array.
Copy the content to the clipboard using JavaScript Code
- Var clickX = new Array ();
- Var clickY = new Array ();
- Var clickDrag = new Array (); // storage path
- Var paint; // specifies whether to draw the image. Set it to true when mousedown.
- Function addClick (x, y, dragging)
- {
- ClickX. push (x );
- ClickY. push (y );
- ClickDrag. push (dragging );
- }
The redraw function re-draws the entire canvas every time it is called. First, we clear the content on the canvas and set the connection method for drawing the line color and width. Then
Draw a path between two points and draw the coordinates in the array in sequence
Copy XML/HTML Code to clipboard
- Function redraw (){
- Context. clearRect (0, 0, context. canvas. width, context. canvas. height); // clear the canvas content
- Context. strokeStyle = "# df4b26"; // set the line color
- Context. lineJoin = "round"; // create a circular corner when two lines converge.
- Context. lineWidth = 5; // line width
- For (var I = 0; I <clickX. length; I ++ ){
- Context. beginPath (); // start a path or reset the current path.
- 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 (); // draw the path
- }
- }
Events required during plotting
1 mousedown event
This event is triggered when you click draw on the canvas. The addClick function is called and the paint is set to true.
Copy the content to the clipboard using JavaScript Code
- $ ('# 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
When the paint set in mousedown is true, the mousemove event is triggered when the mouse moves, all the points that move the mouse are recorded, and redraw is continuously called to re-paint the cloth.
Copy the content to the clipboard using JavaScript Code
- $ ('# Canvas'). mousemove (function (e ){
- If (paint ){
- AddClick (e. pageX-this. offsetLeft, e. pageY-this. offsetTop, true );
- Redraw ();
- }
- });
3. mouseup event
Mouseup: Click and hold the mouse, or drag it, and then release it. This indicates that the path is drawn and the painting is set to false.
Copy XML/HTML Code to clipboard
- $ ('# Canvas'). mouseup (function (e ){
- Paint = false;
- });
4 mouseleave event
Mouseleave move the mouse away from the canvas Element and set paint to false.
Copy XML/HTML Code to clipboard
- $ ('# Canvas'). mouseleave (function (e ){
- Paint = false;
- });
The simple example of creating a drawing program using the canvas and JavaScript of html5 is all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the house.
Address: http://www.cnblogs.com/fangsmile/archive/2016/07/05/5644611.html