Pixel Art Code using HTML and JavaScript
Hello Coder, Welcome to the Codewithrandom. today we’re going to learn how to Create Pixel Art Using Html, Css, and JavaScript Code.
By following these instructions, you can simply make this Pixel Art Maker That can draw anything. I create a heart for you now it’s your turn to create something for me.
Using JavaScript. Simply by adhering to the procedures mentioned below, you will be able to develop this amazing Pixel Art Maker.
How to Make Pixel Art Code
Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Pixel Art Maker Project.
5+ HTML CSS Projects With Source Code
Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Pixel Art Maker Project.
Step 3
At last we will use JS (JavaScript) which will add a logic to make the Pixel Art Maker Project functioning from the user end.
I hope you have got an idea about the project.
Pixel Art Html Code:-
<html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Pixel Art Generator</title> <!-- Google Fonts --> <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@500&display=swap" rel="stylesheet" /> <!-- Stylesheet --> <link rel="stylesheet" href="style.css" /> </head> <body> <div class="wrapper"> <div class="options"> <div class="opt-wrapper"> <div class="slider"> <label for="width-range">Grid Width</label> <input type="range" id="width-range" min="1" max="35" /> <span id="width-value">00</span> </div> <div class="slider"> <label for="height-range">Grid Height</label> <input type="range" id="height-range" min="1" max="35" /> <span id="height-value">00</span> </div> </div> <div class="opt-wrapper"> <button id="submit-grid">Create Grid</button> <button id="clear-grid">Clear Grid</button> <input type="color" id="color-input" /> <button id="erase-btn">Erase</button> <button id="paint-btn">Paint</button> </div> </div> <div class="container"></div> </div> <!-- Script --> <script src="script.js"></script> </body> </html>
First we’ll start with creating the structure of the Pixel Art Maker project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.
Pixel Art Css Code:-
100+ JavaScript Projects With Source Code ( Beginners to Advanced)
* { padding: 0; margin: 0; box-sizing: border-box; font-family: "Roboto Mono", monospace; } body { background-color: #f4c531; } .wrapper { background-color: #ffffff; width: 80vmin; position: absolute; transform: translate(-50%, -50%); top: 50%; left: 50%; padding: 40px 20px; border-radius: 8px; } label { display: block; } span { position: relative; font-size: 22px; bottom: -1px; } .opt-wrapper { display: flex; justify-content: space-between; margin-bottom: 20px; gap: 10px; } button { background-color: #f4c531; border: none; border-radius: 5px; padding: 5px; } input[type="color"] { -webkit-appearance: none; -moz-appearance: none; appearance: none; background-color: transparent; width: 70px; height: 40px; border: none; cursor: pointer; } input[type="color"]::-webkit-color-swatch { border-radius: 8px; border: 4px solid #000000; } input[type="color"]::-moz-color-swatch { border-radius: 8px; border: 4px solid #000000; } .gridCol { height: 1em; width: 1em; border: 1px solid #ddd; } .gridRow { display: flex; } @media only screen and (max-width: 768px) { .gridCol { height: 0.8em; width: 0.8em; } }
Second comes the CSS code, which is mentioned above in that we have styled for the structure we have padded as well as aligned the Pixel Art Maker project so that it is properly situated and doesn’t get messy with suitable CSS elements.
Now we have created the structure using HTML and styled the webpage using CSS its time to add the functionality using JavaScript in this project.
Pixel Art Javascript Code:-
//Initial references let container = document.querySelector(".container"); let gridButton = document.getElementById("submit-grid"); let clearGridButton = document.getElementById("clear-grid"); let gridWidth = document.getElementById("width-range"); let gridHeight = document.getElementById("height-range"); let colorButton = document.getElementById("color-input"); let eraseBtn = document.getElementById("erase-btn"); let paintBtn = document.getElementById("paint-btn"); let widthValue = document.getElementById("width-value"); let heightValue = document.getElementById("height-value"); //Events object let events = { mouse: { down: "mousedown", move: "mousemove", up: "mouseup", }, touch: { down: "touchstart", move: "touchmove", up: "touchend", }, }; let deviceType = ""; //Initially draw and erase would be false let draw = false; let erase = false; //Detect touch device const isTouchDevice = () => { try { //We try to create TouchEvent(it would fail for desktops and throw error) document.createEvent("TouchEvent"); deviceType = "touch"; return true; } catch (e) { deviceType = "mouse"; return false; } }; isTouchDevice(); //Create Grid gridButton.addEventListener("click", () => { //Initially clear the grid (old grids cleared) container.innerHTML = ""; //count variable for generating unique ids let count = 0; //loop for creating rows for (let i = 0; i < gridHeight.value; i++) { //incrementing count by 2 count += 2; //Create row div let div = document.createElement("div"); div.classList.add("gridRow"); //Create Columns for (let j = 0; j < gridWidth.value; j++) { count += 2; let col = document.createElement("div"); col.classList.add("gridCol"); /* We need unique ids for all columns (for touch screen specifically) */ col.setAttribute("id", `gridCol${count}`); /* For eg if deviceType = "mouse" the statement for the event would be events[mouse].down which equals to mousedown if deviceType="touch" the statement for event would be events[touch].down which equals to touchstart */ col.addEventListener(events[deviceType].down, () => { //user starts drawing draw = true; //if erase = true then background = transparent else color if (erase) { col.style.backgroundColor = "transparent"; } else { col.style.backgroundColor = colorButton.value; } }); col.addEventListener(events[deviceType].move, (e) => { /* elementFromPoint returns the element at x,y position of mouse */ let elementId = document.elementFromPoint( !isTouchDevice() ? e.clientX : e.touches[0].clientX, !isTouchDevice() ? e.clientY : e.touches[0].clientY ).id; //checker checker(elementId); }); //Stop drawing col.addEventListener(events[deviceType].up, () => { draw = false; }); //append columns div.appendChild(col); } //append grid to container container.appendChild(div); } }); function checker(elementId) { let gridColumns = document.querySelectorAll(".gridCol"); //loop through all boxes gridColumns.forEach((element) => { //if id matches then color if (elementId == element.id) { if (draw && !erase) { element.style.backgroundColor = colorButton.value; } else if (draw && erase) { element.style.backgroundColor = "transparent"; } } }); } //Clear Grid clearGridButton.addEventListener("click", () => { container.innerHTML = ""; }); //Erase Button eraseBtn.addEventListener("click", () => { erase = true; }); //Paint button paintBtn.addEventListener("click", () => { erase = false; }); //Display grid width and height gridWidth.addEventListener("input", () => { widthValue.innerHTML = gridWidth.value < 9 ? `0${gridWidth.value}` : gridWidth.value; }); gridHeight.addEventListener("input", () => { heightValue.innerHTML = gridHeight.value < 9 ? `0${gridHeight.value}` : gridHeight.value; }); window.onload = () => { gridWidth.value = 0; gridHeight.value = 0; };
Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. In this JS code we have defined all the functions which are present in structure and added logic according to need so that it acts as per the user indication.
Let us see the Final Output of the project.
Final Output Of Pixel Art Code:-
Live Preview of Pixel Art Maker using JavaScript:-
We have successfully created our Pixel Art Code. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.
Ecommerce Website Using HTML, CSS, & JavaScript (Source Code)
If you find out this Blog helpful, then make sure to search Codewithrandom on Google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.
Code Idea – codingartist
Written By – Harsh Sawant
Code By – @harshh9