-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathcircle.js
98 lines (86 loc) · 2.15 KB
/
circle.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
import PDFJSAnnotate from '../PDFJSAnnotate';
import { appendChild } from '../render/appendChild';
import {
findSVGAtPoint,
getMetadata,
convertToSvgPoint
} from './utils';
let _enabled = false;
let _type;
let _circleRadius = 10;
let _circleColor = '0000FF';
/**
* Set the attributes of the pen.
*
* @param {Number} circleRadius The radius of the circle
* @param {String} circleColor The color of the circle
*/
export function setCircle(circleRadius = 10, circleColor = '0000FF') {
_circleRadius = parseInt(circleRadius, 10);
_circleColor = circleColor;
}
/**
* Handle document.mouseup event
*
* @param {Event} e The DOM event to handle
*/
function handleDocumentMouseup(e) {
let svg = findSVGAtPoint(e.clientX, e.clientY);
if (!svg) {
return;
}
let rect = svg.getBoundingClientRect();
saveCircle(svg, _type, {
x: e.clientX - rect.left,
y: e.clientY - rect.top
}, _circleRadius, _circleColor);
}
/**
* Save a circle annotation
*
* @param {SVGElement} svg
* @param {String} type The type of circle (circle, emptycircle, fillcircle)
* @param {Object} pt The point to use for annotation
* @param {float} radius
* @param {String} color The color of the rects
*/
function saveCircle(svg, type, pt, radius, color) {
// Initialize the annotation
let svg_pt = convertToSvgPoint([ pt.x, pt.y ], svg);
let annotation = {
type,
color,
cx: svg_pt[0],
cy: svg_pt[1],
r: radius
};
let { documentId, pageNumber } = getMetadata(svg);
// Add the annotation
PDFJSAnnotate.getStoreAdapter().addAnnotation(documentId, pageNumber, annotation)
.then((annotation) => {
appendChild(svg, annotation);
});
}
/**
* Enable circle behavior
*/
export function enableCircle(type) {
_type = type;
if (_enabled) { return; }
_enabled = true;
document.addEventListener('mouseup', handleDocumentMouseup);
}
/**
* Disable circle behavior
*/
export function disableCircle() {
if (!_enabled) { return; }
_enabled = false;
document.removeEventListener('mouseup', handleDocumentMouseup);
}
export function addCircle(type, e) {
let oldType = _type;
_type = type;
handleDocumentMouseup(e);
_type = oldType;
}