Pixel Art Maker Using Typescript (original) (raw)

Last Updated : 27 Jul, 2025

A Pixel Art Maker lets users create art by colouring squares on a grid, like retro pixel graphics. This project uses TypeScript and DOM manipulation for a fun and creative experience.

What We’re Going to Create

We are going to create a pixel Art maker with the following components.

**Project Preview

Screenshot-2025-01-28-085653

Pixel Art Maker using Typescript

Pixel Art Maker - HTML and CSS code

This code creates a simple interactive grid where users can change the grid size, pick a color, and reset the grid. The layout is styled using CSS.

HTML `

Reset

`

**In this Example

Pixel Art Maker - Typescript logic

This code creates a dynamic pixel art grid, enabling users to draw by hovering or clicking, with adjustable grid size and color options. It includes event handling for user interactions and grid reset functionality.

JavaScript `

const gCont = document.querySelector(".grid"); const gSize = document.querySelector('.sz'); const gColor = document.querySelector('.clr'); const gReset = document.querySelector('.btn');

if (!gCont || !gSize || !gColor || !gReset) { throw new Error("Missing required DOM elements"); }

let gridSize: number = parseInt(gSize.value); let isDrawing: boolean = false;

function createGrid(): void { gCont.style.setProperty("--sz", gridSize.toString()); gCont.innerHTML = "";

for (let i = 0; i < gridSize * gridSize; i++) {
    const cell = document.createElement("div");
    cell.classList.add("cell");
    cell.addEventListener('mouseenter', () => paintCell(cell));
    cell.addEventListener('mousedown', () => clickCell(cell));
    gCont.appendChild(cell);
}

}

function paintCell(cell: HTMLDivElement): void { if (!isDrawing) return; cell.style.backgroundColor = gColor.value; }

function clickCell(cell: HTMLDivElement): void { cell.style.backgroundColor = gColor.value; }

window.addEventListener('mousedown', () => { isDrawing = true; });

window.addEventListener('mouseup', () => { isDrawing = false; });

function resetGrid(): void { createGrid(); }

gReset.addEventListener('click', resetGrid);

gSize.addEventListener('change', () => { gridSize = parseInt(gSize.value); resetGrid(); });

createGrid();

`

**In this example

Convert to JavaScript File

Now You need to convert the TypeScript file into JavaScript to render by browser. Use one of the following command.

npx tsc codes.ts
tsc codes.ts

Complete code

HTML `

Reset

`