SVG拖動繪製矩形程式碼例項
分享一段程式碼例項,通過拖動就可以繪製一個矩形。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> body{ margin:0; padding:0; overflow:hidden; } svg{ position:absolute; width:100%; height:100%; left:0; top:0; } path{ fill:none; stroke:red; } </style> <script> window.onload = function() { var svg = document.querySelector("svg"); svg.setAttribute("width", innerWidth); svg.setAttribute("height", innerHeight); var rect; svg.addEventListener("mousedown", function(e) { rect = document.createElementNS(this.namespaceURI, "path"); rect._d = "M" + e.clientX + "," + e.clientY; rect._d2 = "H" + e.clientX + "Z"; this.appendChild(rect); }, false); svg.addEventListener("mousemove", function(e) { if (rect) { var d = rect._d + "H" + e.clientX + "V" + e.clientY + rect._d2; rect.setAttribute("d", d); } }, false); svg.addEventListener("mouseup", function(e) { rect = null; }, false); svg.addEventListener("mouseleave", function(e) { rect = null; }, false); } </script> </head> <body> <svg></svg> </body> </html>
相關文章
- svg繪製半圓程式碼例項SVG
- canvas繪製圓角矩形程式碼例項Canvas
- JavaScript拖動滑鼠繪製矩形方框JavaScript
- SVG <rect> 繪製矩形SVG
- canvas繪製矩形並填充顏色程式碼例項Canvas
- HTML5利用canvas繪製矩形程式碼例項HTMLCanvas
- SVG 繪製圓角矩形SVG
- canvas繪製扇形程式碼例項Canvas
- canvas繪製星星程式碼例項Canvas
- canvas 繪製雞蛋程式碼例項Canvas
- jQuerydiv元素拖動效果程式碼例項jQuery
- canvas繪製網格程式碼例項Canvas
- css繪製圓形程式碼例項CSS
- canvas繪製笑臉程式碼例項Canvas
- canvas繪製圓環效果程式碼例項Canvas
- 可以拖動的div塊程式碼例項
- SVG繪製矩形缺角問題解決方案SVG
- canvas繪製米字旗程式碼例項Canvas
- canvas繪製箭頭效果程式碼例項Canvas
- jQuery繪製網格效果程式碼例項jQuery
- CSS3繪製菱形程式碼例項CSSS3
- canvas繪製拋物線程式碼例項Canvas線程
- canvas繪製機器貓程式碼例項Canvas
- canvas繪製夜空小屋效果程式碼例項Canvas
- canvas繪製鐘錶效果程式碼例項Canvas
- canvas繪製橢圓效果程式碼例項Canvas
- canvas繪製熊貓頭像程式碼例項Canvas
- svg生成矩形效果程式碼SVG
- 移動端div塊拖動效果程式碼例項
- css3繪製月牙效果程式碼例項CSSS3
- canvas繪製魚吃豆效果程式碼例項Canvas
- canvas繪製雪花飄落效果程式碼例項Canvas
- js按住滑鼠繪製線條程式碼例項JS
- javascript繪製心形圖案程式碼例項JavaScript
- canvas繪製圓形鐘錶程式碼例項Canvas
- canvas繪製實心圓形程式碼例項Canvas
- canvas繪製網狀圓圈程式碼例項Canvas
- 表格拖動滾動條標題固定程式碼例項