Sketchy/Web/js/desktop.js

189 lines
5.3 KiB
JavaScript
Raw Normal View History

2014-02-05 13:02:07 +00:00
"use strict";
$(window).resize(sizeAndPos);
2014-02-07 12:20:19 +00:00
// Check for update
2014-02-05 13:02:07 +00:00
function save() {
var cData = c.getImageData(0, 0, width(), height());
2014-02-05 13:02:07 +00:00
switch(save.background) {
case 'white': {
c.fillStyle = 'white';
c.globalCompositeOperation = 'destination-over';
2014-02-05 16:38:28 +00:00
c.fillRect(0, 0, width(), height());
c.fillStyle = settings.color;
c.globalCompositeOperation = settings.composite;
2014-02-05 13:02:07 +00:00
break;
}
case 'current color': {
c.fillStyle = settings.color;
2014-02-05 13:02:07 +00:00
c.globalCompositeOperation = 'destination-over';
2014-02-05 16:38:28 +00:00
c.fillRect(0, 0, width(), height());
c.fillStyle = settings.color;
c.globalCompositeOperation = settings.composite;
2014-02-05 13:02:07 +00:00
break;
}
}
var data = $c[0].toDataURL();
window.open(data, '_blank').focus();
2014-02-05 13:02:07 +00:00
c.putImageData(cData, 0, 0);
2014-02-05 13:02:07 +00:00
}
2014-02-04 15:02:41 +00:00
$('.menu').click(function() {
$('#menu').toggleClass('pulled');
})
$('.save').click(function() {
2014-02-05 13:02:07 +00:00
$('#save').removeClass('hidden');
2014-02-04 15:02:41 +00:00
})
2014-02-05 13:02:07 +00:00
$c.last().on('mousedown', function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
var xy = relative(e.pageX, e.pageY);
startPoint(xy.x, xy.y);
window.active = true;
2014-02-05 13:02:07 +00:00
}).on('mousemove', function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
if (!window.active || settings.type == 'line') return;
var xy = relative(e.pageX, e.pageY);
drawPoint(xy.x, xy.y);
2014-02-05 13:02:07 +00:00
}).on('mouseup', function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
window.active = false;
2014-02-06 14:40:10 +00:00
if( settings.type == 'eraser' ) return;
2014-02-04 15:02:41 +00:00
if(window.points.history.last < window.points.history.length-1) {
window.points.history.splice(window.points.history.last+1);
}
window.points.history.push({
2014-02-05 16:38:28 +00:00
data: c.getImageData(0, 0, width(), height()),
2014-02-04 15:02:41 +00:00
points: window.points.slice(0)
})
window.points.history.last = window.points.history.length-1;
})
// Value Selector
2014-02-05 13:02:07 +00:00
// Single
var $single = $('form[data-type="value-selector"].single');
2014-02-04 15:02:41 +00:00
2014-02-05 13:02:07 +00:00
$single.find('li').click(function(e) {
$(this).parent().find('li[aria-selected]').removeAttr('aria-selected');
2014-02-04 15:02:41 +00:00
$(this).attr('aria-selected', 'true');
2014-02-05 13:02:07 +00:00
var key = $(this).parents('form').attr('id'),
value = $(this).find('label span').html().toLowerCase();
window.settings[key] = value;
2014-02-04 15:02:41 +00:00
2014-02-05 13:02:07 +00:00
$('button[id="set' + key + '"] span').html(value[0].toUpperCase() + value.substr(1));
$('#menu div.options > div').addClass('hidden');
$('#menu div.options > .general, #menu div.options > .'+value).removeClass('hidden');
2014-02-04 15:02:41 +00:00
$(this).parents('form').addClass('hidden');
})
2014-02-05 13:02:07 +00:00
$single.submit(function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
$(this).addClass('hidden');
})
2014-02-05 13:02:07 +00:00
// Confirm
var $confirm = $('form[data-type="value-selector"].confirm');
$confirm.find('li').click(function(e) {
$(this).parent().find('li[aria-selected]').removeAttr('aria-selected');
$(this).attr('aria-selected', 'true');
})
$confirm.find('button').last().click(function(e) {
e.preventDefault();
var v = $(this).parents('form').attr('id');
$(this).parents('form').find('h1').each(function(i) {
if( i > 0 ) {
var key = $(this).html().toLowerCase();
var value = $(this).parent().find('ol:nth-of-type('+i+') li[aria-selected] span').html().toLowerCase();
2014-02-05 13:02:07 +00:00
window[v][key] = value;
}
})
$(this).parents('form').addClass('hidden');
window[v]();
})
$confirm.find('button').first().click(function(e) {
e.preventDefault();
$(this).parents('form').addClass('hidden');
})
2014-02-04 15:02:41 +00:00
// Value Selector Callers
var $btn = $('button[id^="set"]');
$btn.each(function() {
var target = /set(.*)/.exec($(this).attr('id'))[1];
2014-02-05 13:02:07 +00:00
// Exception for Color
if( target == 'color' ) {
return $(this).click(function() {
$('.picker').removeClass('hidden');
})
}
$(this).click(function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
$('form[id="' + target + '"]').removeClass('hidden');
})
})
// Seekbar
2014-02-05 13:02:07 +00:00
var sliderLeft;
2014-02-04 15:02:41 +00:00
$('div[role="slider"] button').on('mousedown', function() {
$(this).attr('data-moving','true');
2014-02-05 13:02:07 +00:00
if( !sliderLeft ) sliderLeft = $('div[role="slider"] button').offset().left;
2014-02-04 15:02:41 +00:00
}).on('mousemove', function(e) {
if( $(this).attr('data-moving') ) {
2014-02-05 13:02:07 +00:00
var x = parseInt(e.pageX - sliderLeft - 15);
2014-02-06 16:18:11 +00:00
var $c = $('.'+$(this).parents('div[role="slider"]').attr('class'));
var progress = $c.find('progress');
2014-02-05 13:02:07 +00:00
var max = +progress.attr('max');
var min = +progress.attr('min');
if( x <= max && x >= min ) {
2014-02-06 16:18:11 +00:00
$c.find('button').css('left', x+'%');
progress.attr('value', x);
var key = $c.attr('class');
2014-02-05 13:02:07 +00:00
settings[key] = x;
$('#'+ key +' span').html(x);
2014-02-04 15:02:41 +00:00
}
}
}).on('mouseup mouseleave', function() {
$(this).removeAttr('data-moving');
})
2014-02-05 13:02:07 +00:00
// Color Picker
2014-02-07 12:20:19 +00:00
$('.close').click(function() {
$(this).parent().addClass('hidden');
2014-02-05 13:02:07 +00:00
})
// Bottom
$('#clear').click(function() {
2014-02-05 16:38:28 +00:00
c.clearRect(0, 0, width(), height());
window.points = [];
2014-02-05 13:02:07 +00:00
if(window.points.history.last < window.points.history.length-1) {
window.points.history.splice(window.points.history.last+1);
}
window.points.history.push({
2014-02-05 16:38:28 +00:00
data: c.getImageData(0, 0, width(), height()),
2014-02-05 13:02:07 +00:00
points: []
})
window.points.history.last = window.points.history.length-1;
})
$('#undo').click(undo);
$('#redo').click(redo);
2014-02-07 12:20:19 +00:00
$('#about').click(function() {
$('.about').removeClass('hidden');
})