-
Notifications
You must be signed in to change notification settings - Fork 2
/
tagging.js
77 lines (65 loc) · 1.8 KB
/
tagging.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
var canvas;
var ctx;
var img;
var canvasOffset;
var offsetX;
var offsetY;
var isDrawing = false;
canvas = document.getElementById("canvas");
img = document.getElementById("img");
ctx = canvas.getContext("2d");
canvasOffset = $("#canvas").offset();
offsetX = canvasOffset.left;
offsetY = canvasOffset.top;
$("#canvas").on('mousedown', function (e) {
handleMouseDown(e);
}).on('mouseup', function(e) {
handleMouseUp();
}).on('mousemove', function(e) {
handleMouseMove(e);
});
var startX;
var startY;
var sizeX;
var sizeY;
var hasDrawn = false;
function handleMouseUp() {
isDrawing = false;
canvas.style.cursor = "default";
document.getElementById("x").value = startX;
document.getElementById("y").value = startY;
document.getElementById("width").value = sizeX;
document.getElementById("height").value = sizeY;
}
function handleMouseMove(e) {
if (isDrawing) {
var mouseX = parseInt(e.pageX - offsetX);
var mouseY = parseInt(e.pageY - offsetY);
sizeY = sizeX = Math.max(mouseX - startX, mouseY - startY);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.rect(startX, startY, sizeX, sizeY);
ctx.stroke();
}
}
function handleMouseDown(e) {
ctx = canvas.getContext("2d");
canvasOffset = $("#canvas").offset();
offsetX = canvasOffset.left;
offsetY = canvasOffset.top;
canvas.style.cursor = "crosshair";
isDrawing = true
startX = parseInt(e.pageX - offsetX);
startY = parseInt(e.pageY - offsetY);
hasDrawn = true;
}
function callLoad() {
canvas = document.getElementById("canvas");
img = document.getElementById("img");
ctx = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
document.getElementById('container').width = img.width;
document.getElementById('container').height = img.height;
ctx.strokeStyle = "#FF0000";
}