SVG Line Between Divs (multi-point)
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 | <! doctype html> < html > < head > < meta charset="utf-8"> < title >SVG Line Between Divs (multi-point)</ title > < style > html, body { margin: 0; padding: 0; } #svg { position: absolute; top: 0; left: 0; width: 100%; height: 100vh; } #line { stroke-width: 2px; stroke: black; } .point { width: 15px; height: 15px; background: #d54501; position: absolute; z-index: 10; } .dupoint { width: 15px; height: 15px; background:#ffffff; position: absolute; z-index: 10; } body { font-family: "Niramit", sans-serif; background-color: #b2ada3; } p { margin-left: 20px; margin-right: 20px; position: relative; z-index: 11; background: rgba(178, 173, 163, 0.55); } #one { top: 80px; left: 50px; } #two { top: 290px; right: 50px; } #three { top: 390px; right: 150px; } #four { top: 590px; left: 20px; } .btn { display: inline-block; background: #4e7c80; color: white; padding: 10px 15px; margin-left: 5px; cursor: pointer; position: relative; z-index: 11; line-height: 1em; } .btn:hover { background: #3b5d60; } </ style > </ head > < body > <!-- each object needs a unique ID although this could be removed for the className check --> < p >This script dynamic draws SVG lines between DOM objects and updates after window resizing. < span class="btn">Randomize points</ span ></ p > < div id="one" class="point"></ div > < div id="two" class="point"></ div > < div id="three" class="point"></ div > < div id="four" class="point"></ div > < div id="five" class="point dupoint"></ div > < div id="six" class="point dupoint"></ div > < svg id="svg"> < line id="line" class="line original" stroke-dasharray="5, 5"/> </ svg > < script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></ script > < script id="rendered-js" > const config = { target: $(".point"), line: $(".line"), delay: 40 // enter zero for live resizing }; const drawBetweenObjects = { //cmake the line makeLine: function (line, div1, div2) { var className = div1.attr('id') + div2.attr('id'); if (className.includes("undefined") !== true) {//error check $(line).clone().addClass('deleteMe').addClass(className).removeClass("original").insertAfter(line); //calculations (for legibility, these are separte vars) var x1 = div1.offset().left + div1.width() / 2; var y1 = div1.offset().top + div1.height() / 2; var x2 = div2.offset().left + div2.width() / 2; var y2 = div2.offset().top + div2.height() / 2; $("." + className).attr('x1', x1).attr('y1', y1).attr('x2', x2).attr('y2', y2); //svg attributes } else {console.error("undefined object");} }, findLines: function (search) { $(".deleteMe").remove(); //remove last set of lines $(search).each(function (index, el) { if (search.eq(index + 1).length) {//only do drawBetweenObject if there is another. drawBetweenObjects.makeLine(config.line, $(this), search.eq(index + 1)); //args order - line, div1 and div2 - the next div. } }); }, init: function () { drawBetweenObjects.findLines(config.target); //give resizing time to happen var resizeId; $(window).resize(function () { clearTimeout(resizeId); if (config.delay !== 0) { resizeId = setTimeout(doneResizing, config.delay); } else { drawBetweenObjects.findLines(config.target); } }); function doneResizing() { drawBetweenObjects.findLines(config.target); } } }; drawBetweenObjects.init(); // umimportant ugly scripting // this just randomizes the points // It's pretty ugly. $(".btn").click(function () { var heightMax = $(document).height(), widthMax = $(document).width(); function widthCalc() { return Math.floor(Math.random() * widthMax); } function heightCalc() { return Math.floor(Math.random() * heightMax); } $("#one").css({ left: widthCalc(), top: heightCalc() }); $("#four").css({ left: widthCalc(), top: heightCalc() }); $("#three").css({ right: widthCalc(), top: heightCalc() }); $("#two").css({ right: widthCalc(), top: heightCalc() }); $("#five").css({ right: widthCalc(), top: heightCalc() }); $("#six").css({ right: widthCalc(), top: heightCalc() }); drawBetweenObjects.findLines(config.target); }); //# sourceURL=pen.js from https://blog.greggant.com/posts/2018/10/16/drawing-svg-lines-between-multiple-dom-objects.html </ script > </ body > </ html > |
https://canvasjs.com/html5-javascript-line-chart/
https://konvajs.org/docs/shapes/Line_-_Spline.html
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_shapes
https://gogeometry.com/html5/index.html
https://dustinpfister.github.io/2020/03/23/canvas-example/
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!