Sketchy/js/functions.js

170 lines
4.4 KiB
JavaScript
Raw Normal View History

2014-02-01 18:18:29 +00:00
"use strict";
/*** ESSENTIALS ***/
function sizeAndPos() {
var data = c.getImageData(0,0, $c.width(), $c.height());
var w = $(window).width(),
h = $(window).height();
2014-02-02 20:16:28 +00:00
$c.attr('width', w);
2014-02-04 15:02:41 +00:00
$c.attr('height',h - 53);
2014-02-01 18:18:29 +00:00
c.clearRect(0,0, $c.width(), $c.height());
c.putImageData(data, 0, 0);
}
function relative(x,y) {
return {
x : x - $c.offset().left,
y : y - $c.offset().top
}
}
function threshold(x1, y1, x2, y2, threshold) {
var tr = threshold || 5;
if( x1 <= x2 + tr && x1 >= x2 - tr && y1 <= y2 + tr && y1 >= y2 - tr ) return true;
return false;
}
2014-02-02 20:16:28 +00:00
function line(x1, y1, x2, y2, opts, overlay) {
2014-02-01 18:18:29 +00:00
opts = opts || {};
2014-02-02 20:16:28 +00:00
var c = window.c;
if( overlay ) var c = window.o;
2014-02-01 18:18:29 +00:00
c.beginPath();
c.lineCap = opts.lineCap || settings.lineCap;
c.lineJoin = opts.lineJoin || settings.lineJoin;
c.strokeStyle = opts.strokeStyle || settings.strokeStyle;
c.lineWidth = opts.lineWidth || settings.lineWidth;
c.moveTo(x1, y1);
c.lineTo(x2, y2);
c.stroke();
}
2014-02-02 20:16:28 +00:00
function erase(x1, y1, x2, y2, overlay) {
var c = window.c;
if( overlay ) var c = window.o;
c.clearRect(x1, y1, x2 - x1, y2 - y1);
}
function undo() {
var history = window.points.history;
if( history.last > 1 ) {
var step = history[history.last-1];
c.putImageData(step.data, 0, 0);
window.points = step.points.slice(0);
window.points.history = history;
window.points.history.last = history.last-1;
} else {
c.clearRect(0,0, $c.width(), $c.height());
window.points = [];
window.points.history = history;
window.points.history.last = 0;
}
}
function redo() {
var history = window.points.history;
if( history.last < history.length-1 ) {
var step = history[history.last+1];
c.putImageData(step.data, 0, 0);
window.points = step.points.slice(0);
window.points.history = history;
window.points.history.last = history.last+1;
}
}
2014-02-01 18:18:29 +00:00
/*** END ***/
function startPoint(x, y) {
2014-02-02 20:16:28 +00:00
// If no previous point exists, make the first one.
if( !points.length ) points.push({x: x, y: y, type: '', start: {x: x, y: y}});
2014-02-01 18:18:29 +00:00
var old = points[points.length-1],
start = old.start,
current = {
x : x,
y : y,
start : old.start || {x: x, y: y},
type : settings.type
}
2014-02-02 20:16:28 +00:00
if( old.type !== 'line' && current.type == 'line' ) {
line(x,y,x,y, {lineWidth: 5, strokeStyle: 'red'}, true);
}
2014-02-01 18:18:29 +00:00
if( old.type == 'line' ) {
2014-02-02 20:16:28 +00:00
if( points[points.indexOf(old)-1].type !== 'line' ) erase(old.x-5, old.y-5, old.x+5, old.y+5, true);
2014-02-01 18:18:29 +00:00
line(old.x, old.y, x, y);
}
if( points.length > 1 && ((start && threshold(start.x, start.y, x, y)) || threshold(old.x, old.y, x, y, 2)) ) {
window.active = false;
points[points.length-1].type = '';
points[points.length-1].start = undefined;
return;
}
points.push(current);
}
function drawPoint(x,y) {
var capture = points[points.length-1];
switch(capture.type) {
2014-02-04 15:02:41 +00:00
case 'pen': {
2014-02-01 18:18:29 +00:00
line(capture.x, capture.y, x, y);
var current = {
x : x,
y : y,
start : capture.start,
type : capture.type
}
points.push(current);
break;
}
case 'sketch': {
line(capture.x, capture.y, x, y);
var current = {
x : x,
y : y,
start : capture.start,
type : capture.type
}
points.push(current);
for( var i = 0, len = points.length-1; i < len; i++ ) {
2014-02-02 20:16:28 +00:00
if(threshold(points[i].x, points[i].y, current.x, current.y, 40)) {
2014-02-01 18:18:29 +00:00
var x = points[i].x - current.x,
y = points[i].y - current.y;
line(points[i].x - x*0.2, points[i].y - y*0.2, current.x + x*0.2, current.y + y*0.2, {strokeStyle: 'rgba(0,0,0,0.4)', lineWidth: settings.lineWidth/2})
}
}
break;
}
2014-02-02 20:16:28 +00:00
case 'fur': {
line(capture.x, capture.y, x, y);
var current = {
x : x,
y : y,
start : capture.start,
type : capture.type
}
points.push(current);
for( var i = 0, len = points.length-1; i < len; i++ ) {
2014-02-04 15:02:41 +00:00
if(threshold(points[i].x, points[i].y, current.x, current.y, settings.lineWidth*20)) {
2014-02-02 20:16:28 +00:00
var x = points[i].x - current.x,
y = points[i].y - current.y;
var l = settings.furLength || 0.2;
line(points[i].x + x*l, points[i].y + y*l, current.x - x*l, current.y - y*l, {strokeStyle: 'rgba(0,0,0,0.4)', lineWidth: settings.lineWidth/2})
}
}
break;
}
2014-02-01 18:18:29 +00:00
}
}