企业官网建站 小程序开发 云逸学习园地
技术文章 许愿墙
技术文章 PHP学习 Javascript学习 DIV+CSS学习 uniapp学习 HTML学习 微信小程序、公众号知识点 VUE知识学习
您所在的位置>云逸小栈>Javascript学习>正文

javascript画轨迹图,用坐标点进行描绘

2024-08-12 18:30:22 浏览 1467

html代码

<canvas id="myCanvas" width="400" height="400"></canvas>

js代码部分

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');

// 轨迹点数组
const points = [
 { x: 50, y: 50 },
 { x: 100, y: 100 },
 { x: 150, y: 200 },
 { x: 200, y: 150 },
 { x: 250, y: 100 },
 { x: 300, y: 50 }
];

// 绘制轨迹
function drawTrajectory() {
 ctx.beginPath();
 ctx.moveTo(points[0].x, points[0].y); // 从第一个点开始
 for (let i = 1; i < points.length; i++) {
   ctx.lineTo(points[i].x, points[i].y); // 连接其他点
   ctx.strokeStyle = '#000'; // 线条颜色
   ctx.stroke(); // 绘制线条
   ctx.beginPath(); // 重置路径开始新的点
   ctx.arc(points[i].x, points[i].y, 5, 0, 2 * Math.PI); // 绘制点
   ctx.fillStyle = '#f00'; // 点的颜色
   ctx.fill(); // 填充点
 }
}

// 调用函数绘制轨迹
drawTrajectory();


网站开发者电话

18066742510

个人微信号
个人公众号
个人小程序
个人抖音