javascript-在HTML画布内沿线对点进行动画处理

前端之家收集整理的这篇文章主要介绍了javascript-在HTML画布内沿线对点进行动画处理 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这个问题已经在这里有了答案:            >            I want to do animation of an object along a particular path                                    2个
我需要设置动画点以沿此线(航路点)移动.它需要从顶部开始,沿红线向右移动,然后向下沿蓝线移动并重复.

我尝试使用css,但无法编写点沿画布上的这条线移动

  1. var canvas = document.getElementById("myCanvas");
  2. var ctx = canvas.getContext("2d");
  3. ctx.beginPath();
  4. ctx.strokeStyle = '#FF0000';
  5. ctx.moveTo(30,0);
  6. ctx.bezierCurveTo(60,320,150,600,330);
  7. ctx.stroke();
  8. ctx.beginPath();
  9. var ctx = canvas.getContext("2d");
  10. ctx.moveTo(15,0);
  11. ctx.strokeStyle = '#000FFF';
  12. ctx.bezierCurveTo(0,340,100,350,350);
  13. ctx.stroke();
  14. ctx.beginPath();
  15. var ctx = canvas.getContext("2d");
  16. ctx.moveTo(15,0);
  17. ctx.strokeStyle = '#000FFF';
  18. ctx.lineTo(30,0);
  19. ctx.stroke();
  20. ctx.beginPath();
  21. var ctx = canvas.getContext("2d");
  22. ctx.moveTo(600,330);
  23. ctx.strokeStyle = '#000FFF';
  24. ctx.lineTo(600,350);
  25. ctx.stroke();
  1. <canvas id="myCanvas" height="350px;" width="600px"></canvas>
最佳答案
为此,您需要像这样计算Waypoint example – jsfiddle,
在您的情况下,您有一条Curve并计算Waypoint有点棘手,但没有什么不可能

>计算航点,请参阅this answer
>在Waypoints数组循环中的动画点

这是一个只有一行的小示例,您可以添加颜色或线条

jsfiddle

  1. var canvas=document.getElementById("canvas");
  2. var ctx=canvas.getContext("2d");
  3. var cw=canvas.width;
  4. var ch=canvas.height;
  5. var cBez1=[{x:30,y: 0},{x:60,y:320},{x:150,{x:600,y:330}];
  6. drawBez(cBez1);
  7. var cPoints=findCBezPoints(cBez1);
  8. setInterval(timeanim,1);
  9. var indexi =0;
  10. var opr = -1;
  11. function timeanim()
  12. {
  13. //reset view
  14. ctx.clearRect(0,cw,ch);
  15. drawBez(cBez1);
  16. //draw dot
  17. ctx.fillStyle='red';
  18. ctx.beginPath();
  19. ctx.arc(cPoints[indexi].x,cPoints[indexi].y,4,Math.PI*2);
  20. ctx.fill();
  21. if(indexi + opr > cPoints.length-2 || indexi + opr < 0){
  22. opr *= -1;
  23. }
  24. indexi += opr;
  25. }
  26. function findCBezPoints(b){
  27. var startPt=b[0];
  28. var controlPt1=b[1];
  29. var controlPt2=b[2];
  30. var endPt=b[3];
  31. var pts=[b[0]];
  32. var lastPt=b[0];
  33. var tests=5000;
  34. for(var t=0;t<=tests;t++){
  35. // calc another point along the curve
  36. var pt=getCubicBezierXYatT(b[0],b[1],b[2],b[3],t/tests);
  37. // add the pt if it's not already in the pts[] array
  38. var dx=pt.x-lastPt.x;
  39. var dy=pt.y-lastPt.y;
  40. var d=Math.sqrt(dx*dx+dy*dy);
  41. var dInt=parseInt(d);
  42. if(dInt>0 || t==tests){
  43. lastPt=pt;
  44. pts.push(pt);
  45. }
  46. }
  47. return(pts);
  48. }
  49. // Given the 4 control points on a Bezier curve
  50. // Get x,y at interval T along the curve (0<=T<=1)
  51. // The curve starts when T==0 and ends when T==1
  52. function getCubicBezierXYatT(startPt,controlPt1,controlPt2,endPt,T) {
  53. var x = CubicN(T,startPt.x,controlPt1.x,controlPt2.x,endPt.x);
  54. var y = CubicN(T,startPt.y,controlPt1.y,controlPt2.y,endPt.y);
  55. return ({
  56. x: x,y: y
  57. });
  58. }
  59. // cubic helper formula
  60. function CubicN(T,a,b,c,d) {
  61. var t2 = T * T;
  62. var t3 = t2 * T;
  63. return a + (-a * 3 + T * (3 * a - a * T)) * T + (3 * b + T * (-6 * b + b * 3 * T)) * T + (c * 3 - c * 3 * T) * t2 + d * t3;
  64. }
  65. function drawPlots(pts){
  66. ctx.fillStyle='red';
  67. // don't draw the last dot b/ its radius will display past the curve
  68. for(var i=0;i<pts.length-1;i++){
  69. ctx.beginPath();
  70. ctx.arc(pts[i].x,pts[i].y,1,Math.PI*2);
  71. ctx.fill();
  72. }
  73. }
  74. function drawBez(b){
  75. ctx.lineWidth=2;
  76. ctx.beginPath();
  77. ctx.moveTo(b[0].x,b[0].y);
  78. ctx.bezierCurveTo(b[1].x,b[1].y,b[2].x,b[2].y,b[3].x,b[3].y);
  79. ctx.stroke();
  80. }
  1. <canvas id="canvas" width=700 height=600></canvas>

猜你在找的HTML相关文章