Advertisement
NoNameCracksInc

Random

Nov 13th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <style>
  5. body {
  6. padding: 0;
  7. margin: 0;
  8. overflow: hidden;
  9. }
  10. </style>
  11.  
  12. <canvas id="canvas" width="300" height="300" style="border:1px solid;"></canvas>
  13. <script>
  14. function deCasteljau(points, step) {
  15.  
  16. let final = new Array();
  17. let n = points.length;
  18.  
  19. for(let t=0; t<=1; t+=step) {
  20.  
  21. let dummy = new point(0, 0);
  22. for(let i=0; i<n; i++) {
  23.  
  24. var bp = bernsteinPolynomial(t, n-1, i);
  25. let transformed = new point(points[i].x, points[i].y).multX(bp).multY(bp);
  26. dummy.incX(transformed.x).incY(transformed.y);
  27. }
  28.  
  29. final.push(dummy);
  30. }
  31.  
  32. return final;
  33. }
  34.  
  35. function point(x, y) {
  36. this.x = x;
  37. this.y = y;
  38.  
  39. this.constructor.prototype.incX = function(inc) {
  40. this.x += inc;
  41. return this;
  42. }
  43.  
  44. this.constructor.prototype.multX = function(mult) {
  45. this.x *= mult;
  46. return this;
  47. }
  48.  
  49. this.constructor.prototype.multY = function(mult) {
  50. this.y *= mult;
  51. return this;
  52. }
  53.  
  54. this.constructor.prototype.incY = function(inc) {
  55. this.y += inc;
  56. return this;
  57. }
  58. }
  59.  
  60. function bernsteinPolynomial(t, n, i) {
  61. if(t == 1) return 0;
  62. return binomialCoefficient(n, i) * Math.pow(t, i) * Math.pow((1-t), n-i);
  63. }
  64.  
  65. function binomialCoefficient(n, i) {
  66. let nominator = factorial(n);
  67. let denominator = factorial(i) * factorial((n-i))
  68.  
  69. return (denominator < 0) ? 0 : nominator/denominator;
  70. }
  71.  
  72. function factorial(num) {
  73. let res = num;
  74.  
  75. if(num == 0){return 1};
  76.  
  77. while(num >= 2) {
  78. res *= --num;
  79. }
  80. return res;
  81. }
  82.  
  83. function goGraphic(cps) {
  84. let canvas = document.getElementById("canvas");
  85. let ctx = canvas.getContext("2d");
  86.  
  87. canvas.width = canvas.width;
  88. ctx.fillStyle = "black";
  89. ctx.rect(0, 0, canvas.width, canvas.height);
  90. ctx.fill();
  91.  
  92. let points = new Array();
  93.  
  94. for(let i=0; i<cps.length; i++) {
  95. points.push(new point(cps[i][0], cps[i][1]));
  96. }
  97.  
  98. let curve = deCasteljau(points, 0.001);
  99. ctx.shadowColor = "yellow";
  100. ctx.strokeStyle = "yellow";
  101. ctx.shadowOffsetX = 10;
  102. ctx.shadowOffsetY = 10;
  103. ctx.shadowBlur = 15;
  104.  
  105. for(let i=0; i<curve.length; i++) {
  106. ctx.lineWidth = 3-i/500;
  107. ctx.beginPath();
  108. ctx.moveTo(curve[i].x, curve[i].y);
  109. if((i+1) < curve.length) {
  110. ctx.lineTo(curve[i+1].x, curve[i+1].y);
  111. }
  112. ctx.stroke();
  113. ctx.closePath();
  114. }
  115. }
  116.  
  117. let points = [];
  118. document.body.onload = function(e) {let canvas = document.getElementById("canvas");canvas.width = screen.width;canvas.height = screen.height;goGraphic([]);}
  119. document.getElementById("canvas").onclick = function(e) {
  120. let x = e.pageX - e.target.offsetLeft;
  121. let y = e.pageY - e.target.offsetTop;
  122.  
  123. points.push([x, y]);
  124. goGraphic(points);
  125. }
  126.  
  127. </script>
  128. </body>
  129. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement