JSFiddle - React, Tailwind, and code Playground

by Bartek Matuszewski

HTML

<!DOCTYPE html>
<body>
    <div id='leftPanel'>
        <div class="panelTitle">
             <h3>MENU</h3>

        </div>
        <div class="panelContent">
                <label for="freeDraw">draw</label>
                <input type="radio" name="toolSelect" id="freeDraw"></input>
                <label for="drawRectangle">draw rectangle</label>
                <input type="radio" name="toolSelect" id="drawRectangle"></input>
            
            <div type="button" class="panelTitle" id="clrCanvas">clear canvas!<i class="fa fa-trash"></i>
            </div>
        </div>
    </div>
    <canvas width="600" height="400"></canvas>
    <div class="controls">
        <ul>
            <li class="red selected"></li>
            <li class="blue"></li>
            <li class="yellow"></li>
        </ul>
        <button id="revealColorSelect">New Color</button>
        <div id="colorSelect">	<span id="newColor"></span>

            <div class="sliders">
                <p>
                    <label for="red">Red</label>
                    <input id="red" name="red" type="range" min=0 max=255 value=0>
                </p>
                <p>
                    <label for="green">Green</label>
                    <input id="green" name="green" type="range" min=0 max=255 value=0>
                </p>
                <p>
                    <label for="blue">Blue</label>
                    <input id="blue" name="blue" type="range" min=0 max=255 value=0>
                </p>
            </div>
            <div>
                <button id="addNewColor">Add Color</button>
            </div>
        </div>
    </div>
</body>

CSS

body {
    background: #384047;
    font-family: sans-serif;
}
canvas {
    background: #fff;
    display: block;
    margin: 50px auto 10px;
    border-radius: 5px;
    box-shadow: 0 4px 0 0 #222;
    cursor: url(../img/cursor.png), crosshair;
}
.controls {
    min-height: 60px;
    margin: 0 auto;
    width: 600px;
    border-radius: 5px;
    overflow: hidden;
}
ul {
    list-style:none;
    margin: 0;
    float: left;
    padding: 10px 0 20px;
    width: 100%;
    text-align: center;
}
ul li, #newColor {
    display:block;
    height: 54px;
    width: 54px;
    border-radius: 60px;
    cursor: pointer;
    border: 0;
    box-shadow: 0 3px 0 0 #222;
}
ul li {
    display: inline-block;
    margin: 0 5px 10px;
}
.red {
    background: #fc4c4f;
}
.blue {
    background: #4fa3fc;
}
.yellow {
    background: #ECD13F;
}
.selected {
    border: 7px solid #fff;
    width: 40px;
    height: 40px;
}
button {
    background: #68B25B;
    box-shadow: 0 3px 0 0 #6A845F;
    color: #fff;
    outline: none;
    cursor: pointer;
    text-shadow: 0 1px #6A845F;
    display: block;
    font-size: 16px;
    line-height: 40px;
}
#revealColorSelect {
    border: none;
    border-radius: 5px;
    margin: 10px auto;
    padding: 5px 20px;
    width: 160px;
}
/* New Color Palette */
 #colorSelect {
    background: #fff;
    border-radius: 5px;
    clear: both;
    margin: 20px auto 0;
    padding: 10px;
    width: 305px;
    position: relative;
    display:none;
}
#colorSelect:after {
    bottom: 100%;
    left: 50%;
    border: solid transparent;
    content:" ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
    border-color: rgba(255, 255, 255, 0);
    border-bottom-color: #fff;
    border-width: 10px;
    margin-left: -10px;
}
#newColor {
    width: 80px;
    height: 80px;
    border-radius: 3px;
    box-shadow: none;
    float: left;
    border: none;
    margin: 10px 20px 20px 10px;
}
.sliders p {
    margin: 8px 0;
    vertical-align:...

JavaScript

//Problem: No user interaction causes no change to application
//Solution: When user interacts cause changes appropriately
var color = $('.selected').css('background-color');
var $canvas = $('canvas');
var context = $canvas[0].getContext('2d');
var mousedown = false;

//Update the new color span
function updateNewColor() {
    $('#newColor').css('background', 'black');
    var red = $('#red').val();
    var green = $('#green').val();
    var blue = $('#blue').val();
    var background = 'rgb( ' + red + ', ' + green + ', ' + blue + ' )';
    console.log(background);
    $('#newColor').css('background', background);

}

//When clicking on control list items
$('.controls').on('click', 'li', function () {


    //Deselect siblings element
    $(this).siblings().removeClass('selected');
    //Select clicked element
    $(this).addClass('selected');
    //cache current element
    color = $(this).css('background-color');
    console.log(color);
});

//When new color is pressed
$('#revealColorSelect').click(function () {
    updateNewColor();
    //Show color select or hide color select
    $('#colorSelect').toggle();
});


//When color sliders change
$('#colorSelect input').change(updateNewColor);


//When add color is pressed
$('#addNewColor').click(function () {
    var newColorLi = $('<li></li>');
    var colorToPass = $('#newColor').css('background-color');
    console.log(colorToPass);
    newColorLi.css('background-color', colorToPass);
    //Append the color to the controls ul
    $('.controls ul').append(newColorLi);
    //Select the new color
    newColorLi.click();
});

//add class active to currently selected button
$("#leftPanel").on('click', 'button', function () {
    $(this).siblings().removeClass('active');
    $(this).addClass('active');
});

//On mouse events on the canvas



$(document).on('click', '#pencil', function () {
    if ($('#pencil').hasClass('active')) {
        $canvas.mousedown(function (e) {
            lastEvent = e;
           ...