Guest User

Untitled

a guest
Nov 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Dag Randy</title>
  6. <style>
  7. body {
  8. margin: 0;
  9. overflow: hidden;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <canvas id="canvas"></canvas>
  15. <script>
  16. const canvas = document.getElementById('canvas');
  17. canvas.width = window.innerWidth;
  18. canvas.height = window.innerHeight;
  19. const ctx = canvas.getContext('2d');
  20. ctx.fillStyle = "#0000FF";
  21. ctx.fillRect(0, 0, canvas.width, canvas.height);
  22.  
  23. const line = [];
  24. const quadraticCurvePoints = [];
  25. const pointsOnQuadraticCurve = [];
  26.  
  27. for (let i = 0; i < 5; i++) {
  28. const x = canvas.width / 4 * i;
  29. const y = (canvas.height - 500) + Math.random() * 100;
  30. line.push({x, y});
  31. }
  32.  
  33. /* Generate curve */
  34. ctx.beginPath();
  35. for (let i = 0; i < line.length; i++) {
  36. const point = line[i];
  37.  
  38. if (i !== 0) {
  39. const previousPoint = line[i - 1];
  40. const quadraticCurvePoint = {
  41. x: Math.min(point.x, previousPoint.x) + Math.abs(point.x - previousPoint.x) / 2 + Math.random() * 500 - 250,
  42. y: Math.min(point.y, previousPoint.y) + Math.abs(point.y - previousPoint.y) / 2 + Math.random() * 500 - 250
  43. };
  44. ctx.moveTo(previousPoint.x, previousPoint.y);
  45. ctx.strokeStyle = '#000000';
  46. ctx.quadraticCurveTo(quadraticCurvePoint.x, quadraticCurvePoint.y, point.x, point.y);
  47. quadraticCurvePoints.push(quadraticCurvePoint);
  48. }
  49. }
  50. ctx.stroke();
  51.  
  52. /* Show points */
  53. ctx.beginPath();
  54. for (let i = 0; i < line.length; i++) {
  55. const point = line[i];
  56.  
  57. if (i !== 0) {
  58. const previousPoint = line[i - 1];
  59.  
  60. const quadraticPointOnCurve = {
  61. x: (1 - 0.5) * ((1 - 0.5) * previousPoint.x + (0.5 * quadraticCurvePoints[i - 1].x)) + 0.5 * (((1 - 0.5) * quadraticCurvePoints[i - 1].x + (0.5 * point.x))),
  62. y: (1 - 0.5) * ((1 - 0.5) * previousPoint.y + (0.5 * quadraticCurvePoints[i - 1].y)) + 0.5 * (((1 - 0.5) * quadraticCurvePoints[i - 1].y + (0.5 * point.y)))
  63. };
  64.  
  65. ctx.strokeStyle = '#00FF00';
  66. ctx.moveTo(quadraticPointOnCurve.x, quadraticPointOnCurve.y);
  67. ctx.lineTo(quadraticCurvePoints[i -1].x, quadraticCurvePoints[i - 1].y);
  68.  
  69. drawPoint(quadraticCurvePoints[i - 1], 10, '#00FF00');
  70. }
  71.  
  72. drawPoint(point, 10, '#FF0000');
  73. }
  74. ctx.stroke();
  75.  
  76. for (let i = 0; i < line.length; i++) {
  77. const point = line[i];
  78. if (i !== 0) {
  79. const previousPoint = line[i - 1];
  80. for (let j = 0.1; j < 1; j += 0.2) {
  81. const pointOnQuadraticCurve = {
  82. x: (1 - j) * ((1 - j) * previousPoint.x + (j * quadraticCurvePoints[i - 1].x)) + j * (((1 - j) * quadraticCurvePoints[i - 1].x + (j * point.x))),
  83. y: (1 - j) * ((1 - j) * previousPoint.y + (j * quadraticCurvePoints[i - 1].y)) + j * (((1 - j) * quadraticCurvePoints[i - 1].y + (j * point.y)))
  84. };
  85. drawPoint(pointOnQuadraticCurve, 10, '#ffffff');
  86. pointsOnQuadraticCurve.push(pointOnQuadraticCurve);
  87. }
  88. }
  89. }
  90.  
  91.  
  92. /* Show all points on curve (line points + quadratic line points) */
  93. let allPointsOnCurve = line.slice();
  94. for (let i = 0; i < line.length; i++)
  95. allPointsOnCurve.splice(i * 6 + 1, 0, ...pointsOnQuadraticCurve.slice(i * 5, i * 5 + 5));
  96. ctx.beginPath();
  97. ctx.strokeStyle = '#FF00FF';
  98. for (let i = 0; i < allPointsOnCurve.length; i++) {
  99. const point = allPointsOnCurve[i];
  100. if (i === 0)
  101. ctx.moveTo(point.x, point.y - 5);
  102. else
  103. ctx.lineTo(point.x, point.y - 5);
  104. }
  105. ctx.stroke();
  106.  
  107. /* Show perpendiculars */
  108. ctx.beginPath();
  109. ctx.strokeStyle = '#FFFFFF';
  110. for (let i = 0; i < allPointsOnCurve.length; i++) {
  111. if (i !== 0) {
  112. const previousPoint = allPointsOnCurve[i - 1];
  113. const point = allPointsOnCurve[i];
  114. const slope = (point.y - previousPoint.y) / (point.x - previousPoint.x);
  115. const perpendicularSlope = -1/slope;
  116. const midOfPoints = {
  117. x: Math.min(point.x, previousPoint.x) + Math.abs(point.x - previousPoint.x) / 2,
  118. y: Math.min(point.y, previousPoint.y) + Math.abs(point.y - previousPoint.y) / 2
  119. };
  120. const pt1 = {x: midOfPoints.x - 5, y: midOfPoints.y - perpendicularSlope * 5};
  121. const pt2 = {x: midOfPoints.x + 5, y: midOfPoints.y + perpendicularSlope * 5};
  122. ctx.moveTo(pt1.x, pt1.y);
  123. ctx.lineTo(pt2.x, pt2.y);
  124. console.log(pt1, pt2)
  125. }
  126. }
  127. ctx.stroke();
  128.  
  129. function drawPoint(point, size, color) {
  130. ctx.fillStyle = color;
  131. ctx.fillRect(point.x - size / 2, point.y - size / 2, size, size);
  132. }
  133. </script>
  134. </body>
  135. </html>
Add Comment
Please, Sign In to add comment