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.

              <main class="container">
  <div class="row">
    <h1 class="col-xs-12">Canvas Grayscale Sliders</h1>
  <div class="row">
    <canvas id="c" width="500" height="400"></canvas>

  <form class="row">
    <div class="form-group col-xs-4">
      <label for="red">Red: <span>100%</span></label>
      <input type="range" id="red" min="0" max="100" value="100">
    <div class="form-group col-xs-4">
      <label for="green">Green: <span>100%</span></label>
      <input type="range" id="green" min="0" max="100" value="100">
    <div class="form-group col-xs-4">
      <label for="blue">Blue: <span>100%</span></label>
      <input type="range" id="blue" min="0" max="100" value="100">
              .container {
  max-width: 500px;
body {
  background: #403E3E;
  color: #fff;
.preview {
  max-width: 100%;
canvas {
  border: 1px solid #000;
              var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');
var readPixels;
var writePixels;

// sliders
var redControl = document.getElementById('red');
var greenControl = document.getElementById('green');
var blueControl = document.getElementById('blue');

// create image object
var imageObj = new Image();

// set image source
// I have to load an external data URI (puppy.js) to make the Pen work
imageObj.src = img;

imageObj.onload = function() {
  // resize and center the image
  var scaleDiff;
  var xOffset = 0;
  var yOffset = 0;
  if (this.width > this.height) {
    scaleDiff = canvas.height / this.height;
    xOffset = ( (this.width*scaleDiff) - canvas.width)/2;
  } else if (this.height > this.width) {
    scaleDiff = canvas.width / this.width;
    yOffset = ( (this.height*scaleDiff) - canvas.height)/2;
  } else {
    // square, width = height
    scaleDiff = canvas.height / this.height;

  // apply the scale and center info
  ctx.drawImage(this, 0-xOffset, 0-yOffset, this.width*scaleDiff, this.height*scaleDiff);

  // we need two copies
  // readPixels is a reference to the original image
  readPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
  writePixels = ctx.getImageData(0, 0, canvas.width, canvas.height);


var applyGrayscale = function() {

  // slider inputs
  var rScale = redControl.value/100;
  var gScale = greenControl.value/100;
  var bScale = blueControl.value/100;

  // var adjustment = (rAdj + gAdj + bAdj)/(rAdj + gAdj + bAdj);
  var pixelLength =;
  for (var i = 0; i < pixelLength; i++ ) {
    if (i % 4 == 0) {
      // get values
      var r =[i] * rScale;
      var g =[i + 1] * gScale;
      var b =[i + 2] * bScale;

      // find average
      var averageValues = (r + g + b);
      var averageScale = (rScale + gScale + bScale);
      if (averageScale < 1) {
        averageScale = 1;
      var average = averageValues / averageScale;

      // put average back[i] = average; //red[i + 1] = average; //green[i + 2] = average; //blue

  ctx.putImageData(writePixels, 0, 0);

redControl.onchange = function() {
  var label = document.querySelector('label[for="red"] span');
  label.innerHTML = this.value + '%';
greenControl.onchange = function() {
  var label = document.querySelector('label[for="green"] span');
  label.innerHTML = this.value + '%';
blueControl.onchange = function() {
  var label = document.querySelector('label[for="blue"] span');
  label.innerHTML = this.value + '%';

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