-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
65 lines (51 loc) · 1.62 KB
/
main.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
var CELL_NUMBER = 10;
var CELL_WIDTH = 10;
var MARGIN = 40;
var paper = Raphael(document.getElementsByClassName('grid')[0],
CELL_WIDTH * CELL_NUMBER + MARGIN * (CELL_NUMBER - 1) - CELL_WIDTH / 2,
CELL_WIDTH * CELL_NUMBER + MARGIN * (CELL_NUMBER - 1) - CELL_WIDTH / 2);
function getCoords(e) {
var $target = $(e.target);
return {
x: ($target.data('x') - 1) * (CELL_WIDTH + MARGIN),
y: ($target.data('y') - 1) * (CELL_WIDTH + MARGIN)
};
}
jQuery(function($) {
var pathPart = '';
var isEditing = false;
var currentPath;
var coords;
var offset = $('svg').offset();
var svgX = offset.left;
var svgY = offset.top;
$('.grid').click('.cell', function(e) {
coords = getCoords(e);
if (!isEditing) {
isEditing = true;
pathPart += 'M' + coords.x + ',' + coords.y;
} else {
pathPart += 'L' + coords.x + ',' + coords.y;
paper.path(pathPart);
pathPart = '';
isEditing = false;
if (currentPath) {
$(currentPath.node).remove();
}
}
$(e.target).addClass('selected');
}).mousemove(function(e) {
if (!isEditing) {
return false;
}
if (currentPath) {
$(currentPath.node).remove();
}
coords = getCoords(e);
currentPath = paper.path(pathPart + 'L' + Math.round(e.pageX - svgX) + ',' + Math.round(e.pageY - svgY));
currentPath.attr('stroke', '#5C5C5C');
});
$('form').submit(function() {
$('[name="svg"]').val($(paper.canvas).html());
});
});