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.

            
              .info
  %hgroup.about
    %h1 Kaleidoscope
    %h2 Canvas kaleidoscope implementation with drag & drop
    %h3 Drop an image
            
          
!
            
              @import compass


html, body
  background: #1b1b1b

/* Info */

@import url( https://fonts.googleapis.com/css?family=Quantico )

@keyframes show-info
  0%
    transform: rotateY(120deg)
  100%
    transform: rotateY(0deg)

.info

  transition: all 180ms ease-out
  transform-style: preserve-3d
  transform: perspective(800px)

  font-family: 'Quantico', sans-serif
  position: absolute
  font-size: 12px
  opacity: 0.8
  color: #fff
  width: 240px
  left: 0px
  top: 20px

  &:hover

    box-shadow: 0 0 0 4px rgba(255,255,255,0.05)
    opacity: 1.0

  h1, h2, h3

    line-height: 1
    margin: 5px 0

  a

    transition: all 200ms ease-out
    border-bottom: 1px dotted rgba(255,255,255,0.4)
    text-decoration: none
    opacity: 0.6
    color: #fff

    &:hover

      opacity: 0.99

  .about,
  .more

    transform-origin: 0% 50%
    transform: rotateY(120deg)

    margin-bottom: 1px
    background: rgba(0,0,0,0.8)
    padding: 12px 15px 12px 20px

  .about

    animation: show-info 500ms cubic-bezier(0.230, 1.000, 0.320, 1.000) 600ms 1 normal forwards
    padding-bottom: 15px

    a
      opacity: 0.9

    h1

      letter-spacing: -1px
      font-weight: 300
      font-size: 19px
      opacity: 0.95

    h2

      font-weight: 300
      font-size: 13px
      opacity: 0.8

    h3

      text-transform: uppercase
      margin-top: 10px
      font-size: 11px

      &:before

        margin-right: 2px
        font-size: 14px
        content: '\203A'

  .more

    animation: show-info 500ms cubic-bezier(0.230, 1.000, 0.320, 1.000) 500ms 1 normal forwards
    padding: 5px 15px 10px 20px

    a
    
      text-transform: uppercase
      margin-right: 10px
      font-size: 10px
            
          
!
            
              
# Kaleidoscope
  
class Kaleidoscope
  
  HALF_PI: Math.PI / 2
  TWO_PI: Math.PI * 2
  
  constructor: ( @options = {} ) ->
    
    @defaults =
      offsetRotation: 0.0
      offsetScale: 1.0
      offsetX: 0.0
      offsetY: 0.0
      radius: 260
      slices: 12
      zoom: 1.0
        
    @[ key ] = val for key, val of @defaults
    @[ key ] = val for key, val of @options
      
    @domElement ?= document.createElement 'canvas'
    @context ?= @domElement.getContext '2d'
    @image ?= document.createElement 'img'
    
  draw: ->
    
    @domElement.width = @domElement.height = @radius * 2
    @context.fillStyle = @context.createPattern @image, 'repeat'
    
    scale = @zoom * ( @radius / Math.min @image.width, @image.height )
    step = @TWO_PI / @slices
    cx = @image.width / 2

    for index in [ 0..@slices ]
      
      @context.save()
      @context.translate @radius, @radius
      @context.rotate index * step
      
      @context.beginPath()
      @context.moveTo -0.5, -0.5
      @context.arc 0, 0, @radius, step * -0.51, step * 0.51
      @context.lineTo 0.5, 0.5
      @context.closePath()
      
      @context.rotate @HALF_PI
      @context.scale scale, scale
      @context.scale [-1,1][index % 2], 1
      @context.translate @offsetX - cx, @offsetY
      @context.rotate @offsetRotation
      @context.scale @offsetScale, @offsetScale
      
      @context.fill()
      @context.restore()

# Drag & Drop
  
class DragDrop
  
  constructor: ( @callback, @context = document, @filter = /^image/i ) ->
    
    disable = ( event ) ->
      do event.stopPropagation
      do event.preventDefault
    
    @context.addEventListener 'dragleave', disable
    @context.addEventListener 'dragenter', disable
    @context.addEventListener 'dragover', disable
    @context.addEventListener 'drop', @onDrop, no
      
  onDrop: ( event ) =>
    
    do event.stopPropagation
    do event.preventDefault
      
    file = event.dataTransfer.files[0]
    
    if @filter.test file.type
      
      reader = new FileReader
      reader.onload = ( event ) => @callback? event.target.result
      reader.readAsDataURL file

# Init kaleidoscope
  
image = new Image
image.onload = => do kaleidoscope.draw
image.src = 'https://cl.ly/image/1X3e0u1Q0M01/cm.jpg'

kaleidoscope = new Kaleidoscope
  image: image
  slices: 20

kaleidoscope.domElement.style.position = 'absolute'
kaleidoscope.domElement.style.marginLeft = -kaleidoscope.radius + 'px'
kaleidoscope.domElement.style.marginTop = -kaleidoscope.radius + 'px'
kaleidoscope.domElement.style.left = '50%'
kaleidoscope.domElement.style.top = '50%'
document.body.appendChild kaleidoscope.domElement
  
# Init drag & drop

dragger = new DragDrop ( data ) -> kaleidoscope.image.src = data
  
# Mouse events
  
tx = kaleidoscope.offsetX
ty = kaleidoscope.offsetY
tr = kaleidoscope.offsetRotation
  
onMouseMoved = ( event ) =>

  cx = window.innerWidth / 2
  cy = window.innerHeight / 2
                
  dx = event.pageX / window.innerWidth
  dy = event.pageY / window.innerHeight
                
  hx = dx - 0.5
  hy = dy - 0.5
                
  tx = hx * kaleidoscope.radius * -2
  ty = hy * kaleidoscope.radius * 2
  tr = Math.atan2 hy, hx

window.addEventListener 'mousemove', onMouseMoved, no
                
# Init
  
options =
  interactive: yes
  ease: 0.1
                
do update = =>
                
  if options.interactive

    delta = tr - kaleidoscope.offsetRotation
    theta = Math.atan2( Math.sin( delta ), Math.cos( delta ) )
                
    kaleidoscope.offsetX += ( tx - kaleidoscope.offsetX ) * options.ease
    kaleidoscope.offsetY += ( ty - kaleidoscope.offsetY ) * options.ease
    kaleidoscope.offsetRotation += ( theta - kaleidoscope.offsetRotation ) * options.ease
    
    do kaleidoscope.draw
  
  setTimeout update, 1000/60
    
# Init gui

gui = new dat.GUI
gui.add( kaleidoscope, 'zoom' ).min( 0.25 ).max( 2.0 )
gui.add( kaleidoscope, 'slices' ).min( 6 ).max( 32 ).step( 2 )
gui.add( kaleidoscope, 'radius' ).min( 200 ).max( 500 )
gui.add( kaleidoscope, 'offsetX' ).min( -kaleidoscope.radius ).max( kaleidoscope.radius ).listen()
gui.add( kaleidoscope, 'offsetY' ).min( -kaleidoscope.radius ).max( kaleidoscope.radius ).listen()
gui.add( kaleidoscope, 'offsetRotation' ).min( -Math.PI ).max( Math.PI ).listen()
gui.add( kaleidoscope, 'offsetScale' ).min( 0.5 ).max( 4.0 )
gui.add( options, 'interactive' ).listen()
gui.close()

onChange = =>

  kaleidoscope.domElement.style.marginLeft = -kaleidoscope.radius + 'px'
  kaleidoscope.domElement.style.marginTop = -kaleidoscope.radius + 'px'
    
  options.interactive = no
    
  do kaleidoscope.draw

( c.onChange onChange unless c.property is 'interactive' ) for c in gui.__controllers
            
          
!
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