admin管理员组文章数量:1278653
With a canvas you can draw a line with javascript like this,
<html>
<canvas id="myCanvas" width="500" height="300" style="border:1px solid #d3d3d3;"></canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(20,20);
ctx.lineTo(100,100);
ctx.stroke();
</script>
</html>
How can i do the same thing, but by using a div tag instead of a canvas? The reason i want to do this is beacuse the canvas does not seem to work on IE and i know that Google graphs make use of div tags and not canvases to draw graphs, so it might be possible.
I tried replacing the canvas with a div, but it does not work.
With a canvas you can draw a line with javascript like this,
<html>
<canvas id="myCanvas" width="500" height="300" style="border:1px solid #d3d3d3;"></canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(20,20);
ctx.lineTo(100,100);
ctx.stroke();
</script>
</html>
How can i do the same thing, but by using a div tag instead of a canvas? The reason i want to do this is beacuse the canvas does not seem to work on IE and i know that Google graphs make use of div tags and not canvases to draw graphs, so it might be possible.
I tried replacing the canvas with a div, but it does not work.
Share Improve this question asked Jan 7, 2014 at 10:37 Jason SamuelsJason Samuels 9716 gold badges24 silver badges42 bronze badges 4- For a very simple line you could use the HR tag and add a style to that. – Nigel Ellis Commented Jan 7, 2014 at 10:39
- 1 Canvas is supported since IE 9 (caniuse./#search=canvas). You can't do that with a div. – Rob Commented Jan 7, 2014 at 10:39
- Here is a fiddle replicating what you have done with the canvas thing above – Pete Commented Jan 7, 2014 at 11:05
- walterzorn.de/en/jsgraphics/jsgraphics_e.htm give a try – Amarnath Balasubramanian Commented Jan 7, 2014 at 11:25
5 Answers
Reset to default 8Using Jquery:
<div id='rPaper'></div>
Jquery
x1 = 50, y1 = 50,
x2 = 350, y2 = 50;
drawnode(x1, y1);
drawnode(x2, y2);
drawline(x1, y1, x2, y2);
function drawnode(x, y) {
var ele = ""
var style = "";
style += "position:absolute;";
style += "z-index:100;"
ele += "<div class='relNode' style=" + style + ">";
ele += "<span> Test Node</span>"
ele += "<div>"
$('#rPaper').show();
var node = $(ele).appendTo('#rPaper');
var width = node.width();
var height = node.height();
var centerX = width / 2;
var centerY = height / 2;
var startX = x - centerX;
var startY = y - centerY;
node.css("left", startX).css("top", startY);
}
function drawline(ax, ay, bx, by) {
console.log('ax: ' + ax);
console.log('ay: ' + ay);
console.log('bx: ' + bx);
console.log('by: ' + by);
if (ax > bx) {
bx = ax + bx;
ax = bx - ax;
bx = bx - ax;
by = ay + by;
ay = by - ay;
by = by - ay;
}
console.log('ax: ' + ax);
console.log('ay: ' + ay);
console.log('bx: ' + bx);
console.log('by: ' + by);
var angle = Math.atan((ay - by) / (bx - ax));
console.log('angle: ' + angle);
angle = (angle * 180 / Math.PI);
console.log('angle: ' + angle);
angle = -angle;
console.log('angle: ' + angle);
var length = Math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by));
console.log('length: ' + length);
var style = ""
style += "left:" + (ax) + "px;"
style += "top:" + (ay) + "px;"
style += "width:" + length + "px;"
style += "height:1px;"
style += "background-color:black;"
style += "position:absolute;"
style += "transform:rotate(" + angle + "deg);"
style += "-ms-transform:rotate(" + angle + "deg);"
style += "transform-origin:0% 0%;"
style += "-moz-transform:rotate(" + angle + "deg);"
style += "-moz-transform-origin:0% 0%;"
style += "-webkit-transform:rotate(" + angle + "deg);"
style += "-webkit-transform-origin:0% 0%;"
style += "-o-transform:rotate(" + angle + "deg);"
style += "-o-transform-origin:0% 0%;"
style += "-webkit-box-shadow: 0px 0px 2px 2px rgba(0, 0, 0, .1);"
style += "box-shadow: 0px 0px 2px 2px rgba(0, 0, 0, .1);"
style += "z-index:99;"
$("<div style='" + style + "'></div>").appendTo('#rPaper');
}
Demo
// right top -> left bottom
x1 = 850, y1 = 150,
x2 = 550, y2 = 250;
drawnode(x1, y1);
drawnode(x2, y2);
drawline(x1, y1, x2, y2);
Demo
// right bottom -> left top
x1 = 750, y1 = 150,
x2 = 550, y2 = 50;
drawnode(x1, y1);
drawnode(x2, y2);
drawline(x1, y1, x2, y2);
Demo
// left top -> right bottom
x1 = 150, y1 = 150,
x2 = 350, y2 = 350;
drawnode(x1, y1);
drawnode(x2, y2);
drawline(x1, y1, x2, y2);
Demo
// vertical line: down -> up
x1 = 150, y1 = 350,
x2 = 150, y2 = 150;
drawnode(x1, y1);
drawnode(x2, y2);
drawline(x1, y1, x2, y2);
Demo
Rendered some lines using div tag. Please refer below code
<div style="width: 112px; height: 47px; border-bottom: 1px solid black; -webkit-transform: translateY(-20px) translateX(5px) rotate(27deg); position: absolute;/* top: -20px; */"></div>
<div style="width: 112px; height: 47px; border-bottom: 1px solid black; -webkit-transform: translateY(20px) translateX(5px) rotate(-26deg); position: absolute;top: -33px;left: -13px;"></div>
hope it helps.
fiddle link :
http://jsfiddle/NATnr/45/
Thanks,
Siva
You can create a tag like p, or div, set width whatever you wanted, add top border and append it to the div within you want to create a line.
You can use CSS3 2D Transforms http://jsfiddle/dmRhL/:
.line{
width: 150px;
transform: translate(50px,100px) rotate(30deg);
-ms-transform: translate(50px,100px) rotate(30deg); /* IE 9 */
-webkit-transform: translate(50px,100px) rotate(30deg); /* Safari and Chrome */
}
But looks like your problem is the old IE browsers that that will probably not help.
You can try using excanvas - a JS library that simulates the canvas element using IE's VML renderer.
Or some 2D drawing framework that will use canvas or SVG depending on browser support. You can find a full list at http://en.wikipedia/wiki/JavaScript_graphics_library
Other answers are very better than this... This uses PURE CSS, if you need.
<head>
<style>
#i1,#i2,#i3,#i4,#i5,#i6,#i7,#i8{border-right:2px solid black; height:2px;}
#i1{width:30px;}#i2{width:31px;}#i3{width:32px;}#i4{width:33px;}#i5{width:34px;}#i6{width:35px;}#i7{width:36px;}#i8{width:37px;}
</style>
</head>
<div id="i1"></div>
<div id="i2"></div>
<div id="i3"></div>
<div id="i4"></div>
<div id="i5"></div>
<div id="i6"></div>
<div id="i7"></div>
<div id="i8"></div>
And if you need to draw axis-parallel lines, you can put width or height as 1px and increase the other. fiddle here.
本文标签: htmlDrawing line in a div with JavascriptStack Overflow
版权声明:本文标题:html - Drawing line in a div with Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741271678a2369412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论