css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              #wrapper.container
  h1 The Recamán Sequence
    br
    small
      a(href="#")#run run again
      |  | 
      a(href="https://www.youtube.com/watch?v=FGC5TdIiT9U" target="_blank") more info
      
    
            
          
!
            
              @import url("https://fonts.googleapis.com/css?family=Lora:400,400i,700");

@function strip-unit($value) {
  @return $value / ($value * 0 + 1);
}

@mixin fluid-type($min-font-size, $max-font-size, $min-vw, $max-vw) {
  $u1: unit($min-vw);
  $u2: unit($max-vw);
  $u3: unit($min-font-size);
  $u4: unit($max-font-size);

  @if $u1 == $u2 and $u1 == $u3 and $u1 == $u4 {
    & {
      font-size: $min-font-size;
      @media screen and (min-width: $min-vw) {
        font-size: calc(#{$min-font-size} + #{strip-unit($max-font-size - $min-font-size)} * ((100vw - #{$min-vw}) / #{strip-unit($max-vw - $min-vw)}));
      }
      @media screen and (min-width: $max-vw) {
        font-size: $max-font-size;
      }
    }
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  @include fluid-type(16px, 20px, 576px, 992px)
}

body {
  display: flex;
  justify-content: center;
  align-items: center;

  > #wrapper {
    margin: auto;

    h1, p {
      font-family: Lora, "Times New Roman";
      margin: 16px 0;
    }

    h1 {
      @include fluid-type(26px, 80px, 300px, 1000px);
      font-weight: normal;
      text-align: center;
      line-height: 1;
      color: #333;
      display: block;
      padding-bottom: 16px;
      border-bottom: 4px solid #ccc;
    }

    a {
        color: #428bca;
        text-decoration: none;
    }

    canvas {
      display: block;
      margin: 2rem auto;
      max-width: 100%;
    }
  }
}
            
          
!
            
              /**
 * Generate the sequence
 */
const ITERATIONS = 66
let sequence = [0]
let curr

for (let i=2; i<ITERATIONS; i++) {
    curr = sequence[i-2]
    if (sequence.indexOf(curr-i) === -1 && curr-i > 0) {
        sequence.push(curr-i)
    } else {
        sequence.push(curr+i)
    }
}


/**
 * Options
 */
const SCALING = 8
const ANIMATE = true
const ANIMSPEED = 0.1
const COLORSPEED = 5


/**
 * Canvas setup
 */
const c = document.createElement('canvas')
const ctx = c.getContext('2d')
// Find the appropriate width of the canvas, i.e. the largest difference in the number line
c.width = (Math.max(...sequence)-Math.min(...sequence))*SCALING + Math.min(...sequence) + 4
// Find the appropriate height of the canvas, i.e. the largest diameter
for (let i=0, diff, max=0; i<sequence.length-1; i++) {
    diff = Math.abs(sequence[i+1] - sequence[i])
    if (diff > max) max = diff
    c.height = max*SCALING + 4
}
// Adding some internal padding for antializing
ctx.translate(2, 2)
document.getElementById('wrapper').appendChild(c)


/**
 * Animated drawing
 */
const getPos = i => (sequence[i]+sequence[i+1])/2
const getRadius = i => Math.abs(sequence[i]-sequence[i+1])/2
const isNextLarger = i => sequence[i+1] > sequence[i]
const isUp = i => Boolean(i%2)
let nextframe, progress = 0

const drawAnim = function() {
    // Clear the canvas
    ctx.clearRect(0, 0, c.width, c.height)

    // Previous circles
    let index = Math.floor(progress)

    for (let i=0, pos, radius, spin=true; i<index; i++) {
        pos = (sequence[i] + sequence[i+1])/2
        radius = Math.abs(sequence[i+1] - sequence[i])/2
        ctx.strokeStyle = `hsl(${180+i*COLORSPEED}, 50%, 50%)`
        ctx.beginPath()
        ctx.arc(pos*SCALING, c.height/2, radius*SCALING, 0, Math.PI, spin)
        ctx.stroke()
        spin = !spin
    }

    // Animated part
    let pos = getPos(index)
    let radius = getRadius(index)
    let arc = Math.PI * (progress - Math.floor(progress))
    let start = isNextLarger(index) ? Math.PI : 0
    let end = (isUp(index) && !isNextLarger(index)) || !isUp(index) && isNextLarger(index) ? start+arc : start-arc
    let spin = (isUp(index) && !isNextLarger(index)) || !isUp(index) && isNextLarger(index) ? false : true

    ctx.strokeStyle = `hsl(${180+index*COLORSPEED}, 50%, 50%)`
    ctx.beginPath()
    ctx.arc(pos*SCALING, c.height/2, radius*SCALING, start, end, spin)
    ctx.stroke()

    // Next frames
    if (progress < sequence.length-1) nextframe = requestAnimationFrame(drawAnim)
    progress += ANIMSPEED
}


/**
 * Static drawing
 */
const drawStatic = () => {
    // Axes
    ctx.beginPath()
    ctx.moveTo(0, c.height/2)
    ctx.lineTo(c.width, c.height/2)
    ctx.stroke()

    // Curve
    let spin = true
    let pos, radius
    for (let i=0; i<sequence.length-1; i++) {
        pos = (sequence[i] + sequence[i+1])/2
        radius = Math.abs(sequence[i+1] - sequence[i])/2
        ctx.strokeStyle = `hsl(${180+i*COLORSPEED}, 50%, 50%)`
        ctx.beginPath()
        ctx.arc(pos*SCALING, c.height/2, radius*SCALING, 0, Math.PI, spin)
        ctx.stroke()
        spin = !spin
    }
}


/**
 * Startup
 */
if (ANIMATE) {
    drawAnim()
} else {
    drawStatic()
}


/**
 * Run again
 */
document.getElementById('run').onclick = function(ev) {
    try {
        cancelAnimationFrame(nextframe)
    } catch (err) {

    } finally {
        progress = 0
        drawAnim()
    }
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console