cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

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.

            
              <canvas id="canvas" width="720px" height="228px"></canvas>
            
          
!
            
              * {
	border: 0;
	margin: 0;
}
canvas {
  background-color: rgb(246, 256, 253);
}
            
          
!
            
              // https://msvaljek.blogspot.com/2013/09/number-pi-visualization.html
/*
      solution for:
      https://i.imgur.com/LlpGldr.gif
      
      by @msvaljek
*/

var canvas = document.getElementById('canvas'),
    ctx = canvas.getContext('2d'),
    cwidth = canvas.width,
    cheight = canvas.height;


function drawLine(propagate) {
  ctx.strokeStyle = 'rgb(0, 0, 0)';
  ctx.lineWidth = 4;

  ctx.beginPath();
  ctx.moveTo(0, 192);
  ctx.lineTo(cwidth, 192);
  ctx.stroke();

  if (propagate) {
    setTimeout(function () {draw1('rgb(189, 24, 66)');}, 360);
  }
}

drawLine(true);

function drawNumber0() {
  ctx.strokeStyle = ctx.fillStyle = 'rgb(0, 132, 206)';
  ctx.lineWidth = 2;

  ctx.beginPath();
  ctx.moveTo(96, 84);
  ctx.lineTo(96, cheight);
  ctx.stroke();

  ctx.font = 'bold 36pt Arial';
  ctx.fillText('0', 83, 53);
}

function drawNumber1() {
  ctx.strokeStyle = ctx.fillStyle = 'rgb(0, 132, 206)';
  ctx.lineWidth = 2;

  ctx.beginPath();
  ctx.moveTo(240, 84);
  ctx.lineTo(240, cheight);
  ctx.stroke();

  ctx.fillText('1', 225, 53);
}

function drawNumber2() {
  ctx.strokeStyle = ctx.fillStyle = 'rgb(0, 132, 206)';
  ctx.lineWidth = 2;

  ctx.beginPath();
  ctx.moveTo(385, 84);
  ctx.lineTo(385, cheight);
  ctx.stroke();

  ctx.font = 'bold 36pt Arial';
  ctx.fillText('2', 373, 53);
}

function drawNumber3() {
  ctx.strokeStyle = ctx.fillStyle = 'rgb(0, 132, 206)';
  ctx.lineWidth = 2;

  ctx.beginPath();
  ctx.moveTo(530, 84);
  ctx.lineTo(530, cheight);
  ctx.stroke();

  ctx.font = 'bold 36pt Arial';
  ctx.fillText('3', 515, 53);
}

function drawNumber4() {
  ctx.strokeStyle = ctx.fillStyle = 'rgb(0, 132, 206)';
  ctx.lineWidth = 2;

  ctx.beginPath();
  ctx.moveTo(675, 84);
  ctx.lineTo(675, cheight);
  ctx.stroke();

  ctx.font = 'bold 36pt Arial';
  ctx.fillText('4', 660, 53);
}

function draw1(color) {
  drawNumber0();
  drawNumber1();

  ctx.lineWidth = 3;
  ctx.strokeStyle = color;
  ctx.beginPath();
  ctx.arc(168, 120, 71, 0, 2 * Math.PI);
  ctx.stroke();

  if (color === 'rgb(189, 24, 66)') {
    setTimeout(function () {draw2('rgb(189, 24, 66)');}, 360);
  }
}

function draw2(color) {
  if (color !== 'rgb(0, 132, 206)') {
    draw1('rgb(0, 0, 0)');
  }

  drawNumber2();

  ctx.lineWidth = 3;
  ctx.strokeStyle = color;
  ctx.beginPath();
  ctx.arc(313, 120, 71, 0, 2 * Math.PI);
  ctx.stroke();

  if (color === 'rgb(189, 24, 66)') {
    setTimeout(function () {draw3('rgb(189, 24, 66)');}, 360);
  }
}

function draw3(color) {
  if (color !== 'rgb(0, 132, 206)') {
    draw2('rgb(0, 0, 0)');
  }

  drawNumber3();

  ctx.lineWidth = 3;
  ctx.strokeStyle = color;
  ctx.beginPath();
  ctx.arc(458, 120, 71, 0, 2 * Math.PI);
  ctx.stroke();

  if (color === 'rgb(189, 24, 66)') {
    setTimeout(function () {draw4('rgb(189, 24, 66)');}, 360);
  }
}

function draw4(color) {
  if (color !== 'rgb(0, 132, 206)') {
    draw3('rgb(0, 0, 0)');
  }

  drawNumber4();

  ctx.lineWidth = 3;
  ctx.strokeStyle = color;
  ctx.beginPath();
  ctx.arc(603, 120, 71, 0, 2 * Math.PI);
  ctx.stroke();

  if (color === 'rgb(189, 24, 66)') {
    setTimeout(drawAllCirclesBlack, 360);
  }
}

function drawAllCirclesBlack() {
  draw1('rgb(0, 0, 0)');
  draw2('rgb(0, 0, 0)');
  draw3('rgb(0, 0, 0)');
  draw4('rgb(0, 0, 0)');

  setTimeout(drawAllCirclesBlue, 180);
}

function drawAllCirclesBlue() {
  ctx.clearRect(0, 0, cwidth, cheight);

  drawLine();
  draw1('rgb(0, 132, 206)');
  draw2('rgb(0, 132, 206)');
  draw3('rgb(0, 132, 206)');
  draw4('rgb(0, 132, 206)');

  setTimeout(function () {displayBoard(true);}, 180);
}

function displayBoard(cont) {
  ctx.clearRect(0, 0, cwidth, cheight);
  drawLine();
  drawNumber0();
  drawNumber1();
  drawNumber2();
  drawNumber3();
  drawNumber4();
  if (cont) {
    setTimeout(animateWheel, 180);
  }
}

function drawWheel(angle) {
  drawMarker(96, 230, 'rgb(255, 198, 0)');

  var maxAngle = angle >= 2 * Math.PI ? 2 * Math.PI : angle;
  var drawPi = angle >= 2 * Math.PI;

  if (drawPi) {
    ctx.strokeStyle = 'rgb(189, 24, 66)';
    ctx.lineWidth = 2;
    ctx.beginPath();
    ctx.moveTo(96 + maxAngle * 72, 0);
    ctx.lineTo(96 + maxAngle * 72, cheight);
    ctx.stroke();
  }

  ctx.save();

  ctx.translate(96 + angle * 72, 120);
  ctx.rotate(angle);

  ctx.fillStyle = ctx.strokeStyle = 'rgb(255, 198, 0)';

  ctx.beginPath();
  ctx.arc(0, 0, 25, 0, 2 * Math.PI);
  ctx.fill();

  ctx.lineWidth = 15;
  ctx.beginPath();
  ctx.moveTo(0, 0);
  ctx.lineTo(0, 67);
  ctx.stroke();

  ctx.beginPath();
  ctx.moveTo(0, 0);
  ctx.lineTo(-53, 40);
  ctx.stroke();

  ctx.beginPath();
  ctx.moveTo(0, 0);
  ctx.lineTo(53, 40);
  ctx.stroke();

  ctx.beginPath();
  ctx.moveTo(0, 0);
  ctx.lineTo(-64, -18);
  ctx.stroke();

  ctx.beginPath();
  ctx.moveTo(0, 0);
  ctx.lineTo(64, -18);
  ctx.stroke();

  ctx.beginPath();
  ctx.moveTo(0, 0);
  ctx.lineTo(-30, -60);
  ctx.stroke();

  ctx.beginPath();
  ctx.moveTo(0, 0);
  ctx.lineTo(30, -60);
  ctx.stroke();

  ctx.lineWidth = 10;
  ctx.beginPath();
  ctx.arc(0, 0, 66, 0, 2 * Math.PI);
  ctx.stroke();

  ctx.fillStyle = 'white';
  ctx.beginPath();
  ctx.arc(0, 0, 4, 0, 2 * Math.PI);
  ctx.fill();

  ctx.beginPath();
  ctx.arc(0, 0, 23, 0, 2 * Math.PI);
  ctx.fill();

  ctx.lineWidth = 10;
  ctx.fillStyle = ctx.strokeStyle = 'rgb(255, 198, 0)';
  ctx.beginPath();
  ctx.arc(0, 0, 9, 0, 2 * Math.PI);
  ctx.stroke();

  ctx.lineWidth = 8;
  ctx.strokeStyle = 'rgb(189, 24, 66)';
  ctx.beginPath();
  ctx.arc(0, 0, 73, Math.PI / 2, 2 * Math.PI - maxAngle + Math.PI / 2);
  ctx.stroke();

  drawMarker(0, 70, 'blue');
  ctx.restore();

  if (drawPi) {
    ctx.fillStyle = 'black';
    ctx.font = 'bold 50pt Arial';
    ctx.fillText('π', 96 + maxAngle * 72 - 25, 100);
  }

  drawMarker(96 + maxAngle * 72, 230, 'rgb(189, 24, 66)');

  ctx.lineWidth = 8;
  ctx.strokeStyle = 'rgb(189, 24, 66)';
  ctx.beginPath();
  ctx.moveTo(96, 193);
  ctx.lineTo(96 + maxAngle * 72, 193);
  ctx.stroke();
}

function drawMarker(x, y, color) {
  ctx.beginPath();
  ctx.moveTo(x, y);
  ctx.lineTo(x - 10, y - 20);
  ctx.lineTo(x, y - 40);
  ctx.lineTo(x + 10, y - 20);
  ctx.closePath();
  ctx.fillStyle = color;
  ctx.fill();
}

function animateWheel(angle) {
  var animangle = angle ? angle : 0;
  var dangle = 0.1;

  if (animangle < 10) {
    animangle += dangle;
    ctx.clearRect(0, 0, cwidth, cheight);
    displayBoard();
    drawWheel(animangle);
    setTimeout(function () {animateWheel(animangle);}, 15);
  }
}
            
          
!
999px
Loading ..................

Console