Simple implementation of graffiti Html5 write your own canvas, html5 canvas
I recently learned that I was amazed by html5's powerful drawing function. So I wrote a little theme: Graffiti board. The following functions can be implemented: painting, color change, and paint brush size adjustment.
Html5 plotting can be divided into points, lines, faces, circles, images, points, and lines. This is the basis of all the plane effects, only unexpected algorithms are supported.
Code first:
Html
Copy XML/HTML Code to clipboard
- <Body style = "cursor: pointer">
- <Canvas id = "mycavas" width = "1024" height = "400" style = "border: solid 4px #000000"> </canvas> <! -- Canvas -->
- <Input type = "color" id = "color1" name = "color1"/> <! -- Color generator -->
- <Output name = "a" for = "color1" onforminput = "innerHTML = color1.value"> </output>
- <Input type = "range" name = "points" id = "size" min = "5" max = "20"/> <! -- Drag a bar -->
- </Body>
Effect:
Well, a simple drawing interface is ready. Below we start to write some code for drawing lines.
Copy the content to the clipboard using JavaScript Code
- $. Draw = {};
- $. Extend ($. Draw ,{
- D2 :"",
- CX :"",
- Box: "mycavas", // canvas id
- BoxObj: function () {// canvas object
- This. CX = document. getElementById (this. Box );
- },
- D2: function () {// 2d drawing object
- This. D2 = this. CX. getContext ("2d ");
- },
- Cricle: function (x, y, r, color) {// circle
- If (this. D2 ){
- This. D2.beginPath ();
- This. D2.arc (x, y, r, 0, Math. PI * 2, true );
- This. D2.closePath ();
- If (color ){
- This. D2.fillStyle = color;
- }
- This. D2.fill ();
- }
- },
- Init: function () {// Initialization
- This. BoxObj ();
- This. D2 ();
- }
- })
I believe you can understand the simple code here. It mainly involves creating an object, including creating a canvas, creating a 2d Object, the circle method, and the object initialization method.
Next, call this object on the front-end html page/p>
Check the Code:
Copy the content to the clipboard using JavaScript Code
- Var color = "#000000"; // initialization color
- Var size = 5; // initialization size
- Document. getElementById ('color1'). onchange = function (){
- Color = this. value;
- };
- Document. getElementById ('SIZE'). onchange = function (){
- Size = this. value;
- };
- $. Draw. init (); // Initialization
- Var tag = false; // controls the current state of the mouse and enables the ink switch.
- Var current ={}; // stores the point when the mouse is pressed.
- Document. onmousedown = function (option) {// click the event
- Current. x = option. x;
- Current. y = option. y;
- $. Draw. Cricle (option. x, option. y, size, color );
- Tag = true;
- }
- Document. onmouseup = function () {// mouse lift event
- Tag = false;
- }
- Document. onmousemove = function (option) {// move the cursor
- If (tag ){
- If (size> = 0 ){
- $. Draw. Cricle (option. x, option. y, size, color );
- }
- }
- }
This code mainly has the following meanings:
1. Capture the change event of the Color Space and the drag bar control to obtain the corresponding color and size values and store them for the following draw lines.
2. initialize the Drawing Object
3. Capture the mouse press, lift and move the event, the key is that a switch can control the ink
Okay, just a simple graffiti sheet. Just upload my calligraphy:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.