2014-02-01 18:18:29 +00:00
|
|
|
"use strict";
|
|
|
|
/*** ESSENTIALS ***/
|
|
|
|
|
|
|
|
function sizeAndPos() {
|
|
|
|
|
2014-02-06 20:39:42 +00:00
|
|
|
var data = window.points.history[window.points.history.last].data || c.getImageData(0,0, $c.width(), $c.height());
|
2014-02-01 18:18:29 +00:00
|
|
|
var w = $(window).width(),
|
2014-02-06 20:39:42 +00:00
|
|
|
h = $(window).height() - 53;
|
2014-02-05 13:02:07 +00:00
|
|
|
$c.attr('width', w * window.devicePixelRatio);
|
2014-02-06 20:39:42 +00:00
|
|
|
$c.attr('height',h * window.devicePixelRatio);
|
2014-02-05 13:02:07 +00:00
|
|
|
$c.css({
|
|
|
|
'width' : w,
|
2014-02-06 20:39:42 +00:00
|
|
|
'height' : h
|
2014-02-05 13:02:07 +00:00
|
|
|
});
|
2014-02-10 12:18:36 +00:00
|
|
|
alert($(window).height());
|
2014-02-05 16:38:28 +00:00
|
|
|
c.clearRect(0,0, width(), height());
|
2014-02-01 18:18:29 +00:00
|
|
|
c.putImageData(data, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function relative(x,y) {
|
|
|
|
return {
|
2014-02-05 13:02:07 +00:00
|
|
|
x : x*window.devicePixelRatio,
|
2014-02-05 16:38:28 +00:00
|
|
|
y : (y - 53) * window.devicePixelRatio
|
2014-02-01 18:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06 20:39:42 +00:00
|
|
|
function draw(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();
|
2014-02-06 16:18:11 +00:00
|
|
|
if( settings.type == 'eraser' ) c.globalCompositeOperation = 'destination-out';
|
|
|
|
else c.globalCompositeOperation = opts.composite || settings.composite;
|
2014-02-01 18:18:29 +00:00
|
|
|
c.lineCap = opts.lineCap || settings.lineCap;
|
|
|
|
c.lineJoin = opts.lineJoin || settings.lineJoin;
|
2014-02-05 13:02:07 +00:00
|
|
|
c.strokeStyle = opts.color || settings.color;
|
|
|
|
c.fillStyle = opts.color || settings.color;
|
|
|
|
c.lineWidth = ( opts.lineWidth || settings.lineWidth ) / 10;
|
2014-02-01 18:18:29 +00:00
|
|
|
c.moveTo(x1, y1);
|
|
|
|
c.lineTo(x2, y2);
|
2014-02-05 13:02:07 +00:00
|
|
|
if( !opts.noStroke ) c.stroke();
|
|
|
|
if( opts.fill ) c.fill();
|
2014-02-01 18:18:29 +00:00
|
|
|
}
|
|
|
|
|
2014-02-02 20:16:28 +00:00
|
|
|
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 {
|
2014-02-06 13:00:58 +00:00
|
|
|
c.clearRect(0,0, width(), height());
|
2014-02-02 20:16:28 +00:00
|
|
|
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-05 16:38:28 +00:00
|
|
|
function width() {
|
2014-02-06 13:00:58 +00:00
|
|
|
return +$c.attr('width');
|
2014-02-05 16:38:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function height() {
|
2014-02-06 13:00:58 +00:00
|
|
|
return +$c.attr('height');
|
2014-02-05 16:38:28 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 13:02:07 +00:00
|
|
|
function dataToBlob(data) {
|
|
|
|
var binary = atob(data.split(',')[1]), array = [];
|
|
|
|
var type = data.split(',')[0].split(':')[1].split(';')[0];
|
|
|
|
for(var i = 0; i < binary.length; i++) array.push(binary.charCodeAt(i));
|
|
|
|
return new Blob([new Uint8Array(array)], {type: type});
|
|
|
|
}
|
|
|
|
|
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' ) {
|
2014-02-05 13:02:07 +00:00
|
|
|
window.o.beginPath();
|
|
|
|
window.o.fillStyle = 'red';
|
|
|
|
window.o.arc(x,y, 3, 0, 2*Math.PI);
|
|
|
|
window.o.fill();
|
2014-02-02 20:16:28 +00:00
|
|
|
}
|
2014-02-01 18:18:29 +00:00
|
|
|
|
2014-02-06 14:40:10 +00:00
|
|
|
if( old.type == 'line' && current.type == 'line' ) {
|
2014-02-06 20:39:42 +00:00
|
|
|
if( points[points.indexOf(old)-1].type !== 'line' ) {
|
|
|
|
o.clearRect(old.x-3, old.y-3, 6, 6, true);
|
|
|
|
draw(old.x, old.y, x, y);
|
|
|
|
} else
|
|
|
|
draw(old.x, old.y, x, y);
|
2014-02-01 18:18:29 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 20:39:42 +00:00
|
|
|
var thresholds = window.mobile ? [10, 5] : [5, 2];
|
|
|
|
if( points.length > 1 && ((start && threshold(start.x, start.y, x, y, thresholds[0])) || threshold(old.x, old.y, x, y, thresholds[1])) ) {
|
2014-02-01 18:18:29 +00:00
|
|
|
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-06 16:18:11 +00:00
|
|
|
case 'eraser': {
|
|
|
|
capture.type = 'pen';
|
|
|
|
}
|
2014-02-04 15:02:41 +00:00
|
|
|
case 'pen': {
|
2014-02-06 20:39:42 +00:00
|
|
|
draw(capture.x, capture.y, x, y);
|
2014-02-01 18:18:29 +00:00
|
|
|
|
|
|
|
var current = {
|
|
|
|
x : x,
|
|
|
|
y : y,
|
|
|
|
start : capture.start,
|
|
|
|
type : capture.type
|
|
|
|
}
|
|
|
|
|
|
|
|
points.push(current);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'sketch': {
|
2014-02-06 20:39:42 +00:00
|
|
|
draw(capture.x, capture.y, x, y);
|
2014-02-01 18:18:29 +00:00
|
|
|
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-05 13:02:07 +00:00
|
|
|
if(threshold(points[i].x, points[i].y, current.x, current.y, settings.connectTelorance)) {
|
2014-02-01 18:18:29 +00:00
|
|
|
var x = points[i].x - current.x,
|
|
|
|
y = points[i].y - current.y;
|
2014-02-07 13:55:38 +00:00
|
|
|
var w = settings.lineWidth/20 > 0.2 ? settings.lineWidth/20 : 0.2;
|
2014-02-01 18:18:29 +00:00
|
|
|
|
2014-02-07 13:55:38 +00:00
|
|
|
draw(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: w})
|
2014-02-01 18:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-02-02 20:16:28 +00:00
|
|
|
case 'fur': {
|
2014-02-06 20:39:42 +00:00
|
|
|
draw(capture.x, capture.y, x, y);
|
2014-02-02 20:16:28 +00:00
|
|
|
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-05 13:02:07 +00:00
|
|
|
if(threshold(points[i].x, points[i].y, current.x, current.y, settings.connectTelorance)) {
|
2014-02-02 20:16:28 +00:00
|
|
|
var x = points[i].x - current.x,
|
|
|
|
y = points[i].y - current.y;
|
2014-02-05 13:02:07 +00:00
|
|
|
var l = settings.furLength / 100 || 0.2;
|
2014-02-07 13:55:38 +00:00
|
|
|
var w = settings.lineWidth/20 > 0.2 ? settings.lineWidth/20 : 0.2;
|
|
|
|
|
|
|
|
draw(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: w})
|
2014-02-02 20:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-02-01 18:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|