//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 + ' )';
$('#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');
});
//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();
});
//On mouse events on the canvas
function pencil() {
$canvas.mousedown(function(e) {
lastEvent = e;
mouseDown = true;
console.log('function pencil mousedown event lastEvent value taken');
}).mousemove(function(e) {
//Draw lines
if (mouseDown) {
console.log('function pencil mouseMove event e value taken drawing line');
context.beginPath();
context.moveTo(lastEvent.offsetX, lastEvent.offsetY);
context.lineTo(e.offsetX, e.offsetY);
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.