Table of Contents
HTML code:
Javascript code:
Home Web Front-end H5 Tutorial Detailed explanation of the super cool HTML5 Canvas network drawing board code example

Detailed explanation of the super cool HTML5 Canvas network drawing board code example

Mar 09, 2017 pm 03:54 PM

Detailed explanation of the super cool HTML5 Canvas network drawing board code example

In today's HTML tutorial, we are going to learn how to use HTML5 Canvas to implement a super cool and simple network Drawing board function. In this tutorial, we can choose the type of brush, the size of the brush, and the color of the brush. Of course, as a drawing board, it also needs many functions. Here are only the most basic drawing functions. You can implement complex elements such as rectangles and ellipses by yourself. of drawing.

Detailed explanation of the super cool HTML5 Canvas network drawing board code example

You can also view the DEMO demonstration here

Let’s briefly analyze the principles and principles of implementing this HTML5 web page drawing board Code, the code consists of HTML and Javascript, mainly Javascript code.

HTML code:

1

2

3

<p style="width:530px;margin:10px auto">

    <p id="canvasp"></p>

</p>

Copy after login

The HTML code is very simple. It just constructs a canvas container, and our drawing board is generated here.

Javascript code:

First we define the style of the drawing board through a set of variables, and initialize some data:

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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

var canvas;

var context;

var canvasWidth = 490;

var canvasHeight = 220;

var padding = 25;

var lineWidth = 8;

var colorPurple = "#cb3594";

var colorGreen = "#659b41";

var colorYellow = "#ffcf33";

var colorBrown = "#986928";

var outlineImage = new Image();

var crayonImage = new Image();

var markerImage = new Image();

var eraserImage = new Image();

var crayonBackgroundImage = new Image();

var markerBackgroundImage = new Image();

var eraserBackgroundImage = new Image();

var crayonTextureImage = new Image();

var clickX = new Array();

var clickY = new Array();

var clickColor = new Array();

var clickTool = new Array();

var clickSize = new Array();

var clickDrag = new Array();

var paint = false;

var curColor = colorPurple;

var curTool = "crayon";

var curSize = "normal";

var mediumStartX = 18;

var mediumStartY = 19;

var mediumImageWidth = 93;

var mediumImageHeight = 46;

var drawingAreaX = 111;

var drawingAreaY = 11;

var drawingAreaWidth = 267;

var drawingAreaHeight = 200;

var toolHotspotStartY = 23;

var toolHotspotHeight = 38;

var sizeHotspotStartY = 157;

var sizeHotspotHeight = 36;

var sizeHotspotWidthObject = new Object();

sizeHotspotWidthObject.huge = 39;

sizeHotspotWidthObject.large = 25;

sizeHotspotWidthObject.normal = 18;

sizeHotspotWidthObject.small = 16;

var totalLoadResources = 8;

var curLoadResNum = 0;

Copy after login

Then we start to prepare the canvas, that is, initialize the Canvas Object:

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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

function prepareCanvas()

{

    // Create the canvas (Neccessary for IE because it doesn&#39;t know what a canvas element is)

    var canvasp = document.getElementById(&#39;canvasp&#39;);

    canvas = document.createElement(&#39;canvas&#39;);

    canvas.setAttribute(&#39;width&#39;, canvasWidth);

    canvas.setAttribute(&#39;height&#39;, canvasHeight);

    canvas.setAttribute(&#39;id&#39;, &#39;canvas&#39;);

    canvasp.appendChild(canvas);

    if(typeof G_vmlCanvasManager != &#39;undefined&#39;) {

        canvas = G_vmlCanvasManager.initElement(canvas);

    }

    context = canvas.getContext("2d"); // Grab the 2d canvas context

    // Note: The above code is a workaround for IE 8 and lower. Otherwise we could have used:

    //     context = document.getElementById(&#39;canvas&#39;).getContext("2d");

 

    // Load images

    // -----------

    crayonImage.onload = function() { resourceLoaded();

    };

    crayonImage.src = "images/crayon-outline.png";

    //context.drawImage(crayonImage, 0, 0, 100, 100);

 

    markerImage.onload = function() { resourceLoaded();

    };

    markerImage.src = "images/marker-outline.png";

 

    eraserImage.onload = function() { resourceLoaded();

    };

    eraserImage.src = "images/eraser-outline.png";   

 

    crayonBackgroundImage.onload = function() { resourceLoaded();

    };

    crayonBackgroundImage.src = "images/crayon-background.png";

 

    markerBackgroundImage.onload = function() { resourceLoaded();

    };

    markerBackgroundImage.src = "images/marker-background.png";

 

    eraserBackgroundImage.onload = function() { resourceLoaded();

    };

    eraserBackgroundImage.src = "images/eraser-background.png";

 

    crayonTextureImage.onload = function() { resourceLoaded();

    };

    crayonTextureImage.src = "images/crayon-texture.png";

 

    outlineImage.onload = function() { resourceLoaded();

    };

    outlineImage.src = "images/watermelon-duck-outline.png";

 

    // Add mouse events

    // ----------------

    $(&#39;#canvas&#39;).mousedown(function(e)

    {

        // Mouse down location

        var mouseX = e.pageX - this.offsetLeft;

        var mouseY = e.pageY - this.offsetTop;

 

        if(mouseX < drawingAreaX) // Left of the drawing area

        {

            if(mouseX > mediumStartX)

            {

                if(mouseY > mediumStartY && mouseY < mediumStartY + mediumImageHeight){

                    curColor = colorPurple;

                }else if(mouseY > mediumStartY + mediumImageHeight && mouseY < mediumStartY + mediumImageHeight * 2){

                    curColor = colorGreen;

                }else if(mouseY > mediumStartY + mediumImageHeight * 2 && mouseY < mediumStartY + mediumImageHeight * 3){

                    curColor = colorYellow;

                }else if(mouseY > mediumStartY + mediumImageHeight * 3 && mouseY < mediumStartY + mediumImageHeight * 4){

                    curColor = colorBrown;

                }

            }

        }

        else if(mouseX > drawingAreaX + drawingAreaWidth) // Right of the drawing area

        {

            if(mouseY > toolHotspotStartY)

            {

                if(mouseY > sizeHotspotStartY)

                {

                    var sizeHotspotStartX = drawingAreaX + drawingAreaWidth;

                    if(mouseY < sizeHotspotStartY + sizeHotspotHeight && mouseX > sizeHotspotStartX)

                    {

                        if(mouseX < sizeHotspotStartX + sizeHotspotWidthObject.huge){

                            curSize = "huge";

                        }else if(mouseX < sizeHotspotStartX + sizeHotspotWidthObject.large + sizeHotspotWidthObject.huge){

                            curSize = "large";

                        }else if(mouseX < sizeHotspotStartX + sizeHotspotWidthObject.normal +

                        sizeHotspotWidthObject.large + sizeHotspotWidthObject.huge){

                            curSize = "normal";

                        }else if(mouseX < sizeHotspotStartX + sizeHotspotWidthObject.small +

                        sizeHotspotWidthObject.normal + sizeHotspotWidthObject.large + sizeHotspotWidthObject.huge){

                            curSize = "small";                       

                        }

                    }

                }

                else

                {

                    if(mouseY < toolHotspotStartY + toolHotspotHeight){

                        curTool = "crayon";

                    }else if(mouseY < toolHotspotStartY + toolHotspotHeight * 2){

                        curTool = "marker";

                    }else if(mouseY < toolHotspotStartY + toolHotspotHeight * 3){

                        curTool = "eraser";

                    }

                }

            }

        }

        else if(mouseY > drawingAreaY && mouseY < drawingAreaY + drawingAreaHeight)

        {

            // Mouse click location on drawing area

        }

        paint = true;

        addClick(mouseX, mouseY, false);

        redraw();

    });

 

    $(&#39;#canvas&#39;).mousemove(function(e){

        if(paint==true){

            addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);

            redraw();

        }

    });

 

    $(&#39;#canvas&#39;).mouseup(function(e){

        paint = false;

          redraw();

    });

 

    $(&#39;#canvas&#39;).mouseleave(function(e){

        paint = false;

    });

}

Copy after login

Looks very complicated. The front is mainly to initialize the background image of the canvas, and the back is mainly to initialize the brush events, such as click, mouseup, mouseleave and other mouse events.

The following are the main methods of draw:

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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

function redraw()

{

    // Make sure required resources are loaded before redrawing

    if(curLoadResNum < totalLoadResources){ return; }

 

    clearCanvas();

 

    var locX;

    var locY;

    if(curTool == "crayon")

    {

        // Draw the crayon tool background

        context.drawImage(crayonBackgroundImage, 0, 0, canvasWidth, canvasHeight);

 

        // Purple

        locX = (curColor == colorPurple) ? 18 : 52;

        locY = 19;

 

        context.beginPath();

        context.moveTo(locX + 41, locY + 11);

        context.lineTo(locX + 41, locY + 35);

        context.lineTo(locX + 29, locY + 35);

        context.lineTo(locX + 29, locY + 33);

        context.lineTo(locX + 11, locY + 27);

        context.lineTo(locX + 11, locY + 19);

        context.lineTo(locX + 29, locY + 13);

        context.lineTo(locX + 29, locY + 11);

        context.lineTo(locX + 41, locY + 11);

        context.closePath();

        context.fillStyle = colorPurple;

        context.fill();   

 

        if(curColor == colorPurple){

            context.drawImage(crayonImage, locX, locY, mediumImageWidth, mediumImageHeight);

        }else{

            context.drawImage(crayonImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);

        }

 

        // Green

        locX = (curColor == colorGreen) ? 18 : 52;

        locY += 46;

 

        context.beginPath();

        context.moveTo(locX + 41, locY + 11);

        context.lineTo(locX + 41, locY + 35);

        context.lineTo(locX + 29, locY + 35);

        context.lineTo(locX + 29, locY + 33);

        context.lineTo(locX + 11, locY + 27);

        context.lineTo(locX + 11, locY + 19);

        context.lineTo(locX + 29, locY + 13);

        context.lineTo(locX + 29, locY + 11);

        context.lineTo(locX + 41, locY + 11);

        context.closePath();

        context.fillStyle = colorGreen;

        context.fill();   

 

        if(curColor == colorGreen){

            context.drawImage(crayonImage, locX, locY, mediumImageWidth, mediumImageHeight);

        }else{

            context.drawImage(crayonImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);

        }

 

        // Yellow

        locX = (curColor == colorYellow) ? 18 : 52;

        locY += 46;

 

        context.beginPath();

        context.moveTo(locX + 41, locY + 11);

        context.lineTo(locX + 41, locY + 35);

        context.lineTo(locX + 29, locY + 35);

        context.lineTo(locX + 29, locY + 33);

        context.lineTo(locX + 11, locY + 27);

        context.lineTo(locX + 11, locY + 19);

        context.lineTo(locX + 29, locY + 13);

        context.lineTo(locX + 29, locY + 11);

        context.lineTo(locX + 41, locY + 11);

        context.closePath();

        context.fillStyle = colorYellow;

        context.fill();   

 

        if(curColor == colorYellow){

            context.drawImage(crayonImage, locX, locY, mediumImageWidth, mediumImageHeight);

        }else{

            context.drawImage(crayonImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);

        }

 

        // Yellow

        locX = (curColor == colorBrown) ? 18 : 52;

        locY += 46;

 

        context.beginPath();

        context.moveTo(locX + 41, locY + 11);

        context.lineTo(locX + 41, locY + 35);

        context.lineTo(locX + 29, locY + 35);

        context.lineTo(locX + 29, locY + 33);

        context.lineTo(locX + 11, locY + 27);

        context.lineTo(locX + 11, locY + 19);

        context.lineTo(locX + 29, locY + 13);

        context.lineTo(locX + 29, locY + 11);

        context.lineTo(locX + 41, locY + 11);

        context.closePath();

        context.fillStyle = colorBrown;

        context.fill();   

 

        if(curColor == colorBrown){

            context.drawImage(crayonImage, locX, locY, mediumImageWidth, mediumImageHeight);

        }else{

            context.drawImage(crayonImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);

        }

    }

    else if(curTool == "marker")

    {

        // Draw the marker tool background

        context.drawImage(markerBackgroundImage, 0, 0, canvasWidth, canvasHeight);

 

        // Purple

        locX = (curColor == colorPurple) ? 18 : 52;

        locY = 19;

 

        context.beginPath();

        context.moveTo(locX + 10, locY + 24);

        context.lineTo(locX + 10, locY + 24);

        context.lineTo(locX + 22, locY + 16);

        context.lineTo(locX + 22, locY + 31);

        context.closePath();

        context.fillStyle = colorPurple;

        context.fill();   

 

        if(curColor == colorPurple){

            context.drawImage(markerImage, locX, locY, mediumImageWidth, mediumImageHeight);

        }else{

            context.drawImage(markerImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);

        }

 

        // Green

        locX = (curColor == colorGreen) ? 18 : 52;

        locY += 46;

 

        context.beginPath();

        context.moveTo(locX + 10, locY + 24);

        context.lineTo(locX + 10, locY + 24);

        context.lineTo(locX + 22, locY + 16);

        context.lineTo(locX + 22, locY + 31);

        context.closePath();

        context.fillStyle = colorGreen;

        context.fill();   

 

        if(curColor == colorGreen){

            context.drawImage(markerImage, locX, locY, mediumImageWidth, mediumImageHeight);

        }else{

            context.drawImage(markerImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);

        }

 

        // Yellow

        locX = (curColor == colorYellow) ? 18 : 52;

        locY += 46;

 

        context.beginPath();

        context.moveTo(locX + 10, locY + 24);

        context.lineTo(locX + 10, locY + 24);

        context.lineTo(locX + 22, locY + 16);

        context.lineTo(locX + 22, locY + 31);

        context.closePath();

        context.fillStyle = colorYellow;

        context.fill();   

 

        if(curColor == colorYellow){

            context.drawImage(markerImage, locX, locY, mediumImageWidth, mediumImageHeight);

        }else{

            context.drawImage(markerImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);

        }

 

        // Yellow

        locX = (curColor == colorBrown) ? 18 : 52;

        locY += 46;

 

        context.beginPath();

        context.moveTo(locX + 10, locY + 24);

        context.lineTo(locX + 10, locY + 24);

        context.lineTo(locX + 22, locY + 16);

        context.lineTo(locX + 22, locY + 31);

        context.closePath();

        context.fillStyle = colorBrown;

        context.fill();   

 

        if(curColor == colorBrown){

            context.drawImage(markerImage, locX, locY, mediumImageWidth, mediumImageHeight);

        }else{

            context.drawImage(markerImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);

        }

    }

    else if(curTool == "eraser")

    {

        context.drawImage(eraserBackgroundImage, 0, 0, canvasWidth, canvasHeight);

        context.drawImage(eraserImage, 18, 19, mediumImageWidth, mediumImageHeight);   

    }else{

        alert("Error: Current Tool is undefined");

    }

 

    if(curSize == "small"){

        locX = 467;

    }else if(curSize == "normal"){

        locX = 450;

    }else if(curSize == "large"){

        locX = 428;

    }else if(curSize == "huge"){

        locX = 399;

    }

    locY = 189;

    context.beginPath();

    context.rect(locX, locY, 2, 12);

    context.closePath();

    context.fillStyle = &#39;#333333&#39;;

    context.fill();   

 

    // Keep the drawing in the drawing area

    context.save();

    context.beginPath();

    context.rect(drawingAreaX, drawingAreaY, drawingAreaWidth, drawingAreaHeight);

    context.clip();

 

    var radius;

    var i = 0;

    for(; i < clickX.length; i++)

    {       

        if(clickSize[i] == "small"){

            radius = 2;

        }else if(clickSize[i] == "normal"){

            radius = 5;

        }else if(clickSize[i] == "large"){

            radius = 10;

        }else if(clickSize[i] == "huge"){

            radius = 20;

        }else{

            alert("Error: Radius is zero for click " + i);

            radius = 0;   

        }

 

        context.beginPath();

        if(clickDrag[i] && i){

            context.moveTo(clickX[i-1], clickY[i-1]);

        }else{

            context.moveTo(clickX[i], clickY[i]);

        }

        context.lineTo(clickX[i], clickY[i]);

        context.closePath();

 

        if(clickTool[i] == "eraser"){

            //context.globalCompositeOperation = "destination-out"; // To erase instead of draw over with white

            context.strokeStyle = &#39;white&#39;;

        }else{

            //context.globalCompositeOperation = "source-over";    // To erase instead of draw over with white

            context.strokeStyle = clickColor[i];

        }

        context.lineJoin = "round";

        context.lineWidth = radius;

        context.stroke();

 

    }

    //context.globalCompositeOperation = "source-over";// To erase instead of draw over with white

    context.restore();

 

    // Overlay a crayon texture (if the current tool is crayon)

    if(curTool == "crayon"){

        context.globalAlpha = 0.4; // No IE support

        context.drawImage(crayonTextureImage, 0, 0, canvasWidth, canvasHeight);

    }

    context.globalAlpha = 1; // No IE support

 

    // Draw the outline image

    context.drawImage(outlineImage, drawingAreaX, drawingAreaY, drawingAreaWidth, drawingAreaHeight);

}

Copy after login

In fact, HTML5 still requires a lot of Javascript support, but Canvas is very good and allows you to draw graphics and animations freely on it. This web drawing board based on HTML5 Canvas is a good example. Source code download>>







#

The above is the detailed content of Detailed explanation of the super cool HTML5 Canvas network drawing board code example. 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)

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.

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.

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.

See all articles