Skip to content

Instantly share code, notes, and snippets.

@fangmarks
Created December 31, 2018 15:36
Show Gist options
  • Select an option

  • Save fangmarks/927f50d5ca18ff281a787589a66aa852 to your computer and use it in GitHub Desktop.

Select an option

Save fangmarks/927f50d5ca18ff281a787589a66aa852 to your computer and use it in GitHub Desktop.
Fractal Clock

Fractal Clock

Port of the Mac fractal clock screensaver, with some added animation fanciness, written using Sketch.js.

A Pen by Nick Hogle on CodePen.

License.

window.ctx = Sketch.create(
setup: ->
@push()
@MaxDepth = 6
getTime: ->
now = new Date
midnight = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0)
(now.getTime() - midnight.getTime()) / 1000
getTimeFraction: (t, period) ->
(t % period) / period
getAngle: (f) ->
f * 360 * Math.PI / 180
drawLine: (length, angle) ->
@push()
@beginPath()
@rotate(angle)
@moveTo(0, 0)
@lineTo(length, 0)
@stroke()
@pop()
drawLeg: (length, t, recurse, depth) ->
if (recurse <= 0)
return
s = @getTimeFraction(t, 60)
sleftover = t % 1
fracS = (Math.floor(s*60) + @elasticTween(sleftover)) / 60
#fracS = @getAngle(@getTimeFraction(t, 60))
fracM = @getTimeFraction(t, 60*60)
fracH = @getTimeFraction(t, 60*60*12)
opacity = Math.pow( 1.0 - (depth/(@MaxDepth+1)), 3.2 )
@strokeStyle = "rgba(255, 255, 255, #{opacity}"
doLeg = (scale, frac) =>
@push()
angle = @getAngle( frac )
@drawLine( length*scale, angle )
@rotate( angle )
@translate( length*scale, 0 )
@drawLeg( length*0.8, t-0.02*depth, recurse-1, depth+1 )
@pop()
doLeg( 1.1, fracS )
doLeg( 1.0, fracM )
doLeg( 0.6, fracH )
elasticTween: (t) ->
freq = 4.0
pow = 16.0
Math.pow(2,-pow*t) * Math.sin((t-1.0/(4*freq))*(2*Math.PI)*freq) + 1
draw: ->
t = @t = @getTime()
@push()
@lineWidth = 2
@translate @width / 2, @height / 2
@rotate( -90 * Math.PI / 180 )
@drawLeg( Math.min(@width, @height)/@MaxDepth, t, @MaxDepth, 0 )
@pop()
push: ->
@save()
pop: ->
@restore()
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/sketch.js/1.0/sketch.min.js"></script>
html, body
height: 100%
margin: 0
position: absolute
background: #333
overflow: hidden
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment