Home Web Front-end H5 Tutorial Graffiti board is simply implemented to write your own drawing board in Html5

Graffiti board is simply implemented to write your own drawing board in Html5

May 21, 2017 pm 02:46 PM
html5 drawing board

This article mainly teaches you how to use Html5 to write your own drawing board, perform painting, color adjustment and other operations. Interested friends can refer to it

Recently learned about the powerful drawing function of html5 It surprised me, so I wrote a gadget---a graffiti pad, which can perform functions such as: drawing, changing colors, and adjusting the brush size.

html5 drawing can be divided into points, lines, surfaces, circles, Pictures, etc., points and lines, these are the basic points of all plane effects. With these two things, there is nothing that cannot be drawn, only unexpected algorithms.

Let’s start with the code:

html

XML/HTML CodeCopy the content to the clipboard

1

2

3

4

5

6

<body style="cursor:pointer"

<canvas id="mycavas" width="1024" height="400" style="border:solid 4px #000000"></canvas><!--画布--> 

<input type="color" id="color1" name="color1"/><!--设色器--> 

<output name="a" for="color1" onforminput="innerHTML=color1.value"></output> 

 <input type="range" name="points" id="size" min="5" max="20" /><!--拖动条--> 

</body>

Copy after login

Effect:

Okay, a simple drawing interface is ready, let’s start writing some line drawing code

JavaScript CodeCopy content to clipboard

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

$.Draw = {};  

$.extend($.Draw, {  

 D2: "",  

CX:"",  

 Box: "mycavas",//画布id  

 BoxObj:function(){//画布对象  

 this.CX=document.getElementById(this.Box);  

 },  

 D2:function(){//2d绘图对象  

this.D2 = this.CX.getContext("2d");  

 },  

Cricle: function (x, y, r, color) {//画圆  

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 () {//初始化  

this.BoxObj();  

this.D2();  

}    

})

Copy after login

I believe everyone can understand the simple code here, the main thing is to create a Object, including creating canvas, creating 2D object, drawing circle method, and object initialization method.

Go to the front html page to call this object/p>

Look at the code:

JavaScript CodeCopy the content to the clipboard

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

var color = "#000000";//初始化颜色  

 var size = 5;//初始化尺寸  

document.getElementById(&#39;color1&#39;).onchange = function () {  

color = this.value;  

 };  

 document.getElementById(&#39;size&#39;).onchange = function () {  

 size = this.value;  

 };  

 $.Draw.init();//初始化  

 var tag = false;//控制鼠标当前状态并起到开启油墨开关的作用  

  var current = {};//存储鼠标按下时候的点  

document.onmousedown = function (option) {//鼠标按下事件  

 current.x = option.x;  

current.y = option.y;  

 $.Draw.Cricle(option.x, option.y, size, color);  

 tag = true;  

 }  

 document.onmouseup = function () {//鼠标抬起事件  

 tag = false;  

 }  

document.onmousemove = function (option) {//鼠标移动事件  

 if (tag) {  

 if (size >= 0) {  

 $.Draw.Cricle(option.x, option.y, size, color);  

  }   

 }  

 }

Copy after login

This code mainly has the following meanings

1. Capture the change event of the color space and drag bar control to obtain the corresponding color and size values, stored for the following line drawing

2. Initialize the drawing object

3. Capture the mouse press, lift and move events, the key is a switch that can control the ink

Okay, a simple graffiti board will be fine, with my calligraphy on it:

The above is the detailed content of Graffiti board is simply implemented to write your own drawing board in Html5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

See all articles