Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

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

              
                
  <header></header>
  <article></article>
              
            
!

CSS

              
                html, body, h1, h2, h3, h4, h5, h6, p, ol, ul, li, dl,
dt, dd, blockquote, address {
  margin: 0;
  padding: 0;
}
html, body {
  height: 100%;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}
body {
  font-size: 100%;
  line-height: 1;
  -webkit-font-smoothing: antialiased;

  background: white;
  background:radial-gradient(hsl(0, 100%, 30%) 4%, hsl(0, 100%, 18%) 9%, hsla(0, 100%, 20%, 0) 9%) 0 0,
  radial-gradient(hsl(0, 100%, 30%) 4%, hsl(0, 100%, 18%) 8%, hsla(0, 100%, 20%, 0) 10%) 50px 50px,
  radial-gradient(hsla(0, 100%, 35%, 1) 20%, hsla(0, 100%, 20%, 0)) 50px 0,
  radial-gradient(hsla(48, 96%, 35%, .73) 20%, hsla(0, 100%, 20%, 0)) 0 50px,
  radial-gradient(hsla(0, 100%, 20%, 1) 35%, hsla(0, 100%, 20%, 0) 60%) 50px 0,
  radial-gradient(hsla(0, 100%, 20%, 1) 35%, hsla(0, 100%, 20%, 0) 60%) 100px 50px,
  radial-gradient(hsla(0, 100%, 15%, 0.2), hsla(0, 100%, 20%, 0)) 0 0,
  radial-gradient(hsla(0, 100%, 15%, 0.2), hsla(0, 100%, 20%, 0)) 50px 50px,
  linear-gradient(45deg, hsla(0, 100%, 20%, 0) 49%, hsla(0, 100%, 0%, 1) 50%, hsla(0, 100%, 20%, 0) 70%) 0 0,
  linear-gradient(-45deg, hsla(0, 100%, 20%, 0) 49%, hsla(0, 100%, 0%, 1) 50%, hsla(0, 100%, 20%, 0) 70%) 0 0;
  background-color: #300; 
background-size:100px 100px;
}
article {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  margin: 20px;
  padding: 20px;
  height: 80%;
}
header {
  box-shadow: 0 3px 6px rgba(0,0,0,0.5)
}
header > h1 {
  font-family: Sans-serif;
  font-weight: 100;
}
.Clock {
  width: 250px;
  padding: 10px 20px;
  border: solid 1px orange;
  border-radius: 10px;
  box-shadow: 10px 10px 10px rgba(0,0,0,0.5);
  background-color: rgba(150,150,150,0.6);
}    
h2 {
  text-align: center;
  font-family: Sans-serif;
  font-weight: 500;
  color: #fafafa;
  text-shadow: 2px 2px 3px rgba(0,0,0,0.5);
}
p {
  text-align: center;
  padding: 10px 0;
}
 button {
  transition: all .25s ease-out;
  border: solid 1px darkorange;
  border-radius: 4px;
  background-color: darkorange;
  color: #fff;
  font-size: 13pt;
  cursor: pointer;
  padding: 5px;
  box-shadow: 2px 2px 5px rgba(0,0,0,0.5);
}
button:hover {
  background-color: #fff;
  color: darkorange;
  box-shadow: 0 0 0 rgba(0,0,0,0.5);
}
button:active {
  background-color: #FCF8DC,
  box-shadow: inset 0 0 5px darkorange;
}

header {
  padding: 20px;
  background-color: #333;
}
h1 {
  color: #fff;
  margin: 0;
}
              
            
!

JS

              
                /** @jsx h */
const { h, Component } = composi

// Define component for page title:
const title = new Component({
  container: 'header',
  render: (title) => (
    <h1>{title}</h1>
  )
})

title.state = 'Canvas  Clock'

// Define clock component:
class Clock extends Component {
  constructor(props) {
    super(props)
    this.props = props
    this.container = 'article'
    this.state = { time: new Date() }
    this.radius = this.props.size / 2;
    this.drawingContext = null;
    this.hourFormat = props.hourFormat
    this.drawRoman = this.hourFormat.toLowerCase().trim() === "roman";

   }

   render(data) {
     return (
       <div className="Clock">
         <h2>Canvas Clock</h2>
         <canvas width="250" height="250" ref="clockCanvas"/>
         <p><button>Toggle Hour Format</button></p>
       </div>
     )
   }

   getDrawingContext() {
      this.drawingContext = this.element.querySelector('canvas').getContext('2d');
      this.drawingContext.translate(this.radius, this.radius);
      this.radius *= 0.9;
   }

   tick() {
      this.setState({ time: new Date() });
      const radius = this.radius;
      let ctx = this.drawingContext;
      this.drawFace(ctx, radius);
      this.drawNumbers(ctx, radius);
      this.drawTicks(ctx, radius);
      this.drawTime(ctx, radius);
   }

   drawFace(ctx, radius) {
      ctx.beginPath();
      ctx.arc(0,0, radius, 0, 2 * Math.PI);
      ctx.fillStyle = "#FEF6EF";
      ctx.fill();
      const grad = ctx.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05);
      grad.addColorStop(0, "#FF7F50");
      grad.addColorStop(0.5, "white");
      grad.addColorStop(1, "#FF7F50");
      ctx.strokeStyle = grad;
      ctx.lineWidth = radius * 0.1;
      ctx.stroke();
      ctx.beginPath();
      ctx.arc(0, 0, radius * 0.05, 0, 2 * Math.PI);
      ctx.fill();
   }

   drawNumbers(ctx, radius) {
      const romans = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII"];
      const fontBig = radius * 0.15 + "px Arial";
      const fontSmall = radius * 0.075 + "px Arial";
      let ang, num;
      ctx.textBaseline = "middle";
      ctx.textAlign = "center";
      for (num = 1; num < 13; num++) {
         ang = num * Math.PI / 6;
         ctx.rotate(ang);
         ctx.translate(0, -radius * 0.78);
         ctx.rotate(-ang);
         ctx.font = fontBig;
         ctx.fillStyle = "black";
         ctx.fillText(this.drawRoman ? romans[num-1] : num.toString(), 0, 0);
         ctx.rotate(ang);
         ctx.translate(0, radius * 0.78);
         ctx.rotate(-ang);
      }
   }

   drawTicks(ctx, radius) {
      let numTicks, tickAng, tickX, tickY;
      for (numTicks = 0; numTicks < 60; numTicks++) {
         tickAng = (numTicks * Math.PI / 30);
         tickX = radius * Math.sin(tickAng);
         tickY = -radius * Math.cos(tickAng);
         ctx.beginPath();
         ctx.lineWidth = radius * 0.010;
         ctx.moveTo(tickX, tickY);
         if (numTicks % 5 === 0) {
            ctx.lineTo(tickX * 0.88, tickY * 0.88);
         } else {
            ctx.lineTo(tickX * 0.92, tickY * 0.92);
         }
         ctx.stroke();
      }
   }

   drawTime(ctx, radius) {
      const now = this.state.time;
      let hour = now.getHours();
      let minute = now.getMinutes();
      let second = now.getSeconds();

      // hour:
      hour %= 12;
      hour = (hour * Math.PI / 6) + (minute * Math.PI / (6 * 60)) + (second * Math.PI / (360 * 60));
      this.drawHand(ctx, hour, radius * 0.5, radius * 0.05);
      // minute:
      minute = (minute * Math.PI / 30) + (second * Math.PI / (30 * 60));
      this.drawHand(ctx, minute, radius * 0.8, radius * 0.05);
      // second:
      second = (second * Math.PI / 30);
      this.drawHand(ctx, second, radius * 0.9, radius * 0.02, "red");
      this.interval
   }

   drawHand(ctx, position, length, width, color) {
      color = color || "black";
      ctx.beginPath();
      ctx.lineWidth = width;
      ctx.lineCap = "round";
      ctx.fillStyle = color;
      ctx.strokeStyle = color;
      ctx.moveTo(0, 0);
      ctx.rotate(position);
      ctx.lineTo(0, -length);
      ctx.stroke();
      ctx.rotate(-position);
  }

  handleEvent(e) {
    if (e.target.nodeName === 'BUTTON') {
      this.drawRoman = !this.drawRoman
      this.update()
    }
  }

  componentDidMount() {
    this.element.addEventListener('click', this)
    this.getDrawingContext();
    this.interval = setInterval(() => {
      this.tick()
    }, 200)
  }
  
  componentWillUnmount() {
    clearInterval(this.interval)
  }
}

// Create clock instance:
const clock = new Clock({
  size: 250,
  width: 250,
  standard: 'normal',
  hourFormat: "roman"
})
clock.update()
              
            
!
999px

Console