使用canvas绘制路线图

本文介绍了如何使用canvas结合接口返回的坐标数据,绘制出精确的路线轨迹图,详细阐述了绘制过程和技术要点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据接口返回的坐标点位,我们可以将这些左边点位串联起来,形成轨迹图

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>根据返回的坐标,针对当前容器的大小占比</title>
		<style type="text/css">
			.patrol-path {
				position: absolute;
				left: 0;
				top: 0;
				width: 100%;
				height: 100%;
				z-index: 1;
				background-color: #008000;
			}

			.container {
				width: 500px;
				height: 500px;
				background-color: red;
				position: relative;
			}
		</style>
	</head>
	<body>
		<div id="myApp">
			<div class="container" id="container-id">
				<div class="patrol-path">
					<canvas id="canvas" />
				</div>
			</div>
		</div>
		<script type="text/javascript">
			const canvas = document.getElementById('canvas');
			const container = document.getElementById('container-id')
			const {
				clientWidth: containerWidth,
				clientHeight: containerHeight,
			} = container;
			canvas.width = containerWidth;
			canvas.height = containerHeight;
			console.log(`${containerWidth}+容器宽度${containerHeight}`)
			const ctx = canvas.getContext("2d");
			ctx.clearRect(0, 0, containerWidth, containerHeight);
			let pins = [
				[{
						x_axes: 0.1,
						y_axes: 0.1
					},
					{
						x_axes: 0.2,
						y_axes: 0.3
					},
					{
						x_axes: 0.2,
						y_axes: 0.5
					},
					{
						x_axes: 0.4,
						y_axes: 0.6
					},
					{
						x_axes: 0.3,
						y_axes: 0.7
					},
					{
						x_axes: 0.6,
						y_axes: 0.9
					}
				]

			]
			pins.forEach((item, index) => {
				item.forEach((pathItem, pathIndex) => {
					const {
						x_axes: x,
						y_axes: y
					} = pathItem;
					if (pathIndex === 0) {
						ctx.moveTo(containerWidth * x, containerHeight * y);
					} else {
						ctx.lineTo(containerWidth * x, containerHeight * y);
					}
				});
				ctx.lineWidth = 2;
				ctx.strokeStyle = "#3170FF";
				ctx.stroke();
			});
		</script>
	</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xvzhengyang

感谢,励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值