2014-02-01 18:18:29 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
2014-02-02 20:16:28 +00:00
|
|
|
window.c = $('canvas')[0].getContext('2d');
|
|
|
|
window.o = $('canvas')[1].getContext('2d');
|
2014-02-01 18:18:29 +00:00
|
|
|
|
|
|
|
window.settings = {
|
2014-02-05 13:02:07 +00:00
|
|
|
lineWidth : 2,
|
|
|
|
color : 'black',
|
2014-02-01 18:18:29 +00:00
|
|
|
type: 'sketch',
|
|
|
|
lineCap: 'round',
|
2014-02-02 20:16:28 +00:00
|
|
|
lineJoin: 'round',
|
2014-02-05 13:02:07 +00:00
|
|
|
furLength: 5,
|
2014-02-06 16:18:11 +00:00
|
|
|
connectTelorance: 40,
|
|
|
|
composite: 'source-over'
|
2014-02-01 18:18:29 +00:00
|
|
|
};
|
|
|
|
window.points = [];
|
|
|
|
window.$c = $('canvas');
|
2014-02-02 20:16:28 +00:00
|
|
|
window.points.history = [{ data: c.createImageData($c.width(), $c.height()), points: []}];
|
2014-02-06 20:39:42 +00:00
|
|
|
window.points.history.last = 0;
|
2014-02-01 18:18:29 +00:00
|
|
|
|
|
|
|
sizeAndPos();
|
2014-02-06 14:40:10 +00:00
|
|
|
//$(window).resize(sizeAndPos);
|
2014-02-01 18:18:29 +00:00
|
|
|
|
2014-02-05 13:02:07 +00:00
|
|
|
$('.color-picker').change(function() {
|
|
|
|
var c = $(this).find('.color').val();
|
|
|
|
settings.color = c;
|
|
|
|
$('#setcolor span').html(c);
|
|
|
|
})
|
|
|
|
$('.color').val('#000000');
|
|
|
|
|
|
|
|
yepnope({
|
|
|
|
test: window.mobile,
|
2014-02-07 12:20:19 +00:00
|
|
|
yep : ['js/libs/touch.js', 'js/mobile.js', 'js/libs/color-picker-touch.js'],
|
|
|
|
nope: ['js/desktop.js', 'js/libs/color-picker.js']
|
2014-02-05 13:02:07 +00:00
|
|
|
})
|
|
|
|
|
2014-02-09 15:38:14 +00:00
|
|
|
|
|
|
|
function save() {
|
|
|
|
switch(save.background) {
|
|
|
|
case 'white': {
|
|
|
|
c.fillStyle = 'white';
|
|
|
|
c.globalCompositeOperation = 'destination-over';
|
|
|
|
c.fillRect(0, 0, width(), height());
|
|
|
|
c.fillStyle = settings.color;
|
|
|
|
c.globalCompositeOperation = settings.composite;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'current color': {
|
|
|
|
c.fillStyle = settings.color;
|
|
|
|
c.globalCompositeOperation = 'destination-over';
|
|
|
|
c.fillRect(0, 0, width(), height());
|
|
|
|
c.fillStyle = settings.color;
|
|
|
|
c.globalCompositeOperation = settings.composite;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var data = $c[0].toDataURL();
|
|
|
|
|
|
|
|
if( save.type == 'sketchy project' ) {
|
|
|
|
if( localStorage.getItem(save['file name']) ) {
|
|
|
|
if( confirm('A sketch with this name already exists. Do you want to overwrite ' + save['file name']) + '?' ) {
|
|
|
|
localStorage.setItem(save['file name'], JSON.stringify({data: data, points: window.points}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
localStorage.setItem(save['file name'], JSON.stringify({data: data, points: window.points}));
|
|
|
|
} else {
|
|
|
|
window.open(data, '_blank').focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
c.putImageData(window.points.history[window.points.history.last].data, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function load() {
|
|
|
|
var file = JSON.parse(localStorage.getItem(load.file));
|
|
|
|
var img = document.createElement('img');
|
|
|
|
img.src = file.data;
|
|
|
|
img.onload = function() {
|
|
|
|
c.clearRect(0, 0, width(), height());
|
|
|
|
c.drawImage(img, 0, 0);
|
|
|
|
window.points = file.points;
|
|
|
|
window.points.history = [{ data: c.createImageData($c.width(), $c.height()), points: []}, { data: c.getImageData(0, 0, width(), height()), points: file.points}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.load = load;
|
|
|
|
window.save = save;
|
|
|
|
|
|
|
|
// Check for Update
|
|
|
|
|
2014-02-09 16:02:07 +00:00
|
|
|
var request = navigator.mozApps.checkInstalled('mdibaiee.github.io/Sketchy/Web/manifest-web.webapp');
|
2014-02-09 15:38:14 +00:00
|
|
|
request.onsuccess = function() {
|
2014-02-09 16:02:07 +00:00
|
|
|
if( !this.result && confirm('A new version is available, do you want to update?') ) {
|
|
|
|
navigator.mozApps.install('mdibaiee.github.io/Sketchy/Web/manifest-web.webapp');
|
|
|
|
}
|
2014-02-09 15:38:14 +00:00
|
|
|
}
|
2014-02-09 16:02:07 +00:00
|
|
|
request.onerror = function() {
|
|
|
|
alert('An error occured while trying to check for updates');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-09 15:38:14 +00:00
|
|
|
|
2014-02-01 18:18:29 +00:00
|
|
|
})
|