HTML5技术

[js高手之路]html5 canvas动画教程 - 自己动手做一个类似windows的画图软件 - ghostwu

字号+ 作者:H5之家 来源:H5之家 2017-10-18 10:00 我要评论( )

这个绘图工具,我还没有做完,不过已经实现了总架构,以及常见的简易图形绘制功能: 1,可以绘制直线,圆,矩形,正多边形【已完成】 2,填充颜色和描边颜色的选择【已完成】 3,描边和填充功能的选择【已完成】 后续版本: 橡皮擦,坐标系,线形设置,箭头

这个绘图工具,我还没有做完,不过已经实现了总架构,以及常见的简易图形绘制功能:

1,可以绘制直线,圆,矩形,正多边形【已完成】

2,填充颜色和描边颜色的选择【已完成】

3,描边和填充功能的选择【已完成】

后续版本:

橡皮擦,坐标系,线形设置,箭头,其他流程图形,裁剪与调整图形。。。。。

终极目标:

流程绘制软件

我是之前看见一位朋友在我的博客中留言说:

非常感谢这个朋友,今天终于抽出时间完成非常非常小的雏形!

完整的雏形代码,请自行打开,复制到本地测试.

1 <head> 2 <meta charset="UTF-8"> 3 <meta content="width=device-width, initial-scale=1.0"> 4 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 5 <title>windows简易画图工具 - by ghostwu</title> 6 </head> 7 8 <body> 9 <div> 10 <div> 11 <ul> 12 <li>形状</li> 13 <li>颜色</li> 14 <li>绘制类型</li> 15 <li>线条宽度</li> 16 <li>橡皮擦</li> 17 </ul> 18 </div> 19 <div> 20 <div> 21 <div data-type="paint-shape"> 22 <ul> 23 <li data-role="line">线条</li> 24 <li data-role="circle">圆形</li> 25 <li data-role="rect">矩形</li> 26 <li data-role="polygon">正多边形</li> 27 <li data-role="arrow">箭头</li> 28 </ul> 29 </div> 30 <div data-type="paint-color"> 31 <ul> 32 <li data-role="strokeStyle"> 33 <input type="color" data-role="strokeStyle"> 34 </li> 35 <li data-role="fillStyle"> 36 <input type="color" data-role="fillStyle"> 37 </li> 38 </ul> 39 </div> 40 <div data-type="paint-type"> 41 <ul> 42 <li data-role="stroke">描边</li> 43 <li data-role="fill">填充</li> 44 </ul> 45 </div> 46 <div data-type="paint-line"> 47 <ul> 48 <li data-role="1">小号</li> 49 <li data-role="4">中号</li> 50 <li data-role="7">大号</li> 51 <li> 52 <input type="number" data-role="line-size" placeholder="请输入数字"> 53 </li> 54 </ul> 55 </div> 56 <div data-type="paint-erase"> 57 <ul> 58 <li> 59 <input type="number" data-role="erase-size" placeholder="请输入数字"> 60 </li> 61 </ul> 62 </div> 63 </div> 64 </div> 65 </div> oPaintBody = document.querySelector( '.paint-body' ); 68 var oC = document.createElement( 'canvas' ); 69 oC.setAttribute( 'width', '830' ); 70 oC.setAttribute( 'height', '500' ); 71 oPaintBody.appendChild( oC ); 72 var aHeaderLi = document.querySelectorAll('.paint-header li'), 73 aItem = document.querySelectorAll('.paint-body .item'), 74 oCanvas = document.querySelector('.paint canvas'), 75 oGc = oCanvas.getContext('2d'), 76 cWidth = oCanvas.width, cHeight = oCanvas.height, 77 curItem = aItem[0], 78 aItemLi = curItem.querySelectorAll('li'); (let i = 0, len = aHeaderLi.length; i < len; i++) { aHeaderLi[i].onclick = function () { 82 for (let j = 0; j < len; j++) { 83 aHeaderLi[j].classList.remove('active'); 84 aItem[j].style.display = 'none'; 85 } 86 aItem[i].style.display = "block"; 87 this.classList.add('active'); 88 curItem = aItem[i]; 89 aItemLi = curItem.querySelectorAll('li'); 90 activeItem(aItemLi); 91 } 92 } 93 activeItem(aItemLi); 94 var role = null; (let i = 0, len = aItemLi.length; i < len; i++) { 97 aItemLi[i].onclick = function () { (let j = 0; j < len; j++) { 100 aItemLi[j].classList.remove('active'); 101 } 102 this.classList.add('active'); 103 } 104 } 105 } Shape(canvasObj, cxtObj, w, h) { 108 this.oCanvas = canvasObj; 109 this.oGc = cxtObj; 110 this.oCanvas.width = w; 111 this.oCanvas.height = h; 112 this.fillStyle = '#000'; 113 this.storkeStyle = '#000'; 114 this.lineWidth = 1; 115 this.drawType = 'line'; 116 this.paintType = 'stroke'; } 119 120 Shape.prototype = { 121 init: function () { 122 this.oGc.fillStyle = this.fillStyle; 123 this.oGc.strokeStyle = this.strokeStyle; 124 this.oGc.lineWidth = this.lineWidth; 125 }, 126 draw: function () { 127 var _this = this; 128 this.oCanvas.onmousedown = function (ev) { 129 _this.init(); 130 var oEvent = ev || event, 131 startX = oEvent.clientX - _this.oCanvas.offsetLeft, 132 startY = oEvent.clientY - _this.oCanvas.offsetTop; 133 _this.oCanvas.onmousemove = function (ev) { 134 _this.oGc.clearRect(0, 0, _this.oCanvas.width, _this.oCanvas.height); 135 var oEvent = ev || event, 136 endX = oEvent.clientX - _this.oCanvas.offsetLeft, 137 endY = oEvent.clientY - _this.oCanvas.offsetTop; 138 _this[_this.drawType](startX, startY, endX, endY); 139 }; 140 _this.oCanvas.onmouseup = function () { 141 _this.oCanvas.onmousemove = null; 142 _this.oCanvas.onmouseup = null; 143 } 144 } 145 }, 146 line: function (x1, y1, x2, y2) { 147 this.oGc.beginPath(); 148 this.oGc.moveTo(x1, y1); 149 this.oGc.lineTo(x2, y2); 150 this.oGc.closePath(); 151 this.oGc.stroke(); 152 }, 153 circle: function (x1, y1, x2, y2) { 154 this.oGc.beginPath(); 155 var r = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); 156 this.oGc.arc(x1, y1, r, 0, 2 * Math.PI, false); 157 this.oGc.closePath(); 158 this.oGc[this.paintType](); 159 }, 160 rect: function (x1, y1, x2, y2) { 161 this.oGc.beginPath(); 162 this.oGc.rect(x1, y1, x2 - x1, y2 - y1); 163 this.oGc[this.paintType](); 164 }, 165 polygon: function (x1, y1, x2, y2) { r = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); 168 this.oGc.beginPath(); 169 for (var i = 0; i < this.nums; i++) { 170 this.oGc.lineTo(x1 + r * Math.cos(angle * i), y1 + r * Math.sin(angle * i)); 171 } 172 this.oGc.closePath(); 173 this.oGc[this.paintType](); 174 } 175 } oShape = new Shape(oCanvas, oGc, cWidth, cHeight); 178 function checkPaintType(liType) { 179 var dataType = liType.parentNode.parentNode.dataset.type; 180 var curType = liType.dataset.role; 181 switch (dataType) { oShape.drawType = curType; 184 if (curType == 'polygon') { 185 oShape.nums = prompt("请输入边数", 6); 186 } 187 oShape.draw(); 188 break; liType.children[0].onchange = function () { 191 oShape[this.dataset.role] = this.value; 192 } 193 oShape.draw(); 194 break; oShape.paintType = curType; 197 oShape.draw(); 198 break; 199 } 200 } <style> 203 .paint * { 204 margin: 0; 205 padding: 0; 206 } 207 208 .paint ul, 209 .paint li { 210 list-style: none; 211 } 212 213 .paint li:hover { 214 cursor: pointer; 215 } 216 217 .paint { 218 width: 980px; 219 margin: 20px auto; 220 border: 1px solid #ccc; 221 overflow: hidden; 222 } 223 224 .paint .paint-header ul { 225 width: 980px; 226 height: 40px; 227 line-height: 40px; 228 border-bottom: 1px solid #ccc; 229 } 230 231 .paint .paint-header li { 232 float: left; 233 width: 120px; 234 height: 40px; 235 line-height: 40px; 236 text-align: center; 237 } 238 239 .paint li.active { 240 box-shadow: #666 0px 1px 8px inset; 241 } 242 243 .paint .paint-body .siderbar { 244 float: left; 245 width: 150px; 246 height: 500px; 247 } 248 249 .paint .paint-body .item { 250 width: 150px; 251 overflow: hidden; 252 display: none; 253 height: 500px; 254 border-right: 1px solid #ccc; 255 } 256 257 .paint .paint-body canvas { 258 float: right; 259 } 260 261 .paint .paint-body .item li { 262 height: 40px; 263 text-align: center; 264 border-bottom: 1px solid #ccc; 265 line-height: 40px; 266 } 267 268 .paint .paint-body .active { 269 display: block; 270 } 271 </style> 272 </body>

View Code

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • [js高手之路]html5 canvas动画教程 - 边界判断与反弹 - ghostwu

    [js高手之路]html5 canvas动画教程 - 边界判断与反弹 - ghostwu

    2017-10-11 16:15

  • [js高手之路]html5 canvas动画教程 - 跟着鼠标移动消失的一堆炫彩小球 - ghostwu

    [js高手之路]html5 canvas动画教程 - 跟着鼠标移动消失的一堆炫彩小

    2017-10-11 16:14

  • [js高手之路]html5 canvas动画教程 - 边界判断与小球粒子模拟喷泉,散弹效果 - ghostwu

    [js高手之路]html5 canvas动画教程 - 边界判断与小球粒子模拟喷泉,散

    2017-10-10 18:01

  • [js高手之路]html5 canvas动画教程 - 重力、摩擦力、加速、抛物线运动 - ghostwu

    [js高手之路]html5 canvas动画教程 - 重力、摩擦力、加速、抛物线运

    2017-10-10 09:01

网友点评