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

              
                #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
      
    
              
            
!

CSS

              
                @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%;
    }
  }
}
              
            
!

JS

              
                /**
 * 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

Console