123

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.

            
              html
    background #fff
    
body
    background linear-gradient(#f8f8f0, #ccccc0)
    
    font sans-serif
    
    position absolute
    top 0
    bottom 0
    left 0
    right 0
    
    margin 0
    padding 0
    border 0
    
    display flex
    justify-content center

canvas
    align-self center
    


            
          
!
            
              class Clock
    HandProps =
        hour12:
            begin: 1
            end: 12
            offset: 3
        hour24:
            begin: 0
            end: 23
            offset: 18
        minute:
            begin: 1
            end: 60
            offset: 15
        second:
            begin: 1
            end: 60
            offset: 15
    
    Colors =
        light: '#fff'
        shadow: '#000'
        element: '#333'
        second: '#f00'
        
    Palette =
        brightlava: [ '#300303',	'#490a04',	'#611405',	'#792306',	'#913608',	'#aa4c09',	'#c2660a',	'#da840b',	'#f2a60d',	'#f4c025',	'#f5d63d',	'#f6e955',	'#f7f76e', ]
        sunset: [ '#000066',	'#150080',	'#330099',	'#5900b3',	'#8800cc',	'#bf00e6',	'#ff00ff',	'#ff1ad9',	'#ff33bb',	'#ff4da6',	'#ff6699',	'#ff8095',	'#ff9999', ]
        sunset2: [ '#090317',	'#1c0634',	'#390950',	'#5e0c6d',	'#8a0f86',	'#a61286',	'#c3167e',	'#e0196c',	'#e8305b',	'#eb4d55',	'#ee7c6a',	'#f2a887',	'#f5cca3', ]
        autumn: ['#001a00',	'#063300',	'#134d00',	'#266600',	'#408000',	'#609900',	'#86b300',	'#b3cc00',	'#e5e600',	'#ffdf00',	'#ffc61a',	'#ffb333',	'#ffa64d' ]
        
    constructor: (params) ->
        @color = Colors.element
        @color_second = Colors.second
        @h_size = 22
        @m_size = 11
        @effect_width = 2
        @hand_props_hour = HandProps.hour24
        @palette = 'brightlava'
        if params == undefined
            params = {}
        if params.color != undefined
            @color = params.color
        if params.color_second != undefined
            @color_second = params.color_second
        if params.canvas != undefined
            @canvas = params.canvas
        if params.parent != undefined and @canvas == undefined
            @parent = params.parent
        if params.width != undefined
            @width = params.width
        if params.height != undefined
            @height = params.height
        if params.h_size != undefined
            @h_size = params.h_size
        if params.m_size != undefined
            @m_size = params.m_size
        if params.effect_width != undefined
            @effect_width = params.effect_width
        if params.use_12 != undefined
            if params.use_12
                @hand_props_hour = HandProps.hour12
        if params.palette != undefined
            @palette = params.palette
        if params.events != undefined
            @events = params.events
        if @width == undefined
            if @canvas == undefined
                @width = 500
            else
                @width = @canvas.width
        if @height == undefined
            if @canvas == undefined
                @height = 500
            else
                @height = @canvas.height
        if @canvas != undefined 
            if @parent == undefined
                @parent = @canvas.parentElement
        if @canvas == undefined 
            if @parent == undefined
                @parent = document.body
            @canvas = document.createElement 'canvas'
            @parent.appendChild @canvas
        @canvas.width = @width
        @canvas.height = @height
        #console.log([@canvas, @parent])
        #window.x = [@canvas, @parent]
        @ctx = @canvas.getContext('2d')
        @ctx.textAlign = 'center'
        @ctx.textBaseline = 'middle'
        @clock_radius = Math.min(@ctx.canvas.width, @ctx.canvas.height) / 2
        return

    clean: ->
      # clear canvas function
      @ctx.clearRect 0, 0, @ctx.canvas.width, @ctx.canvas.height
      @ctx.translate (@ctx.canvas.width / 2), (@ctx.canvas.height / 2)
      return

    with_effect: (color, code) ->
        if @effect_width>0
            for index in [1..@effect_width]
                @save()
                @ctx.translate 0, index
                @ctx.fillStyle = Colors.shadow
                @ctx.globalAlpha = 0.5
                code()
                @restore()
        
            for index in [1..@effect_width]
                @save()
                @ctx.translate 0, -index
                @ctx.fillStyle = Colors.light
                @ctx.globalAlpha = 0.5
                code()
                @restore()
        
        @save()
        @ctx.fillStyle = color
        code()
        @restore()
        return
    
    get_theta: (value, hand_props) ->
        return (value - hand_props.offset) * 2 * Math.PI / (hand_props.end - hand_props.begin + 1)
    
    get_xy: (length, theta) ->
        return [@clock_radius * length * Math.cos(theta), @clock_radius * length * Math.sin(theta)]

    iterate_on_hand: (hand_props, code) ->
        for value in [hand_props.begin..hand_props.end]
            theta = @get_theta(value, hand_props)
            code(value, hand_props, theta)
        return
            
    draw_hand: (value, hand_props, width, length, color) ->
        theta = @get_theta(value, hand_props)
        @with_effect color, =>
            @ctx.rotate theta
            @ctx.beginPath()
            @ctx.moveTo -15, -width
            @ctx.lineTo -15, width
            @ctx.lineTo @clock_radius * length, 1
            @ctx.lineTo @clock_radius * length, -1
            @ctx.fill()
            return
        
    draw_tick: (value, theta, radius, length, color) ->
        [x, y] = @get_xy(length, theta)
        @with_effect color, =>
            @ctx.beginPath()
            @ctx.arc x, y, radius, 0, Math.PI * 2, true
            @ctx.closePath()
            @ctx.fill()
            return

    draw_text: (value, theta, text_size, length, color) ->
        [x, y] = @get_xy(length, theta)
        @with_effect color, =>
            @ctx.font = text_size+'px Sans-Serif'
            @ctx.fillText value, x, y
            return

    draw_arc: (hour1, hour2, hand_props, length_min, length_max, color) ->
        theta1 = @get_theta(hour1, hand_props)
        theta2 = @get_theta(hour2, hand_props)
        radius_min = @clock_radius * length_min
        radius_max = @clock_radius * length_max
        @with_effect color, =>
            @ctx.globalAlpha = 0.4
            @ctx.beginPath()
            @ctx.arc 0, 0, radius_max, theta1, theta2, false
            @ctx.arc 0, 0, radius_min, theta2, theta1, true
            @ctx.closePath()
            @ctx.fill()
            return

    draw_event: (event) ->
        for hour_range in event.hour_ranges
            @draw_arc hour_range[0], hour_range[1], @hand_props_hour, 0.2+0.05*event.level, 0.3+0.05*event.level, Palette[@palette][event.color_index]
        return
            
    draw_events: (events) ->
        for event in events
            @draw_event event
        return
        
    
    save: ->
        @ctx.save()
        return
        
    restore: ->
        @ctx.restore()
        return
        
    draw_scene: ->
        @save()
        @clean()
        date = new Date
        hours = date.getHours()
        minutes = date.getMinutes()
        seconds = date.getSeconds()
        numMinute = minutes + seconds / 60
        numHour = hours + numMinute / 60

        @iterate_on_hand @hand_props_hour, (value, hand_props, theta) =>
            @draw_tick(value, theta, 1, 0.6, @color)
            @draw_text(value, theta, @h_size, 0.68, @color)
            return
        
        @iterate_on_hand HandProps.minute, (value, hand_props, theta) =>
            if value % 5 == 0
                @draw_text(value, theta, @m_size, 0.89, @color)
            else
                @draw_tick(value, theta, 1, 0.89, @color)
            return

        if @events != undefined
            @draw_events @events
            
        
        @draw_hand(numHour, @hand_props_hour, 5, 0.5, @color)
        @draw_hand(numMinute, HandProps.minute, 4, 0.8, @color)
        @draw_hand(seconds, HandProps.second, 3, 0.9, @color_second)
        
        @restore()
        return
    
    start: ->
        setInterval (()=>
            @draw_scene()
            return
        ), 1000
        return

# $(document).ready ->
clock = new Clock
        # canvas: $('#canvas')[0]
        # parent: $('.clocks')[0]
        color: '#333'
        effect_width: 1
        use_12: false
        width: 500
        height: 500
        h_size: 22
        m_size: 11
        palette: 'sunset2'
        # palette: 'brightlava'
        events: [
            { 
                hour_ranges:[[8, 12],[13, 17]], 
                level: 1, 
                color_index: 3
            },
            {
                hour_ranges:[[11, 12],[13, 20]], 
                level: 2, 
                color_index: 6
            },
            { 
                hour_ranges:[[22, 5]], 
                level: 3, 
                color_index: 9
            },
            { 
                hour_ranges:[[7.9, 9]], 
                level: 4, 
                color_index: 12
            },
            ]
        
clock.start()


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

Console