這個繪圖工具,我還沒做完,不過已經實現了總架構,以及常見的簡易圖形繪製功能:
1,可以繪製直線,圓,矩形,正多邊形【已完成】
2,填滿顏色和描邊顏色的選擇【已完成】
3,描邊和填滿功能的選擇【已完成】
後續版本:
橡皮擦,座標系,線形設置,箭頭,其他流程圖形,裁切與調整圖形。 。 。 。 。
終極目標:
流程繪製軟體
我是之前看見一位朋友在我的部落格中留言說:
非常感謝這個朋友,今天終於抽出時間完成非常非常小的雛形!
完整的雛形代碼,請自行打開,複製到本地測試.
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>windows简易画图工具 - by ghostwu</title> </head> <body> <div class="paint"> <div class="paint-header"> <ul> <li class="active">形状</li> <li>颜色</li> <li>绘制类型</li> <li>线条宽度</li> <li>橡皮擦</li> </ul> </div> <div class="paint-body"> <div class="siderbar"> <div class="item active" data-type="paint-shape"> <ul> <li class="active" data-role="line">线条</li> <li data-role="circle">圆形</li> <li data-role="rect">矩形</li> <li data-role="polygon">正多边形</li> <li data-role="arrow">箭头</li> </ul> </div> <div class="item" data-type="paint-color"> <ul> <li data-role="strokeStyle"> <input type="color" data-role="strokeStyle"> </li> <li data-role="fillStyle"> <input type="color" data-role="fillStyle"> </li> </ul> </div> <div class="item" data-type="paint-type"> <ul> <li data-role="stroke">描边</li> <li data-role="fill">填充</li> </ul> </div> <div class="item" data-type="paint-line"> <ul> <li data-role="1">小号</li> <li data-role="4">中号</li> <li data-role="7">大号</li> <li> <input type="number" data-role="line-size" placeholder="请输入数字"> </li> </ul> </div> <div class="item" data-type="paint-erase"> <ul> <li> <input type="number" data-role="erase-size" placeholder="请输入数字"> </li> </ul> </div> </div> </div> </div> <script>// <![CDATA[ var oPaintBody = document.querySelector( '.paint-body' ); var oC = document.createElement( 'canvas' ); oC.setAttribute( 'width', '830' ); oC.setAttribute( 'height', '500' ); oPaintBody.appendChild( oC ); var aHeaderLi = document.querySelectorAll('.paint-header li'), aItem = document.querySelectorAll('.paint-body .item'), oCanvas = document.querySelector('.paint canvas'), oGc = oCanvas.getContext('2d'), cWidth = oCanvas.width, cHeight = oCanvas.height, curItem = aItem[0], aItemLi = curItem.querySelectorAll('li'); for (let i = 0, len = aHeaderLi.length; i < len; i++) { //头部选项卡切换功能 aHeaderLi[i].onclick = function () { for (let j = 0; j < len; j++) { aHeaderLi[j].classList.remove('active'); aItem[j].style.display = 'none'; } aItem[i].style.display = "block"; this.classList.add('active'); curItem = aItem[i]; aItemLi = curItem.querySelectorAll('li'); activeItem(aItemLi); } } activeItem(aItemLi); var role = null; function activeItem(aItemLi) { //canvas左侧选项卡切换功能 for (let i = 0, len = aItemLi.length; i < len; i++) { aItemLi[i].onclick = function () { checkPaintType(this); //绘制类型 for (let j = 0; j < len; j++) { aItemLi[j].classList.remove('active'); } this.classList.add('active'); } } } function Shape(canvasObj, cxtObj, w, h) { this.oCanvas = canvasObj; this.oGc = cxtObj; this.oCanvas.width = w; this.oCanvas.height = h; this.fillStyle = '#000'; this.storkeStyle = '#000'; this.lineWidth = 1; this.drawType = 'line'; this.paintType = 'stroke'; this.nums = 6; //正多边形的边数 } Shape.prototype = { init: function () { this.oGc.fillStyle = this.fillStyle; this.oGc.strokeStyle = this.strokeStyle; this.oGc.lineWidth = this.lineWidth; }, draw: function () { var _this = this; this.oCanvas.onmousedown = function (ev) { _this.init(); var oEvent = ev || event, startX = oEvent.clientX - _this.oCanvas.offsetLeft, startY = oEvent.clientY - _this.oCanvas.offsetTop; _this.oCanvas.onmousemove = function (ev) { _this.oGc.clearRect(0, 0, _this.oCanvas.width, _this.oCanvas.height); var oEvent = ev || event, endX = oEvent.clientX - _this.oCanvas.offsetLeft, endY = oEvent.clientY - _this.oCanvas.offsetTop; _this[_this.drawType](startX, startY, endX, endY); }; _this.oCanvas.onmouseup = function () { _this.oCanvas.onmousemove = null; _this.oCanvas.onmouseup = null; } } }, line: function (x1, y1, x2, y2) { this.oGc.beginPath(); this.oGc.moveTo(x1, y1); this.oGc.lineTo(x2, y2); this.oGc.closePath(); this.oGc.stroke(); }, circle: function (x1, y1, x2, y2) { this.oGc.beginPath(); var r = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); this.oGc.arc(x1, y1, r, 0, 2 * Math.PI, false); this.oGc.closePath(); this.oGc[this.paintType](); }, rect: function (x1, y1, x2, y2) { this.oGc.beginPath(); this.oGc.rect(x1, y1, x2 - x1, y2 - y1); this.oGc[this.paintType](); }, polygon: function (x1, y1, x2, y2) { var angle = 360 / this.nums * Math.PI / 180;//边对应的角的弧度 var r = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); this.oGc.beginPath(); for (var i = 0; i < this.nums; i++) { this.oGc.lineTo(x1 + r * Math.cos(angle * i), y1 + r * Math.sin(angle * i)); } this.oGc.closePath(); this.oGc[this.paintType](); } } var oShape = new Shape(oCanvas, oGc, cWidth, cHeight); function checkPaintType(liType) { var dataType = liType.parentNode.parentNode.dataset.type; var curType = liType.dataset.role; switch (dataType) { case 'paint-shape': //形状 oShape.drawType = curType; if (curType == 'polygon') { oShape.nums = prompt("请输入边数", 6); } oShape.draw(); break; case 'paint-color': //绘制颜色 liType.children[0].onchange = function () { oShape[this.dataset.role] = this.value; } oShape.draw(); break; case 'paint-type': //绘制类型 oShape.paintType = curType; oShape.draw(); break; } } // ]]></script> <style> .paint * { margin: 0; padding: 0; } .paint ul, .paint li { list-style: none; } .paint li:hover { cursor: pointer; } .paint { width: 980px; margin: 20px auto; border: 1px solid #ccc; overflow: hidden; } .paint .paint-header ul { width: 980px; height: 40px; line-height: 40px; border-bottom: 1px solid #ccc; } .paint .paint-header li { float: left; width: 120px; height: 40px; line-height: 40px; text-align: center; } .paint li.active { box-shadow: #666 0px 1px 8px inset; } .paint .paint-body .siderbar { float: left; width: 150px; height: 500px; } .paint .paint-body .item { width: 150px; overflow: hidden; display: none; height: 500px; border-right: 1px solid #ccc; } .paint .paint-body canvas { float: right; } .paint .paint-body .item li { height: 40px; text-align: center; border-bottom: 1px solid #ccc; line-height: 40px; } .paint .paint-body .active { display: block; } </style> </body>
關於流程設計,後期要做的功能,思路基本上已經有了,好了,圓規正傳,想要完成這個終極目標,完成一個畫圖工具應該就能接近目標了。先體驗下目前的簡易功能,下面是可以正常畫圖的,【需要你的瀏覽器支援canvas才可以額】
主要來講下目標的雛形架構:
1,圖形繪製部分,我封裝了一個類別Shape
function Shape(canvasObj, cxtObj, w, h) { this.oCanvas = canvasObj; this.oGc = cxtObj; this.oCanvas.width = w; this.oCanvas.height = h; this.fillStyle = '#000'; this.storkeStyle = '#000'; this.lineWidth = 1; this.drawType = 'line'; this.paintType = 'stroke'; this.nums = 6; //正多边形的边数 }
canvasObj: 就是canvas畫布物件
cxtObj: 就是上下文繪圖環境
# w: canvas的寬度
h: canvas的高度
fillStyle: 填滿顏色
strokeStyle: 描邊顏色
#lineWidth: 線寬
drawType: 預設為畫直線
paintType: stroke/fill 兩個選擇(描邊/填充)
2,在原型物件上擴展一個公共方法draw用來繪製圖形
draw方法,主要取得起始點座標(startX, startY),以及終點座標( endX, endY );
然後呼叫init方法來取得繪製狀態,繪製具體的圖形靠下面這個關鍵方法:
_this[_this.drawType](startX, startY, endX, endY)
這個方法的drawType會根據介面的即時選擇,變換對應的繪製類型,如:
_this['line']( startX, startY, endX, endY )
呼叫的就是oShape物件中的line,畫直線的方法
Shape.prototype = { init: function () { this.oGc.fillStyle = this.fillStyle; this.oGc.strokeStyle = this.strokeStyle; this.oGc.lineWidth = this.lineWidth; }, draw: function () { var _this = this; this.oCanvas.onmousedown = function ( ev ) { _this.init(); var oEvent = ev || event, startX = oEvent.clientX - _this.oCanvas.offsetLeft, startY = oEvent.clientY - _this.oCanvas.offsetTop; _this.oCanvas.onmousemove = function ( ev ) { _this.oGc.clearRect( 0, 0, _this.oCanvas.width, _this.oCanvas.height ); var oEvent = ev || event, endX = oEvent.clientX - _this.oCanvas.offsetLeft, endY = oEvent.clientY - _this.oCanvas.offsetTop; _this[_this.drawType](startX, startY, endX, endY); }; _this.oCanvas.onmouseup = function(){ _this.oCanvas.onmousemove = null; _this.oCanvas.onmouseup = null; } } }, line: function ( x1, y1, x2, y2 ) { this.oGc.beginPath(); this.oGc.moveTo( x1, y1 ); this.oGc.lineTo( x2, y2 ); this.oGc.closePath(); this.oGc.stroke(); }, circle : function( x1, y1, x2, y2 ){ this.oGc.beginPath(); var r = Math.sqrt( Math.pow( x2 - x1, 2 ) + Math.pow( y2 - y1, 2 ) ); this.oGc.arc( x1, y1, r, 0, 2 * Math.PI, false ); this.oGc.closePath(); this.oGc[this.paintType](); }, rect : function( x1, y1, x2, y2 ){ this.oGc.beginPath(); this.oGc.rect( x1, y1, x2 - x1, y2 - y1 ); this.oGc[this.paintType](); }, polygon : function( x1, y1, x2, y2 ){ var angle = 360 / this.nums * Math.PI / 180;//边对应的角的弧度 var r = Math.sqrt( Math.pow( x2 - x1, 2 ) + Math.pow( y2 - y1, 2 ) ); this.oGc.beginPath(); for( var i = 0; i < this.nums; i ++ ){ this.oGc.lineTo( x1 + r * Math.cos( angle * i ), y1 + r * Math.sin( angle * i ) ); } this.oGc.closePath(); this.oGc[this.paintType](); } }
3,介面操作很簡單,基本上就是選項卡的操作+html5的自訂屬性+classList的應用程式
以上是html5自己做一個類似windows的畫圖軟體的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

HTML是構建網頁結構的基石。 1.HTML定義內容結構和語義,使用、、等標籤。 2.提供語義化標記,如、、等,提升SEO效果。 3.通過標籤實現用戶交互,需注意表單驗證。 4.使用、等高級元素結合JavaScript實現動態效果。 5.常見錯誤包括標籤未閉合和屬性值未加引號,需使用驗證工具。 6.優化策略包括減少HTTP請求、壓縮HTML、使用語義化標籤等。

HTML是一種用於構建網頁的語言,通過標籤和屬性定義網頁結構和內容。 1)HTML通過標籤組織文檔結構,如、。 2)瀏覽器解析HTML構建DOM並渲染網頁。 3)HTML5的新特性如、、增強了多媒體功能。 4)常見錯誤包括標籤未閉合和屬性值未加引號。 5)優化建議包括使用語義化標籤和減少文件大小。

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

HTML的作用是通過標籤和屬性定義網頁的結構和內容。 1.HTML通過到、等標籤組織內容,使其易於閱讀和理解。 2.使用語義化標籤如、等增強可訪問性和SEO。 3.優化HTML代碼可以提高網頁加載速度和用戶體驗。

htmlisaspecifictypefodyfocusedonstructuringwebcontent,而“代碼” badlyLyCludEslanguagesLikeLikejavascriptandPytyPythonForFunctionality.1)htmldefineswebpagertuctureduseTags.2)“代碼”代碼“ code” code code code codeSpassSesseseseseseseseAwiderRangeLangeLangeforLageforLogageforLogicIctInterract

HTML、CSS和JavaScript是Web開發的三大支柱。 1.HTML定義網頁結構,使用標籤如、等。 2.CSS控製網頁樣式,使用選擇器和屬性如color、font-size等。 3.JavaScript實現動態效果和交互,通過事件監聽和DOM操作。

HTML定義網頁結構,CSS負責樣式和佈局,JavaScript賦予動態交互。三者在網頁開發中各司其職,共同構建豐富多彩的網站。

HTML適合初學者學習,因為它簡單易學且能快速看到成果。 1)HTML的學習曲線平緩,易於上手。 2)只需掌握基本標籤即可開始創建網頁。 3)靈活性高,可與CSS和JavaScript結合使用。 4)豐富的學習資源和現代工具支持學習過程。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。