最近做了一个人员轨迹运动的需求, 来分享下代码.

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>点轨迹运动</title><style>.canvas-box {width: 1400px;height: 800px;background: url("http://b-ssl.duitang.com/uploads/item/201202/14/20120214144725_HQ5mX.jpg") 0 0 no-repeat;background-size: 100% 100%;}</style>
</head>
<body>
<div><div class="canvas-box"><canvas id="canvas" width="1400" height="800">不支持canvas</canvas></div>
</div><script type="text/javascript">
let timer = 0
let cvs = document.getElementById('canvas')
let ctx = cvs.getContext('2d')
let points = [] //已经运动过的数据
let animatePoint = { x: 0, y: 0 } //当前运动点位置
let nextPointIndex = 1 //下一个运动点的index//源数据
let routes = [{ x: 12, y: 666 },{ x: 190, y: 120 },{ x: 354, y: 456 },{ x: 269, y: 85 },{ x: 456, y: 41 },{ x: 789, y: 120 },{ x: 876, y: 354 },
]
//开始运动
init(routes)function init (routes) {animatePoint = { x: 0, y: 0 }nextPointIndex = 1ctx.clearRect(0, 0, cvs.clientWidth, cvs.clientHeight)if (routes.length > 0) {points.push({x: routes[0].x,y: routes[0].y,})animatePoint = {x: routes[0].x,y: routes[0].y,}drawPoint(routes[0].x, routes[0].y)}if (routes.length > 1) {this.startTimer()}
}function startTimer () {startTime = new Date().getTime()if (routes.length > 1) {this.clearTimer()this.animate()}
}function clearTimer () {window.cancelAnimationFrame(timer)
}function animate () {timer = window.requestAnimationFrame(animate)startMove()
}function startMove () {let targetDistance = Math.sqrt(Math.pow(routes[nextPointIndex - 1].x - routes[nextPointIndex].x, 2) +Math.pow(routes[nextPointIndex - 1].y - routes[nextPointIndex].y, 2),)let currentDistance = Math.sqrt(Math.pow(routes[nextPointIndex - 1].x - animatePoint.x, 2) +Math.pow(routes[nextPointIndex - 1].y - animatePoint.y, 2),)if (currentDistance >= targetDistance) {//利用运动距离与目标距离, 判断运动的点是否超过下一个目标点, 超过了就重置下一个点startTime = new Date().getTime()points[nextPointIndex] = {x: routes[nextPointIndex].x,y: routes[nextPointIndex].y,}animatePoint = {x: routes[nextPointIndex].x,y: routes[nextPointIndex].y,}nextPointIndex++clearTimer()if (nextPointIndex <= routes.length - 1) {setTimeout(() => {startTimer()}, 500)}//重绘ctx.clearRect(0, 0, cvs.clientWidth, cvs.clientHeight)drawPolygon(points)drawPoint(animatePoint.x, animatePoint.y, 'yellow')return}if (nextPointIndex > routes.length - 1) {//轨迹运动结束后, 关闭timerclearTimer()animatePoint = {x: routes[routes.length - 1].x,y: routes[routes.length - 1].y,}} else {let speed = 0.25let deltaTime = new Date().getTime() - startTimelet deltaDistance = deltaTime * speedlet rate = deltaDistance / targetDistancelet x =routes[nextPointIndex - 1].x +(routes[nextPointIndex].x - routes[nextPointIndex - 1].x) * ratelet y =routes[nextPointIndex - 1].y +(routes[nextPointIndex].y - routes[nextPointIndex - 1].y) * rateanimatePoint.x = xanimatePoint.y = y//重绘, 将animatePoint设为轨迹的下一个点, 以达到动态的效果points[nextPointIndex] = {x: animatePoint.x,y: animatePoint.y,}ctx.clearRect(0, 0, cvs.clientWidth, cvs.clientHeight)drawPolygon(points)drawPoint(animatePoint.x, animatePoint.y, 'yellow')}
}function drawPoint (x, y, color) {//绘制点ctx.fillStyle = color || '#1DEFFF'ctx.strokeStyle = '#fff'if (!color) {ctx.shadowColor = '#FFF'ctx.shadowBlur = 10}ctx.beginPath()ctx.arc(x, y, 5, Math.PI * 2, 0, true)ctx.closePath()ctx.stroke()ctx.fill()
}function drawPolygon (points) {//绘制轨迹ctx.clearRect(0, 0, cvs.clientWidth, cvs.clientHeight)ctx.strokeStyle = '#1DEFFF'ctx.shadowColor = '#1DEFFF'ctx.shadowBlur = 10ctx.lineWidth = 4ctx.beginPath()ctx.moveTo(points[0].x, points[0].y)let i = 1,len = points.lengthfor (; i < len; i++) {ctx.lineTo(points[i].x, points[i].y)}ctx.stroke()let j = 0for (; j < len - 1; j++) {drawPoint(points[j].x, points[j].y)}
}
</script>
</body>
</html>

canvas轨迹运动, 利用向量实现点匀速运动相关推荐

  1. css3运动后留下轨迹尾巴_利用CSS+JS实现唯美星空轨迹运动效果

    先给大家分享效果图: 给大家分享一个使用CSS+JS实现的唯美星空轨迹运动效果, 这样的效果不输给Flash   .相关代码如下: cloth body { background: #000; } i ...

  2. canvas进阶——贝塞尔公式推导与物体跟随复杂曲线的轨迹运动

    写在最前 在之前的这篇文章中我们提到了对于贝塞尔公式的运用.本次分享一下如何推导贝塞尔公式以及附一个简单的?即小球跟随曲线轨迹运动. 欢迎关注我的博客,不定期更新中-- 效果预览 demo地址 对于如 ...

  3. 小折腾:JavaScript与元素间的抛物线轨迹运动

    小折腾:JavaScript与元素间的抛物线轨迹运动 这篇文章发布于 2013年12月30日,星期一,20:40,归类于 js实例. 阅读 61147 次, 今日 55 次 by zhangxinxu ...

  4. Android Animation实现元素在屏幕上按照指定轨迹运动,以及出现NullPointerException的解决方案

    因项目需要,在Android中实现了一个动画,当在Activity中点击特定按钮时,会在屏幕上添加一个ImageView,并按照指定的起点.终点,沿着特定的轨迹运动(例如直线). 实现方法 实现思路是 ...

  5. android滑屏轨迹运动,Android Animation实现元素在屏幕上按照指定轨迹运动,以及出现NullPointerException的解决方案...

    因项目需要,在Android中实现了一个动画,当在Activity中点击特定按钮时,会在屏幕上添加一个ImageView,并按照指定的起点.终点,沿着特定的轨迹运动(例如直线). 实现方法 实现思路是 ...

  6. Canvas入门-利用Canvas绘制好玩的电子时钟

    在这之前 你需要了解一下方法的使用: beginPath() closePath() moveTo() lineTo() fill() stroke() fillRect() clearRect() ...

  7. canvas 平滑运动_什么是电视上的运动平滑?人们为什么讨厌它?

    canvas 平滑运动 Willy Barton/Shutterstock.com威利·巴顿/Shutterstock.com If you've just bought a new TV, you ...

  8. PreScan中对象沿预设轨迹运动的若干方式介绍

    环境:PreScan 8.5.0 1. 绘制预设轨迹 手动绘制预设轨迹,分为三段.第一段前进,第二段后退,第三段圆弧.     最终得到的轨迹如图所示.最后将车辆拖放至轨迹上. 2. 考虑车辆动力学的 ...

  9. BMapGL实现地图轨迹运动(地图视角不变)

    BMapGL实现地图轨迹 想要实现: 轨迹运动地图视角不能随之改变 有icon标识运动当前的位位置 总的说就是在BMapGl里实现BMap的轨迹运动功能 由于数据之间的经纬度之间的距离太遥远,不能使用 ...

最新文章

  1. python难不难学-超级适合新手学习的python教程,入门其实不难?
  2. Java 一维数组作为参数和返回值
  3. Python + Steamlit 快速开发可视化 web 页面!
  4. Vue2.x中vuex的使用方法及应用时的项目文件结构设计以完整demo实例解释
  5. StyleAI:白度-物理上,怎样才算白?
  6. 十四、Canny边缘提取
  7. 前端学习(1776):前端调试之indexDB原理和查看
  8. [分布式训练] 单机多卡的正确打开方式:PyTorch
  9. MySQL:基本命令
  10. rust进水器怎么用_喷丝板钻孔速度太慢怎么办?用这款增速器,效率提高35%
  11. 帮别人改的代码 我也不知道他爬的什么 记录下
  12. 如何从另一个Shell脚本调用Shell脚本?
  13. 汇编语言——偏移地址超过有效地址FFFFH
  14. Android 手写签名 (图片合成)
  15. ssm学习笔记之spring
  16. 宝塔面板权限问题导致php上传文件失败
  17. ios 应用闪退原因
  18. 毕业至今,微博写给自己的感悟的话
  19. MATLAB之拉氏变换
  20. [ZZ]浅谈中国B2C珍珑棋局

热门文章

  1. 电脑所有的浏览器都上不了网怎么解决
  2. UAF—metasequoia_2020_summon
  3. 赞奇科技英特尔共图视觉计算“云”上大作为
  4. Django框架学习记录(3)
  5. HTML CSS整理笔记(建议收藏)
  6. php exif_read_data orientation,PHP exif_read_data Illegal IFD size
  7. hdu 5312 Sequence(数学推导——三角形数)
  8. 两年数据对比柱形图_【系列课程】用Excel进行数据可视化组合图表的制作lt;二gt;...
  9. Greeplum+GPTest
  10. 未来科技感UI界面设计欣赏