This script is intended to generate a table and convert it into a canvas. Clicking on a cell should change its color to the selected color from the color picker input is retrieved from text boxes and the color picker, and executed upon submitting
I need to create a table with specific dimensions. However, I'm struggling to figure out how to store the values entered in the input boxes for creating the table. Additionally, I also want to save the selected color from the color picker to change the cell colors accordingly.View the page in action here.
Note: The requirement is to solve this using JQuery only.
// Select color input
// Select size input
// When size is submitted by the user, call makeGrid()
var Make=$('#pixel_canvas');
var td=$('td');
var rows=$('#input_width').val();
var cols=$('#input_height').val();
td.css("padding","700px");
function change() {
$('td').click( function() {
$(this).css("background-color","red");
});
}
Make.append(makeGrid());
function makeGrid() {
var table='';
for (var i = 0; i < rows; i++) {
table+='<tr>';
for (var j = 0; j < cols; j++) {
table+='<td onclick="change()"></td>';
}
table+='</tr>';
}
return table;
};
body {
text-align: center;
}
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;
}
<!DOCTYPE html>
<html>
<head>
<title>Pixel Art Maker!</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Monoton">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Lab: Pixel Art Maker</h1>
<h2>Choose Grid Size</h2>
<form id="sizePicker">
Grid Height:
<input type="number" id="input_height" name="height" min="1" value="1">
Grid Width:
<input type="number" id="input_width" name="width" min="1" value="1">
<input type="submit">
</form>
<h2>Pick A Color</h2>
<input type="color" id="colorPicker">
<h2>Design Canvas</h2>
<table id="pixel_canvas"></table>
<script src="designs.js"></script>
</body>
</html>