H5 canvas制作画图

我不是女神ヾ 2022-06-10 06:37 413阅读 0赞



1概述

随着前端技术的发展,canvas使用的越累越多,而且越来越多的H5技术被应用到实际开发中,尤其是js插件更是无处不用,今天主要是分享一下如何去使用canvas在H5界面中制一个超级简单的画图工具。

2效果图如下:

Center

3主要功能

支持选择画笔的颜色

支持选择画笔的线条粗细

4实现方式

首先,我们需要一个容器,来将我的画图工具放入网页中

  1. <**divclass="content"**>

然后我们要进行布局,布局详细如下,大体分为header、body、footer三部分:

<divclass=”header”>
<divclass=”left”>
<imgclass=”choosePan”src=”img/p1.png”/>
<imgclass=”pan”src=”img/p2.gif”/>
<imgclass=”pan”src=”img/p3.gif”/>
</div>

  1. <**divclass="right"**>
  2. <**imgclass="chooseLineWidth"src="img/w3.png"**/>
  3. <**imgclass="lineWidth"src="img/w2.gif"**/>
  4. <**imgclass="lineWidth"src="img/w1.gif"**/>
  5. </**div**>

</div>

<divclass=”body”>
<canvasid=”can”height=”500”width=”500”></canvas>
<divclass=”show”>
<divclass=”divKeep”></div>
<divclass=”divKeep”></div>
<divclass=”divKeep”></div>
<divclass=”divKeep”></div>
<divclass=”divKeep”></div>
<divclass=”divKeep”></div>
<inputclass=”rightButton”type=”button”value=”清空”/>
</div>

</div>
<divclass=”footer”>
<inputtype=”button”value=”保存”/>
<inputtype=”button”value=”清空”/>
</div>

  1. 接着就是添加方法和事件:
  2. 1.选择笔的方法:
  3. //这里传参,0表示第一个笔,1表示第二个笔….
  4. <img class="choosePan" src="img/p1.png" οnclick="choosePan(this,0)"/>
  5. <img class="pan" src="img/p2.gif" οnclick="choosePan(this,1)"/>
  6. <img class="pan" src="img/p3.gif" οnclick="choosePan(this,2)"/>
  7. //选择笔:

function*choosePan(cell,index){
**
pan*=index; //改变鼠标的样式(这里可以单独拿出一个方法,此处为了简略) switch(*pan*){ case**0:

canvas.style.cursor=“url(‘img/p1.png’) 0 48,auto”;
break;
case1:
canvas.style.cursor=“url(‘img/p2.gif’) 0 48,auto”;
break;
case2:
canvas.style.cursor=“url(‘img/p3.gif’) 0 48,auto”;
break;
}

//清除笔的颜色
clearPan();

//选择笔的颜色
cell.className=“choosePan”;
}

function*clearPan(){
varpans=document.getElementsByClassName(“choosePan”);
for(vari=0;i<pans.length;i++){
pans[i].className=*”pan”
;
}
}

  1. 2.选择线条的粗细:
  2. 参数同上
  3. <img class="chooseLineWidth" src="img/w3.png" οnclick="chooseLineWidth(this,0)"/>
  4. <img class="lineWidth" src="img/w2.gif" οnclick="chooseLineWidth(this,1)"/>
  5. <img class="lineWidth" src="img/w1.gif" οnclick="chooseLineWidth(this,2)"/>

function*chooseLineWidth(cell,index){
**
line=index; clearLineWidth*();
cell.
className=“chooseLineWidth”;
}
functionclearLineWidth(){ varpans=document.getElementsByClassName(“chooseLineWidth”); for(vari=0;i<pans.length;i++){
pans[i].
className=“lineWidth”**;
}
}

  1. 保存和清空画布:
  1. <inputtype=”button”value=”保存”οnclick=”*keep()/>
    <inputtype=”button”value=”清空”οnclick=”
    clearCanvas()*”/>

function*clearCanvas(){
**
context*.strokeStyle=“white”;
*
context*.clearRect(0,0,500,500);
}

function*keep(){
varurl=**
canvas*.toDataURL(); varmyKeep=document.getElementsByClassName(“divKeep”); varindex=-1; for(vari=0;i<myKeep.length;i++){ if(!myKeep[i].innerHTML){
index=i;
break;
}
}
if(index==-1){
alert(
“缓存已满!请先清除缓存。”);
}
else{ varwidth=myKeep[index].offsetWidth;
myKeep[index].
innerHTML=“**;
}
}

4.清空右侧缓存区域:

  1. <inputclass=”rightButton”type=”button”value=”清空”οnclick=”*clearKeep()*”/>

function*clearKeep(){
varmyKeep=document.getElementsByClassName(“divKeep”);
for(vari=0;i<myKeep.length;i++){
myKeep[i].innerHTML=*””
;
}
}

  1. 5 开始画图:

onload=function(){
canvas=document.getElementById(“can”);
context=canvas.getContext(“2d”);
varx;
vary;
varwrite=false;
document.onmousemove=function(e){
x= e.clientX;
y= e.clientY-100;
};

  1. ***canvas***.onmousedown=**function**(e)\{
  2. **var**color=**"red"**;
  3. **var**lineWidth=**"1"**;
  4. **switch**(***pan***)\{
  5. **case**0:
  6. color=**"red"**;
  7. **break**;
  8. **case**1:
  9. color=**"green"**;
  10. **break**;
  11. **case**2:
  12. color=**"blue"**;
  13. **break**;
  14. \}
  15. **switch**(***line***)\{
  16. **case**0:
  17. lineWidth=**"1"**;
  18. **break**;
  19. **case**1:
  20. lineWidth=**"4"**;
  21. **break**;
  22. **case**2:
  23. lineWidth=**"7"**;
  24. **break**;
  25. \}
  26. ***context***.closePath();
  27. ***context***.beginPath();
  28. ***context***.moveTo(x,y);
  29. ***context***.**strokeStyle**=color;
  30. ***context***.**lineWidth**=lineWidth;
  31. write=**true**;
  32. \};
  33. ***canvas***.onmousemove=**function**(e)\{
  34. **if**(write)\{
  35. ***context***.lineTo(x,y);
  36. ***context***.stroke();
  37. \}
  38. \};
  39. **document**.onmouseup=**function**(e)\{
  40. write=**false**;
  41. \}

};

  1. 5. 主要思路分析:
  2. ①、首先如果想在画布上画出线条,我们需要知道笔(鼠标)的位置信息,所以可以进入画布在记录,也可以当鼠标进入网页就开始记录,需要用到onmousemove
  3. ②、其次是需要按下鼠标拖动时开始画图,松开鼠标停止画图,所以需要有一个开关来控制鼠标,取一个布尔变量就可以,同时需要用到onmousedownonmousemoveonmouseup事件
  4. ③、保存画布上的图,到旁边的缓存区
  5. ④、清空画布
  6. ⑤、清空缓存区
  7. 6. 全部代码如下:

<!DOCTYPEhtml>
<htmllang=”en”>
<head>
<metacharset=”UTF-8”>
<title></title>
<style>
*{
margin:0;
padding:0;
}
#can{
border:2px solid red;
position:absolute;
left:0;
top:100px;
cursor:url(“img/p1.png”)048,auto;
}
.content{
margin-top:10px;
}
.header{
height:50px;
margin:10px;
}
.header img{
cursor:pointer;
}
.pan{
border:1px solid transparent;
}
.choosePan{
border:1px solid red;
}
.lineWidth{
border:1px solid transparent;
}
.chooseLineWidth{
border:1px solid red;
}
.left, .right{
width:200px;
display:inline;
margin-right:150px;
}

  1. .**body**\{
  2. **width**:1100**px**;
  3. \}
  4. .**show**\{
  5. **width**:550**px**;
  6. **height**:500**px**;
  7. **float**:**right**;
  8. \}
  9. .**show div**\{
  10. **width**:160**px**;
  11. **height**:160**px**;
  12. **margin**:60**px**10**px**0 10**px**;
  13. **border**:1**px solid black**;
  14. **float**:**left**;
  15. \}
  16. **input**\{
  17. **width**:80**px**;
  18. **height**:30**px**;
  19. \}
  20. .**rightButton**\{
  21. **margin**:15**px**;
  22. **float**:**right**;
  23. \}
  24. </**style**>
  25. <**script**>
  26. **var*canvas***;
  27. **var*context***;
  28. **var*pan***;
  29. **var*line***;
  30. onload=**function**()\{
  31. ***canvas***=**document**.getElementById(**"can"**);
  32. ***context***=***canvas***.getContext(**"2d"**);
  33. **var**x;
  34. **var**y;
  35. **var**write=**false**;
  36. **document**.onmousemove=**function**(e)\{
  37. x= e.**clientX**;
  38. y= e.**clientY**\-100;
  39. \};
  40. ***canvas***.onmousedown=**function**(e)\{
  41. **var**color=**"red"**;
  42. **var**lineWidth=**"1"**;
  43. **switch**(***pan***)\{
  44. **case**0:
  45. color=**"red"**;
  46. **break**;
  47. **case**1:
  48. color=**"green"**;
  49. **break**;
  50. **case**2:
  51. color=**"blue"**;
  52. **break**;
  53. \}
  54. **switch**(***line***)\{
  55. **case**0:
  56. lineWidth=**"1"**;
  57. **break**;
  58. **case**1:
  59. lineWidth=**"4"**;
  60. **break**;
  61. **case**2:
  62. lineWidth=**"7"**;
  63. **break**;
  64. \}
  65. ***context***.closePath();
  66. ***context***.beginPath();
  67. ***context***.moveTo(x,y);
  68. ***context***.**strokeStyle**=color;
  69. ***context***.**lineWidth**=lineWidth;
  70. write=**true**;
  71. \};
  72. ***canvas***.onmousemove=**function**(e)\{
  73. **if**(write)\{
  74. ***context***.lineTo(x,y);
  75. ***context***.stroke();
  76. \}
  77. \};
  78. **document**.onmouseup=**function**(e)\{
  79. write=**false**;
  80. \}
  81. \};
  82. **function***choosePan*(cell,index)\{
  83. ***pan***=index;
  84. **switch**(***pan***)\{
  85. **case**0:
  86. ***canvas***.**style**.**cursor**=**"url('img/p1.png') 0 48,auto"**;
  87. **break**;
  88. **case**1:
  89. ***canvas***.**style**.**cursor**=**"url('img/p2.gif') 0 48,auto"**;
  90. **break**;
  91. **case**2:
  92. ***canvas***.**style**.**cursor**=**"url('img/p3.gif') 0 48,auto"**;
  93. **break**;
  94. \}
  95. *clearPan*();
  96. cell.**className**=**"choosePan"**;
  97. \}
  98. **function***clearPan*()\{
  99. **var**pans=**document**.getElementsByClassName(**"choosePan"**);
  100. **for**(**var**i=0;i<pans.**length**;i\++)\{
  101. pans\[i\].**className**=**"pan"**;
  102. \}
  103. \}
  104. **function***chooseLineWidth*(cell,index)\{
  105. ***line***=index;
  106. *clearLineWidth*();
  107. cell.**className**=**"chooseLineWidth"**;
  108. \}
  109. **function***clearLineWidth*()\{
  110. **var**pans=**document**.getElementsByClassName(**"chooseLineWidth"**);
  111. **for**(**var**i=0;i<pans.**length**;i\++)\{
  112. pans\[i\].**className**=**"lineWidth"**;
  113. \}
  114. \}
  115. **function***clearCanvas*()\{
  116. ***context***.**strokeStyle**=**"white"**;
  117. ***context***.clearRect(0,0,500,500);
  118. \}
  119. **function***keep*()\{
  120. **var**url=***canvas***.toDataURL();
  121. **var**myKeep=**document**.getElementsByClassName(**"divKeep"**);
  122. **var**index=-1;
  123. **for**(**var**i=0;i<myKeep.**length**;i\++)\{
  124. **if**(!myKeep\[i\].**innerHTML**)\{
  125. index=i;
  126. **break**;
  127. \}
  128. \}
  129. **if**(index==-1)\{
  130. alert(**"缓存已满!请先清除缓存。"**);
  131. \}**else**\{
  132. **var**width=myKeep\[index\].**offsetWidth**;
  133. myKeep\[index\].**innerHTML**=**"<img src="**\+url\+**"width="**\+width\+**"\\>"**;
  134. \}
  135. \}
  136. **function***clearKeep*()\{
  137. **var**myKeep=**document**.getElementsByClassName(**"divKeep"**);
  138. **for**(**var**i=0;i<myKeep.**length**;i\++)\{
  139. myKeep\[i\].**innerHTML**=**""**;
  140. \}
  141. \}
  142. </**script**>

</head>
<body>
<divclass=”content”>
<divclass=”header”>
<divclass=”left”>
<imgclass=”choosePan”src=”img/p1.png”οnclick=”*choosePan(this,0)/>
<imgclass=”pan”src=”img/p2.gif”οnclick=”
choosePan(this,1)/>
<imgclass=”pan”src=”img/p3.gif”οnclick=”
choosePan(this,2)/>
</*div
>

  1. <**divclass="right"**>
  2. <**imgclass="chooseLineWidth"src="img/w3.png"οnclick="***chooseLineWidth*(**this**,0)**"**/>
  3. <**imgclass="lineWidth"src="img/w2.gif"οnclick="***chooseLineWidth*(**this**,1)**"**/>
  4. <**imgclass="lineWidth"src="img/w1.gif"οnclick="***chooseLineWidth*(**this**,2)**"**/>
  5. </**div**>
  6. </**div**>
  7. <**divclass="body"**>
  8. <**canvasid="can"height="500"width="500"**></**canvas**>
  9. <**divclass="show"**>
  10. <**divclass="divKeep"**></**div**>
  11. <**divclass="divKeep"**></**div**>
  12. <**divclass="divKeep"**></**div**>
  13. <**divclass="divKeep"**></**div**>
  14. <**divclass="divKeep"**></**div**>
  15. <**divclass="divKeep"**></**div**>
  16. <**inputclass="rightButton"type="button"value="清空"οnclick="***clearKeep*()**"**/>
  17. </**div**>
  18. </**div**>
  19. <**divclass="footer"**>
  20. <**inputtype="button"value="保存"οnclick="***keep*()**"**/>
  21. <**inputtype="button"value="清空"οnclick="***clearCanvas*()**"**/>
  22. </**div**>

</div>

</body>
</html>

到目前为止,完成了画图的基本功能,而且简单易使用。

发表评论

表情:
评论列表 (有 0 条评论,413人围观)

还没有评论,来说两句吧...

相关阅读

    相关 H5 canvas制作画图

     1概述   随着前端技术的发展,canvas使用的越累越多,而且越来越多的H5技术被应用到实际开发中,尤其是js插件更是无处不用,今天主要是分享一下如何去使用canv