Advertisement
MichalN

Bezier curve in Javascript

Feb 18th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 0.98 KB | None | 0 0
  1. <canvas height="600" width="600" id="can"></canvas>
  2. <script>
  3.     window.onload = function(){
  4.         var xt;
  5.         var yt;
  6.         var xab;
  7.         var yab;
  8.         var xbc;
  9.         var ybc;
  10.         var t;
  11.         var xa = 10;
  12.         var xb = 300;
  13.         var xc = 590;
  14.         var ya = 10;
  15.         var yb = 590;
  16.         var yc = 10;
  17.         var can = document.getElementById("can");
  18.         var ctx = can.getContext("2d");
  19.         ctx.fillStyle = "black";
  20.         ctx.fillRect(0,0,600,600);
  21.         ctx.beginPath();
  22.         ctx.moveTo(xa,ya)
  23.         for(var i=0;i<=100;i++){
  24.             t = i/100;
  25.             xab = (xa-(xa-xb)*t);
  26.             yab = (ya-(ya-yb)*t);
  27.             xbc = (xb-(xb-xc)*t);
  28.             ybc = (yb-(yb-yc)*t);
  29.             xt = (xab-(xab-xbc)*t);
  30.             yt = (yab-(yab-ybc)*t);
  31.             ctx.lineTo(xt,yt);
  32.         }
  33.         ctx.lineWidth = 5;
  34.         ctx.strokeStyle = "green";
  35.         ctx.stroke();
  36.         ctx.fillStyle = "red";
  37.         ctx.beginPath();
  38.         ctx.arc(xa,ya,5,2*Math.PI,false);
  39.         ctx.fill();
  40.         ctx.beginPath();
  41.         ctx.arc(xb,yb,5,2*Math.PI,false);
  42.         ctx.fill();
  43.         ctx.beginPath();
  44.         ctx.arc(xc,yc,5,2*Math.PI,false);
  45.         ctx.fill();
  46.     }
  47. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement