CSS

CSS Pixel Art Generator

Sponsor

Welcome to the CSS pixel art generator - written in Javascript, and inspired by this article. Draw your pixel creations below, and then click "Generate CSS" to get your pixel art in CSS, which you can copy into your web pages.

Demoes
Mario
Yoshi
Babomb

How it works

As described in the article, we use scaled up box shadows to act as pixels on the screen. We can scale these up and down to make our pixel art bigger or smaller.

Since each box shadow is 1px by 1px, we can create a piece of pixel art where every "pixel" is 1x1. If we want each pixel to be 20x20, we would simply use transform to scale it by 20x:

css Copy
transform: scale(20);

To achieve the effect we are after, we then use Javascript to create a UI which lets us draw our pixel art creations. The code for the UI can be found on codepen here., or if you like, find it below:

See the Pen CSS Pixel Art Generator by smpnjn (@smpnjn) on CodePen.

Overview of Javascript

To get this all to work, we have to use Javascript. The first step was generating a grid of pixels using a simple loop:

javascript Copy
let config = { width: 40, height: 40, color: 'white', drawing: true, eraser: false } let events = { mousedown: false } document.getElementById('pixel-art-area').style.width = `calc(${(0.825 * config.width)}rem + ${(config.height * 2)}px)`; document.getElementById('pixel-art-area').style.height = `calc(${(0.825 * config.height)}rem + ${(config.width * 2)}px)`; document.getElementById('pixel-art-options').style.width = `calc(${(0.825 * config.width)}rem + ${(config.height * 2)}px)`; for(let i = 0; i < config.width; ++i) { for(let j = 0; j < config.height; ++j) { let createEl = document.createElement('div'); createEl.classList.add('pixel'); createEl.setAttribute('data-x-coordinate', j); createEl.setAttribute('data-y-coordinate', i); document.getElementById('pixel-art-area').appendChild(createEl); } }

This ultimately creates about 40x40 pixels, or 1600 new HTML elements. You can easily scale this up for bigger experiments, but 40x40 works fine.

Tracking a users mouse movements

We can then track a user's mouse movements with three events: pointerdown, pointermove and pointerup. Since we have to apply this to all pixels, we use a loop to loop over each pixel to add the event.

Then, if a user continues to hold down, we can track which pixel they are over using e.target, which returns the current HTML entity which is being hovered over on pointermove. If they are using the eraser, we can take that into consideration here.

javascript Copy
document.querySelectorAll('.pixel').forEach(function(item) { item.addEventListener('pointerdown', function(e) { if(config.eraser === true) { item.setAttribute('data-color', null); item.style.background = `#101532`; } else { item.setAttribute('data-color', config.color); item.style.background = `${config.color}`; } events.mousedown = true; }); }); document.getElementById('pixel-art-area').addEventListener('pointermove', function(e) { if(config.drawing === true && events.mousedown === true || config.eraser === true && events.mousedown === true) { if(e.target.matches('.pixel')) { if(config.eraser === true) { e.target.setAttribute('data-color', null); e.target.style.background = `#101532`; } else { e.target.setAttribute('data-color', config.color); e.target.style.background = `${config.color}`; } } } }); document.body.addEventListener('pointerup', function(e) { events.mousedown = false; });

Finally, we set up a few events on the colors and eraser, so we can track which tool and color is being selected:

javascript Copy
[ 'click', 'input' ].forEach(function(item) { document.querySelector('.color-picker').addEventListener(item, function() { config.color = this.value; document.querySelectorAll('.colors > div').forEach(function(i) { i.classList.remove('current'); }); this.classList.add('current'); config.eraser = false; document.querySelector('.eraser').classList.remove('current'); }); }); document.querySelectorAll('.colors > div').forEach(function(item) { item.addEventListener('click', function(e) { document.querySelector('.color-picker').classList.remove('current'); document.querySelectorAll('.colors > div').forEach(function(i) { i.classList.remove('current'); }) item.classList.add('current'); config.eraser = false; config.color = `${item.getAttribute('data-color')}`; document.querySelector('.eraser').classList.remove('current'); }) });

Conclusion

When I saw the original article, I thought it was really cool to create pixel art with just CSS - but it would be even cooler to create a way to export pixel art creations - and it wasn't so hard with just a little bit of Javascript. Here are some useful links to the source code:

Last Updated Sunday, 21 November 2021

Subscribe to Newsletter

Subscribe to stay up to date with our latest web development and software engineering posts via email. You can opt out at any time.

Not a valid email