Sketchy/js/mobile-events.js

197 lines
6.0 KiB
JavaScript
Raw Normal View History

2014-02-05 13:02:07 +00:00
"use strict";
function save() {
switch(save.background) {
case 'white': {
var cache = {
fillStyle: c.color,
composite: c.globalCompositeOperation
}
c.fillStyle = 'white';
c.globalCompositeOperation = 'destination-over';
2014-02-05 16:38:28 +00:00
c.fillRect(0, 0, width(), height());
2014-02-05 13:02:07 +00:00
c.fillStyle = cache.fillStyle;
c.globalCompositeOperation = cache.composite;
break;
}
case 'current color': {
var cache = {
fillStyle: c.color,
composite: c.globalCompositeOperation
}
c.fillStyle = settings.strokeStyle;
c.globalCompositeOperation = 'destination-over';
2014-02-05 16:38:28 +00:00
c.fillRect(0, 0, width(), height());
2014-02-05 13:02:07 +00:00
c.fillStyle = cache.fillStyle;
c.globalCompositeOperation = cache.composite;
break;
}
}
var data = $c[0].toDataURL();
var file = dataToBlob($c[0].toDataURL());
var pics = navigator.getDeviceStorage('pictures');
var r = pics.addNamed(file, save['file name'] + '.png');
r.onsuccess = function() {
2014-02-06 14:40:10 +00:00
alert('Your sketch was successfuly saved to ' + this.result);
2014-02-05 13:02:07 +00:00
}
r.onerror = function() {
alert('Something bad happened when we tried to save your file\n Possible problems: \n Duplicate name \n Permission problems')
console.warn(this.error);
}
2014-02-06 14:40:10 +00:00
c.putImageData(window.points.history[window.points.history.last].data, 0, 0);
2014-02-05 13:02:07 +00:00
}
$('.menu').on('tap', function() {
2014-02-04 15:02:41 +00:00
$('#menu').toggleClass('pulled');
})
2014-02-05 13:02:07 +00:00
$('.save').on('tap', function() {
$('#save').removeClass('hidden');
2014-02-04 15:02:41 +00:00
})
2014-02-05 13:02:07 +00:00
$c.last().on('touchstart', function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
var xy = relative(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
startPoint(xy.x, xy.y);
window.active = true;
2014-02-05 13:02:07 +00:00
}).on('touchmove', function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
if (!window.active || settings.type == 'line') return;
var xy = relative(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
drawPoint(xy.x, xy.y);
2014-02-05 13:02:07 +00:00
}).on('touchend', 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
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').on('tap', function(e) {
e.preventDefault();
$(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').on('tap', function(e) {
$(this).parent().find('li[aria-selected]').removeAttr('aria-selected');
$(this).attr('aria-selected', 'true');
})
$confirm.find('button').last().on('tap', 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();
2014-02-06 14:40:10 +00:00
if( key !== 'file name' ) value = value.toLowerCase();
2014-02-05 13:02:07 +00:00
window[v][key] = value;
}
})
$(this).parents('form').addClass('hidden');
window[v]();
})
$confirm.find('button').first().on('tap', 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
if( target == 'color' ) {
return $(this).on('tap', function() {
$('.picker').removeClass('hidden');
})
}
$(this).on('tap', 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('touchstart', 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('touchmove', function(e) {
2014-02-05 13:02:07 +00:00
if( $(this).attr('data-moving') ) {
var x = parseInt(e.changedTouches[0].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('touchend', function() {
$(this).removeAttr('data-moving');
})
2014-02-05 13:02:07 +00:00
// Color Picker
$('#closePicker').on('tap', function() {
$('.picker').addClass('hidden');
})
// Bottom
$('#clear').on('tap', function() {
2014-02-05 16:38:28 +00:00
c.clearRect(0, 0, width(), height());
2014-02-06 13:00:58 +00:00
var h = window.points.history;
2014-02-05 16:38:28 +00:00
window.points = [];
2014-02-06 13:00:58 +00:00
window.points.history = h;
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').on('tap', undo);
$('#redo').on('tap', redo);