LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code
 
    
Pixel Maker Logo

CREATE GRID CANVAS

GRID HEIGHT:


GRID WIDTH:


SET BACKGROUND COLOR:

PAINT CONTROLS

BRUSH SIZE:

COLOR PICKER:

CONTROLS

PAINT :
LEFT/RIGHT-CLICK

ERASE :
SHIFT + LEFT/RIGHT-CLICK

EYEDROPPER :
CTRL/CMD + LEFT/RIGHT-CLICK


CLICK AND DRAG TO PAINT

CSS
@font-face {
    font-family: FFMarkRegular;
    src: url('../fonts/FF-Mark-Pro-Book.otf')
}

* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: FFMarkRegular, sans-serif;
    font-size: 12px;
}

body {
    background-color: #1b1014;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.main-header {
    background-color: #131317;
    border-bottom: 1px solid #000;
}

.main-header img {
    width: 600px;
    padding: 15px;
}

.content-wrapper {
    display: flex;
    min-height: calc(100vh - 74.7167px);
}

.controls-wrapper {
    padding:15px;
    min-height: 100%;
    width: 350px;
    background-color: #131317;
    border-right: 1px solid #000;
}

.grid-controls,
.paint-controls {   
    padding-top: 10px;
}

h2 {
    font-size: 16px;
    font-weight: normal;
    color: #ec1e59;
    margin-bottom: 10px;
}

p{
    color: #ec1e59;
    margin-bottom: 5px;
}

span {
    color: #795e66;
}

input[type=text],
input[type=number],
input[type=submit],
button,
select {
    text-align: center;
    padding: 5px;
    border: 0;
    border-radius: 0;
    background-color: #2b101a;
    color: #ec1e59;
    margin-bottom: 10px;
}

input[type=text],
input[type=number],
input[type=submit],
select {
    width: 100%;
}

#backgroundPicker {
    margin-bottom: 15px;
}

input[type=submit],
button {
    border: 1px solid #ec1e59;

    -webkit-transition: .2s ease-in-out;
    -moz-transition: .2s ease-in-out;
    -o-transition: .2s ease-in-out;
    transition: .2s ease-in-out;
}

input[type=submit]:hover,
button:hover {
    cursor: pointer;
    background-color: #ec1e59;
    color: #2b101a;
}

.paint-controls,
.controls-list {
    margin-top:20px;
}

.grid-buttons {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
}

.grid-buttons button {
    flex-grow: 1;
    width: 50%;
    margin-right: 5px;
}

.grid-buttons button:nth-child(2n) {
    margin-left: 5px;
    margin-right: 0;
}

input[type=color] {
    width: 100%;
    height: 80px;
    padding: 0;
    border: 0;
    margin-bottom: 10px;
}

input[type=color]:hover {
    cursor: pointer;
}

.canvas-wrapper {
    min-height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    padding-top:35px;
}

table,
tr,
td {
    border: 1px solid rgba(236, 30, 89, 0.2);
}

table {
    border-collapse: collapse;
    margin: 0 auto;
}

/* table:hover {
    cursor: defaul;
} */

tr {
    height: 20px;
}

td {
    width: 20px;
}

.canvasCol {
    background-color: #1b1014;
}

.toggleGrid {
    border: none;
}

.eyedropper {
    cursor: crosshair;
}
JS
// document is ready
$(function () {

    // changes the color inputs default value
    $('#colorPicker').val('#00ff9d');
    $('#backgroundPicker').val('#1b1014');
    let currentBackground;

    // creates a grid using sizes inputted by the user.
    function makeGrid() {
        const pixelCanvas = $('#pixel_canvas');
        const gridHeight = $('#input_height').val();
        const gridWidth = $('#input_width').val();
        // clears any existing pixel canvas
        pixelCanvas.empty();
        // creates the table grid
        for (let r = 0; r < gridHeight; r++) {
        let row = $('').appendTo(pixelCanvas);
            for (let c = 0; c < gridWidth; c++) {
                $('').appendTo(row);
            }
        }

        // sets the background color of the canvas
        $('.canvasCol').css('background-color', $('#backgroundPicker').val());
        currentBackground = $('#backgroundPicker').val();
    }

    function paintCanvas() {
        // assumes mouse isnt clicked
        let clicked = false;
        let shiftClicked = false;
        let currentColor = $('#colorPicker').val();

        // checks if the ctrl is held and changes the cursor style
        $(document).keydown(function (event) {
            if (event.which === 17) {
                $('table').css('cursor', 'crosshair');
            }
        }).keyup( function (event) {
            if (event.which === 17) {
                $('table').css('cursor', 'default');
            }
        });

        // checks if mouse is clicked
        $('.canvasCol').mousedown(function (event) {
            // prevents the user from dragging the table
            event.preventDefault();
            clicked = true;

            const brushSize = $('#brush_Size').val();
            const $this = $(this);
            const cellIndex = $this.index();
            
            // object containing cell locations
            const currentCells = [
                // brush 1
                { cell : $this }, // origin point
                // brush 2
                { cell : $this.closest('td').next('td') }, // right
                { cell : $this.closest('tr').next().children().eq(cellIndex) }, // bottom
                { cell : $this.closest('tr').next().children().eq(cellIndex).closest('td').next('td') }, // bottom right
                // // brush 3
                { cell : $this.closest('td').prev('td') }, // left
                { cell : $this.closest('tr').next().children().eq(cellIndex).closest('td').prev('td') }, // bottom left
                { cell : $this.closest('tr').prev().children().eq(cellIndex) }, // top
                { cell : $this.closest('tr').prev().children().eq(cellIndex).closest('td').next('td') }, // top right
                { cell : $this.closest('tr').prev().children().eq(cellIndex).closest('td').prev('td') } // top left
            ];

            // checks if shift is held while clicking
            if (event.shiftKey) {
                clicked = false;
                shiftClicked = true;
            }

            // eyedropper tool
            if (event.ctrlKey || event.metaKey) {
                clicked = false;
                let hexColor = '';
                // retrieves rgb color value of cell background
                const rgbColor = $(this).css('background-color');
                hexify(rgbColor);

                // converts the retrieved rgb color value to a hexidecimal value
                function hexify(color) {
                    const parts = color.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
                    delete(parts[0]);
                    for (let i = 1; i <= 3; ++i) {
                        parts[i] = parseInt(parts[i]).toString(16);
                        if (parts[i].length == 1) parts[i] = '0' + parts[i];
                    }
                    hexColor = '#' + parts.join('');
                    return hexColor;
                }
                // sets color input value to returned hexidecimal color value
                $('#colorPicker').val(hexColor);
            }

            // paints on click, scales according to selected brush size
            if (clicked) {
                for (let i = 0; i < Math.pow(brushSize, 2); i++) {
                  currentColor = $('#colorPicker').val();
                  currentCells[i].cell.css('background-color', currentColor);
                }
            // if shift is held, will erase instead, also scales with brush size
            } else if (shiftClicked) {
                for (let i = 0; i < Math.pow(brushSize, 2); i++) {
                    currentCells[i].cell.css('background-color', currentBackground);
                }
            }
            
        }).mouseup(function () {
            clicked = false;
            shiftClicked = false;

        }).mousemove(function () {
            const brushSize = $('#brush_Size').val();
            const $this = $(this);
            const cellIndex = $this.index();
            
            // object containing cell locations
            const currentCells = [
                // brush 1
                { cell : $this }, // origin point
                // brush 2
                { cell : $this.closest('td').next('td') }, // right
                { cell : $this.closest('tr').next().children().eq(cellIndex) }, // bottom
                { cell : $this.closest('tr').next().children().eq(cellIndex).closest('td').next('td') }, // bottom right
                // // brush 3
                { cell : $this.closest('td').prev('td') }, // left
                { cell : $this.closest('tr').next().children().eq(cellIndex).closest('td').prev('td') }, // bottom left
                { cell : $this.closest('tr').prev().children().eq(cellIndex) }, // top
                { cell : $this.closest('tr').prev().children().eq(cellIndex).closest('td').next('td') }, // top right
                { cell : $this.closest('tr').prev().children().eq(cellIndex).closest('td').prev('td') } // top left
            ];

            // prevents painting if mouse is released off canvas
            $('body').mouseup(function (){
                clicked = false;
            })

            // paints on click, scales according to selected brush size
            if (clicked) {
                for (let i = 0; i < Math.pow(brushSize, 2); i++) {
                  currentColor = $('#colorPicker').val();
                  currentCells[i].cell.css('background-color', currentColor);
                }
            // if shift is held, will erase instead. also scales with brush size
            } else if (shiftClicked) {
                for (let i = 0; i < Math.pow(brushSize, 2); i++) {
                    currentCells[i].cell.css('background-color', currentBackground);
                }
            }
        });
    }
    
    // resets the canvas
    $('#reset').click(function () {
        $('.canvasCol').css('background-color', currentBackground);
    });

    // toggles grid
    $('#toggleGrid').click(function () {
        $('tr, td').toggleClass('toggleGrid');
    });

    // calls the makeGrid function on form submission
    // prevents the page from refreshing when submitted
    $('#submitGrid').click(function (event) {
        event.preventDefault();
        makeGrid();
        paintCanvas();
    });

});
Wed, 12/27/2017 - 07:02

Related Codes

Pen ID
Pen ID
Pen ID
Square Adv