问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

如何利用 js+html 绘制 带箭头的 弧线

发布网友 发布时间:2022-04-23 19:13

我来回答

6个回答

热心网友 时间:2022-04-07 11:08

纯js不好实现,但是配合html,css就有了近似的办法.下面提供一个解决方案,已知两个点的坐标,为它们画一条带箭头的弧线.

<html>

<head>

<meta http-equiv = "content-type" content = "text/html; charset = gb2312">

<title>箭头弧线</title>

<style type = "text/css">

span { position: absolute; width: 5px; height: 5px; background-color: #0000ff; display: block; border-radius: 50%; }

</style>

<script language = "javascript">

function locateO () {

var x0 = parseInt (spnA.style.left, 10), y0 = parseInt (spnA.style.top, 10), x1 = parseInt (spnB.style.left, 10), y1 = parseInt (spnB.style.top, 10), horizontalDistance = Math.abs (x1 - x0), verticalDistance = Math.abs (y1 - y0), x = Math.min (x0, x1) + horizontalDistance / 2, y = Math.min (y0, y1) + verticalDistance / 2, distance = Math.sqrt (Math.pow (horizontalDistance, 2) + Math.pow (verticalDistance, 2)), array = new Array (x0, y0, x1, y1, distance);

spnO.style.left = x;

spnO.style.top = y;

return array;

}

function getRadian (x0, y0, x1, y1) {

var horizontalDistance = Math.abs (x1 - x0), verticalDistance = Math.abs (y1 - y0), rate = horizontalDistance == 0 ? 0 : verticalDistance / horizontalDistance, radian;

if (y1 < y0) {

if (x1 > x0) {

radian = - Math.atan (rate);

} else if (x1 == x0) {

radian = - Math.PI / 2;

} else {

radian = - (Math.PI - Math.atan (rate));

}

} else if (y1 == y0) {

radian = x1 > left ? 0 : - Math.PI;

} else if (x1 < x0) {

radian = - (Math.PI + Math.atan (rate));

} else if (x1 == x0) {

radian = - Math.PI * 3 / 2;

} else {

radian = - 2 * Math.PI + Math.atan (rate);

}

return radian;

}

function getAngle (radian) {

var angle = - radian * 180 / Math.PI;

return angle;

}

function slantArc (x0, y0, x1, y1) {

var radian = getRadian (x0, y0, x1, y1), angle = 360 - getAngle (radian);

cnvArc.style.transform = "rotate(" + angle + "deg)";

}

function drawArc (x0, y0, width) {

var context = cnvArc.getContext ("2d"), radius = width / 2, height = radius / 2;

cnvArc.width = width + 10;

cnvArc.height = height;

cnvArc.style.left = x0;

cnvArc.style.top = y0 - height;

context.ellipse (radius + 5, height, radius, height / 2, 0, 0, Math.PI * 2);

context.strokeStyle = "#00ff00";

context.stroke ();

}

function hex (figure) {

return figure.toString (16);

}

function zeroize (cluster) {

if (cluster.length < 2) {

cluster = 0 + cluster;

}

return cluster;

}

function getColour (red, green, blue) {

return "#" + zeroize (hex (red)) + zeroize (hex (green)) + zeroize (hex (blue));

}

function printArc () {

var width = cnvArc.width, height = cnvArc.height, context = cnvArc.getContext ("2d"), imageData = context.getImageData (0, 0, width, height), data = imageData.data, coordinates = new Array (), cluster = "", i, red, green, blue, colour, index, x, y;

for (i = 0; i < data.length; i += 4) {

red = data [i];

green = data [i + 1];

blue = data [i + 2];

colour = getColour (red, green, blue);

index = i / 4;

y = parseInt (index / width, 10);

x = index % width;

if (x == 0) {

//console.log (y + "\n" + cluster);

cluster = "";

}

cluster += x + ":" + colour + " ";

if (colour == "#00ff00") {

coordinates.push (new Array (x, y));

}

}

return coordinates;

}

function sortCoordinates (coordinates, direction) {

var i = 0, flag, j, coordinate;

do {

flag = false;

for (j = 0; j < coordinates.length - 1 - i; j ++) {

if (direction && (coordinates [j] [0] > coordinates [j + 1] [0] || coordinates [j] [0] == coordinates [j + 1] [0] && coordinates [j] [1] > coordinates [j + 1] [1]) || ! direction && (coordinates [j] [0] < coordinates [j + 1] [0] || coordinates [j] [0] == coordinates [j + 1] [0] && coordinates [j] [1] > coordinates [j + 1] [1])) {

coordinate = coordinates [j];

coordinates [j] = coordinates [j + 1];

coordinates [j + 1] = coordinate;

flag = true;

}

}

i ++;

} while (flag);

}

function drawArrow (x0, y0, x1, y1) {

var context = cnvArc.getContext ("2d"), colour = "#00ff00", angle = (x1 - x0) / (y1 - y0);

context.strokeStyle = colour;

context.fillStyle = colour;

context.setLineDash ([3, 3]);

context.beginPath ();

context.arc (x0, y0, 1, 0, 2 * Math.PI);

context.translate (0, 0, 0);

context.moveTo (x0, y0);

context.lineTo (x1, y1);

context.fill ();

context.stroke ();

context.save ();

context.translate (x1, y1);

angle = Math.atan (angle);

context.rotate ((y1 >= y0 ? 0 : Math.PI) - angle);

context.lineTo (- 3, - 9);

context.lineTo (0, - 3);

context.lineTo (3, - 9);

context.lineTo (0, 0);

context.fill ();

context.restore ();

context.closePath ();

}

function initialize () {

var array = locateO (), x0 = array [0], y0 = array [1], x1 = array [2], y1 = array [3], width = array [4], direction = x1 > x0, coordinates, length, coordinate0, coordinate1, x2, y2, x3, y3;

drawArc (x0, y0, width);

coordinates = printArc ();

length = coordinates.length;

sortCoordinates (coordinates, direction);

coordinate0 = coordinates [length - 2];

x2 = coordinate0 [0];

y2 = coordinate0 [1];

coordinate1 = coordinates [length - 1];

x3 = coordinate1 [0];

y3 = coordinate1 [1];

drawArrow (x2, y2, x3, y3);

slantArc (x0, y0, x1, y1);

}

</script>

</head>

<body style = "margin: 0;" onload = "initialize ()">

<span id = "spnA" style = "left: 50px; top: 150px;"></span>

<span id = "spnB" style = "left: 850px; top: 350px;"></span>

<span id = "spnO"></span>

<canvas id = "cnvArc" style = "position: absolute; background-color: rgb(255, 255, 0, 0.01); z-index: 1; transform-origin: 0 100%;"></canvas>

</body>

</html>

复制进来的代码都不带缩进的吗?

该代码纯手写,不从第三方处盗用.仅供参考.

热心网友 时间:2022-04-07 12:26

这个没办法绘制, 只能用canvas或者图片实现

热心网友 时间:2022-04-07 14:01

用图片或者canvas的方式进行。

热心网友 时间:2022-04-07 15:52

使用js图形库,比如zrender等

热心网友 时间:2022-04-07 18:00

像我就会ps画一张,然后放上去,哈哈哈

热心网友 时间:2022-04-07 20:25

给个参考http://www.jb51.net/html5/32168.html
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
将一张薄纸的一端靠近下嘴唇,另一端自然下垂,沿纸的上方吹气,手中的纸... ...双手轻轻捏住一张薄纸,将它放在下嘴唇底下,然后沿着纸片的表面用力... 用双手将一张纸的边缘拉紧,放在嘴唇中间(嘴唇和纸的边缘相距约1毫米... 请问工贸公司的经营范围 沈阳德诺工贸有限公司怎么样? update简介 update的创作背景 update专辑评价 办房产证面积多了交多少钱 办证面积比预售合同面积多契税 html求助 (如图)我想让右边的箭头靠在图片右边代码要怎么改呢 html 中如何让鼠标箭头移到某个div上就变为手型,移出后再变为原型... 如何把html的select控件的下拉箭头改成图 问:HTML中如下图的这种带上下箭头选择框的效果如何实现? html两个div画箭头 关于HTML的问题,图中那个箭头符号怎么打,我找不到TAT html里这种箭头标签外框的样式是怎么做的? html箭头代码 html 箭头符号的颜色怎么修改 html中图片中箭头指的那个向下的小箭头,是怎么做出来的! 如何用html在图片右面加一个剪头? html中这种箭头怎么实现? 杭州溺水男孩在ICU躺着,他的母亲难过发声,说了什么? ICU护士应具备的条件有哪些? ICU病房牵手照泪目,明星离婚霸屏,经历怎样的“考验”才能白头? 泡网吧半年进重症监护室是经历了什么呢? 从生死营救到成功转出ICU,排长冒小驰经历了什么? 癌症患者自述:不想活了,真的太痛了!癌痛,到底是什么感受? 吴孟达病情突然恶化,已转入ICU,每四小时打一次止疼药,他经历了什么? 你听过最恐怖的『挤痘痘』并发症都有哪些? html图片两边有箭头可以左右翻 24寸的行李箱可以免费托运吗? 24寸行李箱可以托运吗? 我的行李箱是24寸,托运的话是免费的吗,没有超过20kg 24寸的行李箱托运需要钱吗 飞机上可以带旅行箱吗24寸行李箱超重怎么托运收费吗? - 信息提示 失恋33天里,黄小仙拿的那个白色布袋是什么牌子?图案像个心 百幕大三角的资料 蒙古族白食和红食哪个是最高礼遇 白色垃圾对环境有什么危害? 白色污染,给环境带来了什么危害? 屏幕上方出现这样的白色闪屏 什么鬼 白色的衣服上有小黑点,大概是汗渍,怎么去掉? 霹雳布袋戏人物「白衣剑少」的图片收集。 白色垃圾对地球造成了什么危害 口袋装有4个只有颜色不同的球,其中2个红色,1个白色,1个黑色,搅匀后从布袋里2次摸出红色球的概率 白色污染有哪些危害? 红岩小萝卜头离开白公馆后的命运 白色垃圾