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 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.

            
              .ctrl
  label
    input(name='speed', type='range', min='0', max='1', step='0.05', value='0.25')
    span Speed
  label
    input(name='inversion', type='checkbox')
    span Show inversion
  label
    input(name='centers', type='checkbox')
    span Draw centers

            
          
!
            
              html
  background-color #0
  color #e
  overflow hidden
canvas
  vertical-align top
 
.ctrl
  position absolute
  label
    display block
  input
    vertical-align middle
  span
    margin-left .5em
            
          
!
            
              class Circle {
  /**
  new Circle(center: vector    , radius:number, color:string)
  new Circle(x:number, y:number, radius:number, color:string)
  **/
  constructor (...arg) {
    let x = arg.shift()
    if (x instanceof Vector) {
      this.center = x
    } else {
      this.center = new Vector(x, arg.shift())
    }
    this.radius = arg.shift()
    this.color = arg.shift()
    if (this.color == null) {
      this.color = '#fff'
    }
    this.highlight = arg.shift()
  }
  mul (a) {
    return new Circle(this.center.mul(a), this.radius * a, this.color, this.highlight)
  }
  invert (v) {
    if (v instanceof Circle) {
      return this.invertCircle(v)
    }
    let l1 = this.center.dist(v)
    let l2 = (this.radius * this.radius) / l1
    return v.sub(this.center).len(l2).add(this.center)
  }
  invertCircle (c) {
    let c2 = c.center.sub(this.center)
    let a = c2.angle()
    c2 = c.center.add(Vector.polar(c.radius, a))
    let c3 = c.center.add(Vector.polar(c.radius, a + Math.PI))
    c2 = this.invert(c2)
    c3 = this.invert(c3)

    let ctr = c2.mix(c3, 0.5)
    let r = ctr.dist(c2)
    return new Circle(ctr, r, c.color, c.highlight)
  }
  inverterCircle (c) {
    let c2 = c.center.sub(this.center)
    let a = c2.angle()
    c2 = c.center.add(Vector.polar(c.radius, a))
    let c3 = c.center.add(Vector.polar(c.radius, a + Math.PI))
    c2 = this.invert(c2)
    c3 = this.invert(c3)

    let ctr = c2.mix(c3, 0.5)
    let r = ctr.dist(c2)
    return new Circle(ctr, r, c.color, c.highlight)
  }
  points (n) {
    let arr = []
    for (let i = 0; i < n; i++) {
      arr.push(this.center.add(Vector.polar(this.radius, Math.PI * 2 * i / n)))
    }
    return arr
  }
}

const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
document.body.appendChild(canvas)
let win
let mid
let scale
let n = 7
let init = Date.now()
let mouse = new Vector(0, -0.35)
const circles = new Set()
const ctrl = {}
const ctrlFunc = {
  speed(v){
    let now = Date.now()
    let oldSpeed = ctrl.speed || 1
    v = v ** 3 / 100
    let cur = (now - init) * oldSpeed
    let diff = cur * v / oldSpeed
    if(v === 0){
      init = now
    } else {
      init = now - (now - init) * oldSpeed / v
    }
    //init = (now - init) * (oldSpeed - v)
    return v
  }
}
let reqAF
Array.prototype.forEach.call(document.querySelectorAll(".ctrl input"), inp=>{
  function change(){
    let val = inp.value
    if(inp.type === 'checkbox')
      val = inp.checked
    if(inp.type === 'range' || inp.type === 'number')
      val = +val
     if(inp.name in ctrlFunc)
       val = ctrlFunc[inp.name](val)
    ctrl[inp.name] = val
    draw()
  }
  change()
  inp.addEventListener('input', change)
  inp.addEventListener('change', change)
})
function resize () {
  win = new Vector(window.innerWidth, window.innerHeight)
  mid = win.div(2)
  canvas.width = win.x
  canvas.height = win.y
  scale = Math.min(mid.x, mid.y)
  draw()
}
window.addEventListener('resize', resize)
resize()
window.addEventListener('mousemove', function (e) {
  mouse = new Vector(e.pageX, e.pageY).sub(mid).div(scale)
  draw()
})

window.addEventListener('wheel', function (e) {
  e.preventDefault()
  let d = e.deltaY
  if (!d) {
    return
  }
  d = d < 0 ? -1 : 1
  if (e.ctrlKey) {
    d /= 12
  } else {
    n = Math.round(n)
  }
  n = n + d
  if (n < 3) {
    n = 3
  }
  draw()
})
function loop () {
  draw()
  window.requestAnimationFrame(loop)
}
loop()

function draw(){
  if(reqAF)
    return;
  //window.cancelAnimationFrame(reqAF)
  reqAF = window.requestAnimationFrame(_draw)
  //try{
  //  _draw()
  //}catch(e){}
}

function _draw () {
  reqAF = null;
  ctx.fillStyle = '#000'
  ctx.fillRect(0, 0, win.x, win.y)
  circles.clear()
  const out = new Circle(0, 0, 1, '#f84', 2)
  circles.add(out)
  const s = Math.sin(Math.PI / n)
  const r = (1 - s) / (1 + s)
  const rc = (1 - r) / 2
  const rr = (1 + r) / 2
  const inc = new Circle(0, 0, r, '#84f', 2)
  circles.add(inc)
  const mouseCircle = new Circle(mouse.mul(ctrl.inversion?1:-.5), ctrl.inversion?0.5*0.7:0.5, "#444")
  for (let i = 0; i < n; i++) {
    const angle = i / n * Math.PI * 2 + (Date.now() - init) * ctrl.speed
    const ctr = Vector.polar(rr, angle)
    const hue = (i + 0.5) / n * 360
    circles.add(new Circle(ctr, rc, `hsl(${hue},100%,50%)`))
  }
  const inc2 = mouseCircle.invert(inc)
  const rap = out.radius / inc2.radius * 0.7
  const ctr2 = inc2.center.mul(rap)
  circles.forEach(function (c) {
    if(ctrl.inversion){
      c = c.mul(0.7)
      ctx.globalAlpha = 0.25
      drawCircle(c)
      ctx.globalAlpha = 1
    }
    c = mouseCircle.invert(c)
    if(!ctrl.inversion){
      c = c.mul(rap)
      c.center = c.center.sub(ctr2)
    }
      drawCircle(c)
  })
  if(ctrl.inversion){
    ctx.setLineDash([5, 5])
    drawCircle(mouseCircle)
    ctx.setLineDash([])
  }
}

function drawPoint (p, col) {
  const s = 3
  p = mid.add(p.mul(scale))
  ctx.beginPath()
  ctx.strokeStyle = col || '#acf'
  ctx.moveTo(p.x - s, p.y)
  ctx.lineTo(p.x + s, p.y)
  ctx.moveTo(p.x, p.y - s)
  ctx.lineTo(p.x, p.y + s)
  ctx.stroke()
}

function drawCircle (c) {
  ctx.beginPath()
  ctx.strokeStyle = c.color
  ctx.setLineDash(c.highlight ? [7,3] : [])
  let ctr = mid.add(c.center.mul(scale))
  ctx.arc(ctr.x, ctr.y, scale * c.radius, 0, Math.PI * 2, 1)
  ctx.stroke()
  if (ctrl.centers) {
    ctx.strokeStyle = '#999'
    drawPoint(c.center)
  }
}

            
          
!
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