Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
<form>  
  <input type="checkbox" onchange = "deletePointCheck()">Delete point<br/>
  <input type="button" value="clear" onclick="deletePoints()"/>
</form>

<canvas id="drawing" width="600" height="600">Canvas not supported.</canvas>
</body>
              
            
!

CSS

              
                #drawing { 
  border: 1px solid #000;
  background: #fff;  
  display: block;
}
              
            
!

JS

              
                var obj = document.getElementById('drawing');
var ctx = obj.getContext('2d');
var Idint;
var points = [];
var legendPoints = [];
var pointsDel = false;

addEventListener("mousedown", mDown);
addEventListener("mouseup", mUp);
addEventListener("mousemove", mMove);


function deletePoints(){
  points = new Array();
  legendPoints = new Array();
}

function deletePoint(ind){
  points.splice(ind, 1);
  for(let i = ind; i < points.length; i++){
    points[i].legend = "P" + String(i);    
  }
}

function deletePointCheck(){
  pointsDel = !pointsDel;
}

function addPoint(x, y){
  if(x >=0 && x <= obj.width && y >=0 && y<= obj.height) points.push(new Point(x, y, "P" + String(points.length)));
}

var mouseDownOnPoint = false;
var mouseDown = false;
var dragPoint = 0;
function mDown(event){
  mouseDown = true;
  let rect = obj.getBoundingClientRect();
  let x = event.clientX - rect.left;
  let y = event.clientY - rect.top;
  for(let i = 0; i < points.length; i++){
    if(x >= points[i].x - points[i].r && x <= points[i].x + points[i].r && y >= points[i].y - points[i].r && y <= points[i].y + points[i].r){
      points[i].x = x; points[i].y = y; dragPoint = i;
      if(pointsDel) { deletePoint(i); return; }
      mouseDownOnPoint = true;
    }
  }
  if(!mouseDownOnPoint && !pointsDel) addPoint(x, y);
}
function mUp(event){
  mouseDown = false;
  mouseDownOnPoint = false;
}

function mMove(event){
  if(mouseDown){
    if(pointsDel) return;
    if(mouseDownOnPoint){
      let rect = obj.getBoundingClientRect();
      let x = event.clientX - rect.left;
      let y = event.clientY - rect.top;
      let ind = dragPoint;
      if(x < points[ind].r) x = points[ind].r;
      if(x > obj.width - points[ind].r) x = obj.width - points[ind].r;
      if(y < points[ind].r) y = points[ind].r;
      if(y > obj.height - points[ind].r) y = obj.height - points[ind].r;
      points[ind].x = x; points[ind].y = y;
    }
  }
}

function sin(deg){
  return Math.sin(deg * Math.PI / 180);
}
function cos(deg){
  return Math.cos(deg * Math.PI / 180);
}

function circle(x, y, r, color = "white"){
  let oldColor = ctx.fillStyle;
  ctx.beginPath();
  ctx.moveTo(x, y + r);
  for(let t = 0; t <= 360; t += 1){
    ctx.lineTo(r * sin(t) + x, r * cos(t) + y);
  }
  ctx.fillStyle = color; ctx.fill();
  ctx.stroke();
  ctx.fillStyle = oldColor;
}

class Point{
  x = 0; y = 0; legend;
  r = 5; color; fontSize;
  
  constructor(x = 0, y = 0, legend = "", color = "black", r = 5, fontSize = 14){
    this.x = x; this.y = y; this.r = r;
    this.legend = legend; this.fontSize = fontSize
    this.color = color;
  }
  
  copyFrom(point){
    this.x = point.x; this.y = point.y;
    this.r = point.r; this.color = point.color;
    this.legend = point.legend; this.fontSize = point.fontSize;
  }
  
  draw(fillColor = "white"){
    ctx.beginPath();
    ctx.srokeStyle = "white";
    circle(this.x, this.y, this.r, "white")
    ctx.strokeStyle = this.color;
    circle(this.x, this.y, this.r, fillColor)
    ctx.fillStyle = this.color;
    ctx.font = String(this.fontSize) + 'px sherif';
    ctx.fillText(this.legend, this.x - this.r - 2, this.y - this.r - 2);
  }
}

function betaSpline(points, color = "black"){
  let oldStrokeStyle = ctx.strokeStyle;
  ctx.beginPath();
  legendPoints = [];
  for(let i = 1; i < points.length - 2; i++){
    let a3 = (-points[i-1].x + 3*points[i].x - 3*points[i+1].x + points[i+2].x) / 6,
        a2 = (points[i-1].x - 2*points[i].x + points[i+1].x) / 2,
        a1 = (-points[i-1].x + points[i+1].x) / 2,
        a0 = (points[i-1].x + 4*points[i].x + points[i+1].x) / 6;
    let b3 = (-points[i-1].y + 3*points[i].y - 3*points[i+1].y + points[i+2].y) / 6,
        b2 = (points[i-1].y - 2*points[i].y + points[i+1].y) / 2,
        b1 = (-points[i-1].y + points[i+1].y) / 2,
        b0 = (points[i-1].y + 4*points[i].y + points[i+1].y) / 6;    
 
    ctx.fillStyle = "blue";
    ctx.font = '12px sherif';    
    ctx.fillText("Q" + String(i+2), a0 + 0.5*(a1 + 0.5*(a2 + 0.5*a3)) - 2, b0 + 0.5*(b1 + 0.5*(b2 + 0.5*b3)) - 2);
    legendPoints.push(new Point(a0, b0, "t" + String(i+2), "black", 3, 12));
    if(i + 3 == points.length) legendPoints.push(new Point(a0 + a1 + a2 + a3, b0 + b1 + b2 + b3, "t" + String(i+3), "black", 3, 12));
    
    for(let t = 0; t <= 1; t += 0.005){
      let x = ((a3 * t + a2) * t + a1) * t + a0;
      let y = ((b3 * t + b2) * t + b1) * t + b0;
      ctx.lineTo(x, y);
    }
  }
  ctx.strokeStyle = color;
  ctx.stroke();
  ctx.strokeStyle = oldStrokeStyle;
}

function clear(){
  ctx.fillStyle = "white";
  ctx.fillRect(0,0,ctx.canvas.width, ctx.canvas.height);
}

function init(){
  clear();
  betaSpline(points, "red");
  for(let i = 0; i < points.length; i++) points[i].draw();
  for(let i = 0; i < legendPoints.length; i++) legendPoints[i].draw("black");
  Idint = requestAnimationFrame(init);
}

function main(){
  deletePoints();
  addPoint(80, 240);
  addPoint(80, 100);
  addPoint(230, 115);
  addPoint(225, 260);
  addPoint(385, 240);
  addPoint(450, 100);
  addPoint(555, 111);
  addPoint(570, 240);
  init();
}

main();
              
            
!
999px

Console