-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.mustache_maker.js
108 lines (96 loc) · 2.95 KB
/
jquery.mustache_maker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
(function($){
$.fn.mustache_maker = function(options){
var opts = $.extend({}, $.fn.mustache_maker.defaults, options);
var attach_canvas = function($img){
$img.load(function(){
var clickX = new Array();
var clickY = new Array();
var clickDrag = new Array();
var paint;
var height = $img.height();
var width = $img.width();
var canvas = $("<canvas/>");
canvas.attr("height", height);
canvas.attr("width", width);
var context = canvas[0].getContext("2d");
function addClick(x, y, dragging)
{
clickX.push(x);
clickY.push(y);
clickDrag.push(dragging);
}
canvas.mousedown(function(e){
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
paint = true;
addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
$.fn.mustache_maker.draw({
x : clickX,
y : clickY,
drag : clickDrag
}, context, canvas);
});
canvas.mousemove(function(e){
if(paint){
addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
$.fn.mustache_maker.draw({
x : clickX,
y : clickY,
drag : clickDrag
}, context, canvas);
}
});
canvas.mouseup(function(e){
paint = false;
opts.mustache_callback({
x : clickX,
y : clickY,
drag : clickDrag
});
});
canvas.mouseleave(function(e){
paint = false;
opts.mustache_callback({
x : clickX,
y : clickY,
drag : clickDrag
});
});
//Store the original image on the canvas and replace it with the canvas version.
$img.parent().append(canvas);
canvas.position($img.position());
canvas.data('original', $img);
$img.detach();
context.drawImage($img[0],0, 0, width, height);
});
}
return this.each(function(){
var img = $('img', this);
attach_canvas(img);
});
};
$.fn.mustache_maker.draw = function(mustache, context, canvas){
canvas.width = canvas.width; // Clears the canvas
context.strokeStyle = "#000000";
context.lineJoin = "round";
context.lineWidth = 5;
var clickX = mustache.x,
clickY = mustache.y,
clickDrag = mustache.drag;
for(var i=0; i < clickX.length; i++)
{
context.beginPath();
if(clickDrag[i] && i){
context.moveTo(clickX[i-1], clickY[i-1]);
}else{
context.moveTo(clickX[i]-1, clickY[i]);
}
context.lineTo(clickX[i], clickY[i]);
context.closePath();
context.stroke();
}
};
$.fn.mustache_maker.defaults = {
mustache_callback : function(mustache){}
};
})( jQuery );