Sketchy/js/main.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

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-02 10:07:46 +00:00
lineWidth : 0.2,
2014-02-01 18:18:29 +00:00
strokeStyle : 'black',
type: 'sketch',
lineCap: 'round',
2014-02-02 20:16:28 +00:00
lineJoin: 'round',
ribbonEnd: 50,
ribbonTurn : 0
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-01 18:18:29 +00:00
sizeAndPos();
$(window).resize(sizeAndPos);
2014-02-02 20:16:28 +00:00
$c.last().bind('mousedown touchstart', function(e) {
2014-02-02 09:58:27 +00:00
e.preventDefault();
2014-02-02 20:16:28 +00:00
if( e.changedTouches ) e = e.changedTouches[0];
2014-02-01 18:18:29 +00:00
var xy = relative(e.pageX, e.pageY);
startPoint(xy.x, xy.y);
window.active = true;
2014-02-02 20:16:28 +00:00
}).bind('mousemove touchmove', function(e) {
2014-02-02 09:58:27 +00:00
e.preventDefault();
2014-02-01 18:18:29 +00:00
if (!window.active || settings.type == 'line') return;
2014-02-02 20:16:28 +00:00
if( e.changedTouches ) e = e.changedTouches[0];
2014-02-01 18:18:29 +00:00
var xy = relative(e.pageX, e.pageY);
drawPoint(xy.x, xy.y);
2014-02-02 09:58:27 +00:00
}).bind('mouseup touchend', function(e) {
e.preventDefault();
2014-02-01 18:18:29 +00:00
window.active = false;
2014-02-02 20:16:28 +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({
data: c.getImageData(0, 0, $c.width(), $c.height()),
points: window.points.slice(0)
})
window.points.history.last = window.points.history.length-1;
2014-02-01 18:18:29 +00:00
})
})