LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code



  Pixel Art Maker!
  
  
  



  

Lab: Pixel Art Maker

Choose Grid Size

  • Grid Height:
  • Grid Width:

Pick A Color

Design Canvas

*You can draw clicking/dragging left mouse and erase clicking/dragging the right*

CSS
body {
  text-align: center;
  font-family: "Pangolin", cursive;
  background: #4caf50;
  color: #ffe082;
  margin: 0;
}

.pixelArt {
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.15);
  margin: 5vh auto 0;
  transition: all 0.5s;
  width: calc(100vw - 60px);
  background-color: #fff;
  border-radius: 3px;
}

h1 {
  font-family: "Cabin Sketch", cursive;
  font-size: 70px;
  margin: 0;
  color: green;
  background: #ffe082;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.15);
  border-radius: 3px 3px 0 0;
  padding: 15px;
}

.container {
  display: flex;
  justify-content: space-between;
  min-height: 500px;
}

.control {
  width: 250px;
  background: green;
  padding: 15px;
  box-sizing: border-box;
}

h2 {
  margin: 0;
  padding: 0.5em 0;
  font-size: 30px;
  color: #a7ffeb;
}

.canvas {
  flex: 1;
  padding: 15px;
  box-sizing: border-box;
  color: green;
}

.canvas h2 {
  color: green;
}

#sizePicker {
  margin-bottom: 2em;
}

#sizePicker ul {
  margin: 0;
  padding: 0;
}

#sizePicker li {
  list-style: none;
  margin-bottom: 0.5em;
  color: #ffe082;
}

table,
tr,
td {
  border: 1px solid #ccc;
}

table {
  border-collapse: collapse;
  margin: 1em auto 0;
}

tr {
  height: 20px;
}

td {
  width: 20px;
  background: white;
}

form {
  font-size: 24px;
}

form input[type="button"] {
  display: block;
  margin: 1em auto 0;
}

input {
  margin: 0;
  padding: 0;
  border: none;
  border-radius: 3px;
  outline: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  font-size: 16px;
  font-family: "Pangolin", cursive;
}

input[type="number"] {
  width: 3em;
  padding: 0.3em 0;
  text-align: center;
  font-size: 24px;
  margin-left: 0.3em;
}

input[type="button"] {
  display: inline-block;
  padding: 0.3em 1em;
  text-decoration: none;
  color: #a7ffeb;
  border: solid 2px #2d35a2;
  transition: 0.4s;
  cursor: pointer;
  background: #2d35a2;
  box-sizing: border-box;
  margin-bottom: 0.25em;
}

input[type="button"]:hover {
  background: #a7ffeb;
  color: #2d35a2;
  border-color: #a7ffeb;
}

input[type="color"] {
  width: 32px;
  height: 32px;
  cursor: pointer;
  border: 2px solid #fff;
  box-sizing: border-box;
}

input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}

input[type="color"]::-webkit-color-swatch {
  border: none;
}

@media (max-width: 768px) {
  .pixelArt {
    height: 100%;
    width: calc(100vw - 40px);
    margin: 2vh auto;
  }
  .container {
    display: block;
    height: 100%;
  }
  .control {
    width: 100%;
    margin-bottom: 2em;
  }
  #sizePicker {
    margin-bottom: 1em;
  }
}
JS
//Global variable for memorizing design state
let saved;

//Global variable for '#pixel_canvas td'
let tdlist;

//Global variable for '#pixel_canvas'
let canvas = $("#pixel_canvas");

// make grid when loading
makeGrid();

// When size is submitted by the user, call makeGrid()
function makeGrid() {
  canvas.find("*").remove();

  let c = $("#input_height").val();
  let r = $("#input_width").val();
  let tablebody = "";
  for (let x = 0; x < c; x++) {
    tablebody += "";
    for (let y = 0; y < r; y++) {
      tablebody += "";
    }
    tablebody += "";
  }
  canvas.append(tablebody);

  //Intialize to correct array size
  saved = Array(c * r);

  //Add mouse event handling for td
  canvas.on("mouseover mousedown contextmenu", "td", function(event) {
    event.preventDefault();
    if (event.buttons === 1) {
      let color = $("#colorPicker").val();
      $(this).css("background-color", color);
    } else if (event.buttons === 2) {
      $(this).css("background-color", "white");
    }
  });

  tdlist = $("#pixel_canvas td");
}

//Make grid
$("#submit").on("click", makeGrid);

//Memorize colors to global variable
$("#save").on("click", function() {
  tdlist.each(function(index) {
    saved[index] = $(this).css("background-color");
  });
});

//Revert to memorized colors
$("#revert").on("click", function() {
  tdlist.each(function(index) {
    $(this).css("background-color", saved[index]);
  });
});

//Clear all colors
$("#reset").on("click", function() {
  tdlist.css("background-color", "white");
});

//Invert colors
$("#invert").on("click", function() {
  tdlist.each(function() {
    let tdcolor = $(this).css("background-color");
    let parts = tdcolor.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    delete parts[0];
    let newr = 255 - parts[1];
    let newg = 255 - parts[2];
    let newb = 255 - parts[3];
    let tdnewcolor = "rgb(" + newr + "," + newg + "," + newb + ")";
    $(this).css("background-color", tdnewcolor);
  });
});
Wed, 12/27/2017 - 07:02

Related Codes

Pen ID
Pen ID
Pen ID
Square Adv