css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <div class="container">
  <h1>Lab: Pixel Art Maker</h1>
  <h3>Version 0.3</h3>
  <div class="instruction">
    <p>Please, use values from <b>1</b> to <b>50</b> for size of grid</p>
  </div>
  <h2>Choose Grid Size</h2>
  <form id="sizePicker">
    <label for="height">Grid Height:</label>
    <input type="number" id="input_height" name="height" min="1" max="50" value="30">
    <label for="width">Grid Width:</label>
    <input type="number" id="input_width" name="width" min="1" max="50" value="50">
    <input type="submit" id="submit_canvas_size" value="Submit Size">
  </form>

  <h2>Pick A Color</h2>
  <div class="predefined-colors">
    <div id="white"></div>
    <div id="black"></div>
    <div id="red"></div>
    <div id="green"></div>
    <div id="blue"></div>
    <div id="yellow"></div>
    <div id="purple"></div>
    <div id="cyan"></div>
  </div>
  <label for="colorPicker">Set custom color: </label>
  <input type="color" id="colorPicker" value="#0d47a1">


  <h2>Design Canvas</h2>
  <table id="pixel_canvas"></table>


</div>

<script src="designs.js"></script>
            
          
!
            
              body {
  text-align: center;
  font-family: 'Lato', sans-serif;
  background: url("https://i.imgur.com/nAoQiHb.png") repeat;
  color: #fff;
  box-sizing: border-box;
}

.container {
  margin: 0 auto;
  padding: 5%;
  width: 80%;
  background: radial-gradient(farthest-corner at 10px 10px, #2196f3, #0d47a1);
  box-shadow: 0 5px 15px 5px rgba(15, 15, 15, 0.5);
  border: 7px solid #0d47a1;
  border-radius: 25px;
}

h1 {
  font-family: 'Montserrat', sans-serif;
  font-weight: 700;
  font-size: 70px;
  margin: 0.2em;
}

.instruction  p {
  height: 24px;
  color: #fff;
  background: rgba(13, 71, 161, 0.7);
  font-size: 16px;
  font-weight: 700;
  border-radius: 5px;
}

h2 {
  margin: 1em 0 0.25em;
}

h2:first-of-type {
  margin-top: 0.5em;
}

table,
tr,
td {
  background: #fff;
  border: 1px solid rgba(51, 51, 51, 0.5);
}

td:hover,
.predefined-colors div:hover,
input[type=color]:hover {
  opacity: 0.80;
  cursor: pointer;
}

table {
  border-collapse: collapse;
  margin: 0 auto;
  border: 5px solid #42a5f5;
}

tr {
  height: 20px;
}

td {
  width: 20px;
}

input[type=number] {
  width: 4em;
  margin-right: 15px;
  padding: 5px;
  border: 3px solid #42a5f5;
  border-radius: 3px;
}

input[type=submit] {
  width: 120px;
  height: 30px;
  background: #42a5f5;
  color: #fff;
  font-weight: bold;
  border: none;
  border-bottom: 3px solid #0d47a1;
  border-radius: 3px;
}

input[type=submit]:hover {
  background: #0d47a1;
  cursor: pointer;
}

input[type=color] {
  background: #42a5f5;
  border: none;
  border-radius: 5px;
  width: 30px;
  height: 25px;
}

.predefined-colors {
  margin: 0 auto 15px auto;
}

.predefined-colors div {
  display: inline-block;
  border: 3px solid #42a5f5;
  border-radius: 10px;
  width: 30px;
  height: 20px;
}

#white {
  background: #fff;
}

#black {
  background: #000;
}

#red {
  background: #f00;
}

#green {
  background: #0f0;
}

#blue {
  background: #00f;
}

#yellow {
  background: #ff0;
}

#purple {
  background: #f0f;
}

#cyan {
  background: #0ff;
}

            
          
!
            
              'use strict';

// Defining function used to make grid
function makeGrid() {
  // Removing previous grid if it exists
  $('tr').remove();
  // Setting variables for width and height
  const canvasWidth = $('#input_width').val();
  const canvasHeight = $('#input_height').val();
  // Conditional for checking correct input
  // NOT NEEDED ANYMORE

  // if (((canvasWidth < 1) || (canvasWidth > 50) || (canvasHeight < 1) || (canvasHeight > 50))) {
  //   $('.warning p').css('visibility', 'visible');
  //   $('.warning p').fadeOut(7000, function() {
  //     $(this).css('display', 'block');
  //     $(this).css('visibility', 'hidden');
  //   });
  // } else {

  // Actual function making all hard work :)
  for (let i = 0; i < canvasHeight; i++) {
    $('#pixel_canvas').append('<tr></tr>');
    for (let i = 0; i < canvasWidth; i++) {
      $('tr:last-of-type').append('<td></td>');
    };
  };
}

// Defining function for rgb() to hex color conversion
function searchRGB() {
  let i = colorPicked;
  let rgbValues = i.split("(")[1].split(")")[0].split(", ");
  return rgbValues;
}

function componentToHex(c) {
  var hex = Number(c).toString(16);
  return hex.length == 1 ? "0" + hex : hex;
}

function rgbToHex(r, g, b) {
  return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}

// Calling the function makeGrid()
$('#sizePicker').submit(function(evt) {
  evt.preventDefault();
  makeGrid();
})

// Select color input
let colorPicked = $('#colorPicker').val();
$('#colorPicker').on('change', function() {
  colorPicked = $('#colorPicker').val();
});
$('.predefined-colors').on('click', function(evt) {
  colorPicked = $(evt.target).css("background-color");
  $('#colorPicker').val(rgbToHex(searchRGB()[0], searchRGB()[1], searchRGB()[2]));
});

// Drawing on the canvas stuff (old version)
// $('#pixel_canvas').on('click', 'td', function(evt) {
//   $(evt.target).css('background-color', colorPicked);
// });

// Drawing on the canvas stuff (WIP)
let isDrawing = false;
$('#pixel_canvas').on('mousedown', 'td', function(evt) {
  evt.preventDefault();
  isDrawing = true;
  if (isDrawing) {
    $(evt.target).css('background-color', colorPicked);
  };
});
$('#pixel_canvas').on('mouseenter', 'td', function(evt) {
  if (isDrawing) {
    $(evt.target).css('background-color', colorPicked);
  }
});
$('#pixel_canvas').on('mouseup', 'td', function(evt) {
  isDrawing = false;
});
$('#pixel_canvas').on('mouseleave', function(evt) {
  isDrawing = false;
});

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

Console