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

              
                <div class="wrapper">
  <div class="content">
    <h1>Calculating PI</h1>
    <p>Dividing the number of raindrops that hit the circle and the square and multiplying the result by 4 gives an approximation to PI. This approximation gets better with time.</p>
    <p>This demo is directly inspired by this real-life experiment: <a href="https://www.reddit.com/r/dataisbeautiful/comments/84esm6/pi_as_calculated_by_raindrops_falling_on_square/" target="_blank">See it on Reddit</a></p>
    <p>Number of raindrop impacts so far: <code id="impact-number">0</code></p>
    <p>Current approximation: <code id="approximation-value">N/A</code></p>
    <p>Closest approximation: <code id="closest-approximation">N/A</code> (at impact #: <code id="closest-approximation-at"></code>)</p>
    <p>Actual PI: <code id="actual-pi"></code></p>
  </div>
  <canvas id="zdog-canvas" width="400" height="300"></canvas>
</div>
<canvas id="chart-canvas" width="800" height="300"></canvas>
              
            
!

CSS

              
                html, body {
  min-height: 100vh;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font: 400 normal 16px/1.4 helvetica, sans-serif;
}

canvas, img {
  vertical-align: center;
  margin: 0;
}

.wrapper {
  display: flex;
}

.content {
  padding: 1em;

  h1 {
    font-size: 2.5em;
    font-weight: bold;
    margin: 0 0 1rem;
  }

  p {
    margin: 0 0 1em;
  }

  code {
    font-family: "Operator Mono", menlo, monaco, monospace;
    background: #fd5;
    padding: .125em .25em;
    border-radius: 3px;
  }
}
              
            
!

JS

              
                const { TAU } = Zdog
const { PI, sqrt, abs, pow, round } = Math

const random = (max = 1, min = 0) => {
  return min + Math.random() * (max - min)
}

const maybeNegative = (number) => {
  if (random() >= .5) {
    return -number
  }
  return number
}

const randomPos = (values) => {
  return {
    x: maybeNegative(random(400)),
    y: maybeNegative(random(400)),
    z: maybeNegative(random(400)),
    ...values
  }
}

const times = (howMany, what) => {
  return [...Array(howMany).keys()].map(what)
}

const rainIntensity = 15
const plateSize = 120
const circlePos = { x: -90, z: 0 }
const squarePos =  { x: 90, z: 0 }

let isSpinning = true
const drawing = new Zdog.Illustration({
  element: '#zdog-canvas',
  rotate: { y: TAU/16, x: TAU/-10 },
  dragRotate: true,
  onDragStart() {
    isSpinning = false
  }
})

const plates = new Zdog.Group({
  addTo: drawing,
  translate: { y: 80 }
})

new Zdog.Ellipse({
  addTo: plates,
  diameter: plateSize,
  stroke: 10,
  color: 'hotpink',
  fill: true,
  translate: circlePos,
  rotate: { x: Zdog.TAU/4 }
})

new Zdog.Rect({
  addTo: plates,
  width: plateSize,
  height: plateSize,
  stroke: 10,
  color: 'mediumspringgreen',
  fill: true,
  cornerRadius: 0,
  translate: squarePos,
  rotate: { x: Zdog.TAU/4 }
})

const createDrop = () => {
  const rgba = [
    round(random(31, 63)),
    round(random(63, 100)),
    round(random(200, 255)),
    random(0.3, 0.6)
  ].join(',')
  return new Zdog.Shape({
    addTo: drawing,
    stroke: 3,
    color: `rgba(${rgba})`,
    translate: randomPos({ y: -400 }),
    path: [
      { y: -10 },
      { y: 10 }
    ]
  })
}

const collides = (plate, { translate: { x, z }}, { isCircle } = {}) => {
  if (isCircle) {
    const xDiff = abs(x - plate.x)
    const zDiff = abs(z - plate.z)
    const distance = sqrt( pow(xDiff, 2) + pow(zDiff, 2) )
    return distance <= plateSize / 2
  }
  const xMin = plate.x - plateSize / 2
  const xMax = plate.x + plateSize / 2
  const xIntersects =  x >= xMin && x <= xMax
  
  const zMin = plate.z - plateSize / 2
  const zMax = plate.z + plateSize / 2
  const zIntersects = z >= zMin && z <= zMax
  
  return xIntersects && zIntersects
}

const getApproximation = () => {
  return Counts.circle/Counts.square * 4
}

const getImpactCount = () => {
  return Counts.circle + Counts.square
}

let drops = []
const Counts = {
  circle: 0,
  square: 0
}
const loop = () => {
  drops.push(...times(rainIntensity, createDrop))
  drops.forEach((drop, index) => {
    if (drop.translate.y <= 300) {
      drop.translate.y += 10
    } else {
      if (collides(circlePos, drop, {isCircle: true})) {
        Counts.circle++
      }
      if (collides(squarePos, drop)) {
        Counts.square++
      }
      drop.remove()
      drops.splice(index, 1)
    }
  })
  if (isSpinning) {
    drawing.rotate.y += .005
  }
  drawing.updateRenderGraph()
  requestAnimationFrame(loop)
}

requestAnimationFrame(loop)

const chartConfig = {
  type: 'line',
  data: {
    labels: [],
    datasets: [{
      label: 'Approximation',
      fill: false,
      backgroundColor: 'blue',
      borderColor: 'blue',
      borderWidth: 1,
      pointRadius: 2,
      pointHoverRadius: 3,
      data: [],
    }]
  },
  options: {
    responsive: true,
    scales: {
      xAxes: [{
        display: true,
        scaleLabel: {
          display: true,
          labelString: 'Number of raindrop impacts'
        }
      }],
      yAxes: [{
        display: true,
        scaleLabel: {
          display: true,
          labelString: 'Values'
        }
      }]
    },
    annotation: {
      annotations: [{
        drawTime: 'beforeDatasetsDraw',
        id: 'hline',
        type: 'line',
        mode: 'horizontal',
        scaleID: 'y-axis-0',
        value: PI,
        borderColor: 'red',
        borderWidth: 1,
        label: {
          backgroundColor: 'transparent',
          fontColor: 'red',
          content: 'Actual PI',
          position: 'left',
          yAdjust: -6,
          xAdjust: -5,
          enabled: true,
        }
      }]
    }
  },
}

const chartCanvas = document.getElementById('chart-canvas')
const chartCtx = chartCanvas.getContext('2d')
const myChart = new Chart(chartCtx, chartConfig)

let chartLoopFrame = 0
let closestApproximation = 0
let closestApproximationAt = 0
let refreshRate = 50

const chartLoop = () => {
  const ratio = getApproximation()
  const impacts = getImpactCount()
  if (abs(PI - ratio) < abs(PI - closestApproximation)) {
    closestApproximation = ratio
    closestApproximationAt = impacts
  }
  if (ratio && !(chartLoopFrame++ % refreshRate)) {
    chartConfig.data.labels.push(impacts)
    chartConfig.data.datasets[0].data.push(ratio)
    if (chartConfig.data.datasets[0].data.length > 50) {
      refreshRate = 60
    }
    if (chartConfig.data.datasets[0].data.length > 100) {
      refreshRate = 70
      chartConfig.options.animation = {
        duration: 0
      }
      chartConfig.options.hover = {
        animationDuration: 0
      }
      chartConfig.options.elements = {
        line: {
          tension: 0
        }
      }
    }
    if (chartConfig.data.datasets[0].data.length > 300) {
      refreshRate = 80
      chartConfig.options.showLines = false 
      chartConfig.data.datasets[0].pointRadius = 1
      chartConfig.data.datasets[0].pointHoverRadius = 2
    }
    myChart.update(chartConfig)
  }
  requestAnimationFrame(chartLoop)
}

requestAnimationFrame(chartLoop)

const approximationView = document.getElementById('approximation-value')
const closestApproximationView = document.getElementById('closest-approximation')
const closestApproximationAtView = document.getElementById('closest-approximation-at')
const impactCountView = document.getElementById('impact-number')

const domLoop = () => {
  const value = getApproximation()
  const impacts = getImpactCount()
  approximationView.innerText = value
  closestApproximationView.innerText = closestApproximation
  closestApproximationAtView.innerText = closestApproximationAt
  impactCountView.innerText = impacts
  requestAnimationFrame(domLoop)
}

requestAnimationFrame(domLoop)

const actualPI = document.getElementById('actual-pi')
actualPI.innerText = Math.PI
              
            
!
999px

Console