pixel-art-maker/index.js

38 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-06-10 13:03:07 +05:30
const WIDTH = 48;
const HEIGHT = 24;
2018-06-10 12:29:56 +05:30
2018-06-10 13:00:54 +05:30
// State
let brushColor = '';
2018-06-10 12:46:13 +05:30
// Canvas
const canvas = document.createElement('div');
canvas.className = 'canvas';
2018-06-10 12:29:56 +05:30
for(let y = 0; y < HEIGHT; y++) {
const row = document.createElement('div');
row.className = 'row';
for(let x = 0; x < WIDTH; x++) {
const pixel = document.createElement('div');
pixel.className = 'pixel';
2018-06-10 12:35:59 +05:30
pixel.addEventListener('click', () => {
2018-06-10 13:00:54 +05:30
pixel.style.backgroundColor = brushColor;
2018-06-10 12:35:59 +05:30
});
2018-06-10 12:29:56 +05:30
row.appendChild(pixel);
}
2018-06-10 12:46:13 +05:30
canvas.appendChild(row);
}
document.body.appendChild(canvas);
// Palette
2018-06-10 13:15:37 +05:30
const colors = ['#b23232', '#ff4848', '#ff6c6c', '#e59b40', '#ffad48', '#ffc57e', '#e5de40', '#fff748', '#fffa91', '#39cc4b', '#48ff5e', '#91ff9e', '#3248b2', '#4867ff', '#91a3ff', '#6432b2', '#8f48ff', '#bb91ff', '#7c2b99', '#cf48ff', '#e291ff', '#000000', '#323232', '#666666', '#999999', '#cccccc', '#ffffff', '#3a2119', '#512e23', '#754233', '#90675b', '#ac8d84'];
2018-06-10 12:46:13 +05:30
const palette = document.createElement('div');
palette.className = 'palette';
for(let color of colors) {
const swatch = document.createElement('div');
swatch.className = 'swatch';
swatch.style.backgroundColor = color;
2018-06-10 13:00:54 +05:30
swatch.addEventListener('click', () => {
brushColor = color;
});
2018-06-10 12:46:13 +05:30
palette.appendChild(swatch);
}
document.body.appendChild(palette);