Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML

              
                <h1>Traffic Lights, yay!</h1>
<canvas id="canvas" width="500" height="500" style="border: 1px solid black"></canvas>
<p>Optional: Read the <a target="_blank" href="http://junerockwell.com/traffic-lights-in-html5-canvas-and-javascript/">Backstory</a></p>
              
            
!

CSS

              
                
              
            
!

JS

              
                const RECT_W = 200
const RECT_H = 400;
const LIGHT_RADIUS = 50;
const OFF_LIGHT_COLOR = 'gray';
const RED_LIGHT_ON = 'red';
const YELLOW_LIGHT_ON = 'yellow';
const GREEN_LIGHT_ON = 'green';
const SWITCH_LIGHT_TIMER = 30;
const RED_LIGHT_Y = 100;
const YELLOW_LIGHT_Y = 220;
const GREEN_LIGHT_Y = 340;
const TOTAL_LIGHTS = 3;

let canvas, context;
let centerX, centerY;
let swithLightTimer = SWITCH_LIGHT_TIMER;
let whichLightOn = 0;

window.onload = function() {
  canvas = document.getElementById("canvas");
  context = canvas.getContext("2d");
  
  centerX = canvas.width/2;
  
  setInterval(() => {
    resetCanvas();
    drawEverything();
  }, 1000/30);
};

function resetCanvas() {
  context.fillStyle = '#fff';
  context.fillRect(0,0, canvas.width, canvas.height);
}

function drawEverything() {
  context.fillStyle = 'black';
  context.fillRect(centerX-RECT_W/2, 20, RECT_W, RECT_H);
  
  if (whichLightOn === 0) {
    toggleRedLight(true)
  } else {
    toggleRedLight(false);
  }
  
  if (whichLightOn === 1) {
    toggleGreenLight(true)
  } else {
    toggleGreenLight(false);
  }
  
  if (whichLightOn === 2) {
    toggleYellowLight(true)
  } else {
    toggleYellowLight(false);
  }
  
  startTimer();
}

function startTimer() {
  swithLightTimer--;
  if (swithLightTimer === 0) {
    swithLightTimer = SWITCH_LIGHT_TIMER;
    switchLights();
  }
}

function switchLights() {
  whichLightOn++;
  if (whichLightOn === TOTAL_LIGHTS) {
    whichLightOn = 0;
  } 
}

function drawLight(color, centerY) {
  context.beginPath();
  context.arc(centerX, centerY, LIGHT_RADIUS, 0, 2 * Math.PI, false);
  context.fillStyle = color;
  context.fill();
  context.closePath();
}

function toggleRedLight(turnOn) {
  let color = OFF_LIGHT_COLOR;
  if (turnOn) {
    color = RED_LIGHT_ON;
  }
  
  drawLight(color, RED_LIGHT_Y);
}

function toggleYellowLight(turnOn) {
  let color = OFF_LIGHT_COLOR;
  if (turnOn) {
    color = YELLOW_LIGHT_ON;
  }
  
  drawLight(color, YELLOW_LIGHT_Y);
}

function toggleGreenLight(turnOn) {
  let color = OFF_LIGHT_COLOR;
  if (turnOn) {
    color = GREEN_LIGHT_ON;
  }
  
  drawLight(color, GREEN_LIGHT_Y);
}

              
            
!
999px

Console