Home Web Front-end H5 Tutorial Html5 simply implements the sample code of graffiti board

Html5 simply implements the sample code of graffiti board

Mar 16, 2017 pm 01:17 PM
code Graffiti

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.

First upload the code:

html

<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:

Html5 simply implements the sample code of graffiti board

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

$.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. It mainly creates an object, including creating a canvas and creating 2d. Object, circle drawing method, and object initialization method.

Next, go to the front html page to call this object/p>

Look at the code:

var color = "#000000";//初始化颜色   
        var size = 5;//初始化尺寸   
        document.getElementById('color1').onchange = function () {   
            color = this.value;   
        };   
        document.getElementById('size').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, and store them for the following line drawing

2. Initialize the drawing object

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

Okay, a simple graffiti pad will be enough, with my calligraphy on it:

Html5 simply implements the sample code of graffiti board

Related articles:

6 hand-drawn graffiti button effects based on pure CSS3

How to use html5 Complete google graffiti animation with css3

Implement adjustable brush color/thickness/eraser graffiti board based on javascript html5 canvas

The above is the detailed content of Html5 simply implements the sample code of graffiti board. 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 Article

Hot Article

Hot Article Tags

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)

What to do if the blue screen code 0x0000001 occurs What to do if the blue screen code 0x0000001 occurs Feb 23, 2024 am 08:09 AM

What to do if the blue screen code 0x0000001 occurs

How to solve win7 driver code 28 How to solve win7 driver code 28 Dec 30, 2023 pm 11:55 PM

How to solve win7 driver code 28

Solve the 'error: expected initializer before 'datatype'' problem in C++ code Solve the 'error: expected initializer before 'datatype'' problem in C++ code Aug 25, 2023 pm 01:24 PM

Solve the 'error: expected initializer before 'datatype'' problem in C++ code

The computer frequently blue screens and the code is different every time The computer frequently blue screens and the code is different every time Jan 06, 2024 pm 10:53 PM

The computer frequently blue screens and the code is different every time

Resolve code 0xc000007b error Resolve code 0xc000007b error Feb 18, 2024 pm 07:34 PM

Resolve code 0xc000007b error

What does the blue screen code 0x000000d1 represent? What does the blue screen code 0x000000d1 represent? Feb 18, 2024 pm 01:35 PM

What does the blue screen code 0x000000d1 represent?

GE universal remote codes program on any device GE universal remote codes program on any device Mar 02, 2024 pm 01:58 PM

GE universal remote codes program on any device

Detailed explanation of the causes and solutions of 0x0000007f blue screen code Detailed explanation of the causes and solutions of 0x0000007f blue screen code Dec 25, 2023 pm 02:19 PM

Detailed explanation of the causes and solutions of 0x0000007f blue screen code

See all articles