123 lines
2.6 KiB
JavaScript
123 lines
2.6 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
class App
|
||
|
{
|
||
|
constructor() {
|
||
|
this.createCanvas(16,16);
|
||
|
this.createPalette();
|
||
|
}
|
||
|
|
||
|
createCanvas( width, height ) {
|
||
|
let holder = document.querySelector( '#canvasHolder' );
|
||
|
let canvas = document.createElement( 'canvas' );
|
||
|
|
||
|
canvas.id = 'artboard';
|
||
|
canvas.width = width;
|
||
|
canvas.height = height;
|
||
|
|
||
|
holder.appendChild( canvas );
|
||
|
|
||
|
canvas.addEventListener( 'click', this.handleCanvas );
|
||
|
|
||
|
this.canvas = canvas;
|
||
|
this.context = canvas.getContext( '2d' );
|
||
|
this.context.imageSmoothingEnabled = false;
|
||
|
|
||
|
this.context.fillStyle = 'black';
|
||
|
this.context.fillRect( 0, 0, width, height );
|
||
|
}
|
||
|
|
||
|
createPalette() {
|
||
|
let paletteItBe = [
|
||
|
"0 0 0",
|
||
|
"20 12 28",
|
||
|
"68 36 52",
|
||
|
"48 52 109",
|
||
|
"78 74 78",
|
||
|
"133 76 48",
|
||
|
"52 101 36",
|
||
|
"208 70 72",
|
||
|
"117 113 97",
|
||
|
"89 125 206",
|
||
|
"210 125 44",
|
||
|
"133 149 161",
|
||
|
"109 170 44",
|
||
|
"210 170 153",
|
||
|
"109 194 202",
|
||
|
"218 212 94",
|
||
|
"222 238 214",
|
||
|
"0 71 189",
|
||
|
"2 136 217",
|
||
|
"7 185 252",
|
||
|
"0 149 67",
|
||
|
"0 171 56",
|
||
|
"154 240 0",
|
||
|
"255 179 0",
|
||
|
"255 206 0",
|
||
|
"255 239 59",
|
||
|
"255 206 0",
|
||
|
"255 230 59",
|
||
|
"234 0 52",
|
||
|
"253 71 3",
|
||
|
"255 139 42",
|
||
|
"130 0 172",
|
||
|
"182 16 191",
|
||
|
"204 114 245"
|
||
|
];
|
||
|
|
||
|
let holder = document.querySelector( '#paletteHolder' );
|
||
|
for ( let i = 0 ; i < paletteItBe.length; i++ ) {
|
||
|
let paletteStr = paletteItBe[ i ];
|
||
|
let colorValues = paletteStr.split( ' ' );
|
||
|
|
||
|
let element = document.createElement( 'div' );
|
||
|
element.className = 'paletteBox column';
|
||
|
element.dataset.colorR = colorValues[ 0 ];
|
||
|
element.dataset.colorG = colorValues[ 1 ];
|
||
|
element.dataset.colorB = colorValues[ 2 ];
|
||
|
|
||
|
element.style.backgroundColor = `rgba(${colorValues[ 0 ]}, ${colorValues[ 1 ]}, ${colorValues[ 2 ]}, 1)`;
|
||
|
|
||
|
holder.appendChild( element );
|
||
|
|
||
|
element.addEventListener( 'click', this.handlePalette );
|
||
|
}
|
||
|
|
||
|
let colorValues = paletteItBe[ paletteItBe.length - 1 ].split( ' ' );
|
||
|
this.selectedR = colorValues[ 0 ];
|
||
|
this.selectedG = colorValues[ 1 ];
|
||
|
this.selectedB = colorValues[ 2 ];
|
||
|
}
|
||
|
|
||
|
handlePalette( ev ) {
|
||
|
|
||
|
if ( ev.preventDefault ) {
|
||
|
ev.preventDefault();
|
||
|
}
|
||
|
|
||
|
app.selectedR = this.dataset.colorR;
|
||
|
app.selectedG = this.dataset.colorG;
|
||
|
app.selectedB = this.dataset.colorB;
|
||
|
|
||
|
}
|
||
|
|
||
|
handleCanvas( ev ) {
|
||
|
|
||
|
if ( ev.preventDefault ) {
|
||
|
ev.preventDefault();
|
||
|
}
|
||
|
|
||
|
let rect = app.canvas.getBoundingClientRect(),
|
||
|
scale = app.canvas.width / rect.width;
|
||
|
|
||
|
let mouseX = Math.floor( ( ev.clientX - rect.left ) * scale ),
|
||
|
mouseY = Math.floor( ( ev.clientY - rect.top ) * scale );
|
||
|
|
||
|
app.context.fillStyle = `rgba(${app.selectedR}, ${app.selectedG}, ${app.selectedB}, 1)`;
|
||
|
app.context.fillRect( mouseX, mouseY, 1, 1 );
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
let app = new App();
|