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

CSS
body {
    text-align: center;
    margin: 0;
    padding-bottom: 15px;
}

h1 {
    font-family: Monoton;
    font-size: 70px;
    margin: 0.2em;
}

h2 {
    margin: 1em 0 0.25em;
}

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

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

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

tr {
    height: 20px;
}

td {
    width: 20px;
}

input[type=number] {
    width: 6em;
}
JS
// When size is submitted by the user, call makeGrid()
$( '#sizePicker' ).submit( 'click', function( event ) {
  event.preventDefault();
  makeGrid(); 
})

/* 
//When size is submitted by the user, call makeGrid() version 2
$('#sizePicker').click('submit', function( event ) {
  event.preventDefault();
  makeGrid(); 
})

//When size is submitted by the user, call makeGrid() version 3
$('#sizePicker').on('submit', function( event ){
  event.preventDefault();
  makeGrid();
})

// NB : $("input[type='submit']") => OK ! $('submit') => NOT OK !
*/

//make the Grid of pixel art
function makeGrid() {
  var canvas = $( '#pixel_canvas' );
  var size = {
    rows: $( '#input_height' ).val(),
    cols: $( '#input_width' ).val(),
  }

  // Grid eraser
  canvas.children().remove();

  // rows builder
  for (var i = 0 ; i < size.rows; i++) {
    canvas.append( '' );
  }

  // cols builder
  for (var j = 0 ; j < size.cols; j++) {
    $( 'tr' ).append( '' );
  }
}

// When the user click on a cell, paint it OR DIE !
$( '#pixel_canvas' ).click( 'td', function(cell) {
  var colorInput = $('#colorPicker').val();
  //$(cell.target).attr('bgcolor', colorInput ); 
  $(cell.target).css('background-color', colorInput);
})
Wed, 12/27/2017 - 07:02

Related Codes

Pen ID
Pen ID
Pen ID
Square Adv