I have written a little Paint-Program in HTML. You can also save and load your wonderful paintings. My code works, but my heart says that my code is not so beautiful. I dont use JS/HTML/CSS very often and thats why I dont know how to write clean JS-Code, but I want to know that. Javascript seems to provide a thousand ways to express your ideas sometimes. I dont know that from Java (I usually write Java Code). How would you improve the code? I am very interested!
/**
* user can choose color by hisself
**/
// global color variable
let currentColor = "black";
function createColorTable(colorStringArray) {
let colorTable = document.getElementById("colorTable");
for (i = 0; i < colorStringArray.length; i++) {
let colorRow = document.createElement("tr");
let colorBox = document.createElement("td");
colorBox.value = colorStringArray[i];
colorBox.style.backgroundColor = colorStringArray[i];
colorBox.onclick = function () {
window.currentColor = colorBox.style.backgroundColor;
};
colorRow.appendChild(colorBox);
colorTable.appendChild(colorBox);
}
}
let colors = [
'black',
'brown',
'red',
'grey', 'lightgrey',
'blue', 'lightblue',
'yellow',
'green', 'lightgreen'
];
createColorTable(colors);
/**
* function for drawing table
**/
function drawPaintingArea(height, width) {
let paintingArea = document.getElementById("paintingArea");
// clear table for redrawing
paintingArea.innerHTML = "";
for (i = 0; i < width; i++) {
let row = document.createElement("tr");
for (j = 0; j < height; j++) {
let column = document.createElement("td");
column.style.backgroundColor = "white";
column.setAttribute("onclick", "tableClickHandler(this)");
row.appendChild(column);
}
paintingArea.appendChild(row);
}
}
function tableClickHandler(box) {
if (box.style.backgroundColor == "white")
box.style.backgroundColor = window.currentColor;
else
box.style.backgroundColor = "white";
}
drawPaintingArea(20, 20);
/**
* draw table by button click
**/
let heightTextField = document.getElementById("height");
let widthTextField = document.getElementById("width");
let drawingButton = document.getElementById("drawingButton");
drawingButton.onclick = function() {
drawPaintingArea(heightTextField.value, widthTextField.value);
};
/**
* saving and loading (via buttons)
**/
let saveButton = document.getElementById("saveButton");
saveButton.onclick = function() {
let paintintArea = document.getElementById("paintingArea");
download("save.txt", paintingArea.innerHTML);
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
document.getElementById('fileInput').addEventListener('change', readFileAsString)
function readFileAsString() {
var files = this.files;
if (files.length === 0) {
console.log('No file is selected');
return;
}
var reader = new FileReader();
reader.onload = function(event) {
let paintintArea = document.getElementById("paintingArea");
paintingArea.innerHTML = event.target.result;
};
reader.readAsText(files[0]);
}
table {
border: 1px solid black;
border-collapse: collapse;
}
td {
height: 20px;
width: 20px;
}
<html>
<head>
<title>Malprogramm Web</title>
<link rel="stylesheet" href="design.css">
</head>
<body>
<h1>Web Painting Program</h1>
<div>
<input id=height value="20" /> Height <br /><br />
<input id=width value="20" /> Width <br /><br />
<button id=drawingButton>Draw Field</button></br></br>
</div>
<h2>Choose Color</h2>
<table id=colorTable>
</table>
<br /><br />
<table border id=paintingArea>
</table>
<br /><br />
<button id=saveButton>Save to file</button><br /><br />
Load: <input type='file' id=fileInput />
<script type="text/javascript" src="main.js"></script>
</body>
</html>