Skip to content

Commit 65c25bb

Browse files
committed
Creation of circle annotations at center of rectangle.
No longer offsetting cx, cy +15 each. Arrows now always draw from center of circle.
1 parent a238bc1 commit 65c25bb

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

src/UI/arrow.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,58 @@ import {
44
disableUserSelect,
55
enableUserSelect,
66
findSVGAtPoint,
7+
findSVGContainer,
78
getMetadata,
8-
convertToSvgPoint
9+
convertToSvgPoint,
10+
convertToScreenPoint,
11+
findAnnotationAtPoint
912
} from './utils';
1013

1114
let _enabled = false;
1215
let _penSize;
1316
let _penColor;
1417
let path;
1518
let lines;
19+
let originY;
20+
let originX;
1621

1722
/**
1823
* Handle document.mousedown event
1924
*/
20-
function handleDocumentMousedown() {
21-
path = null;
22-
lines = [];
25+
function handleDocumentMousedown(e) {
26+
let target = findAnnotationAtPoint(e.clientX, e.clientY);
27+
if (target === null)
28+
return;
29+
30+
let type = target.getAttribute('data-pdf-annotate-type');
31+
if (type !== 'circle' && type !== 'fillcircle' && type !== 'emptycircle') {
32+
return;
33+
}
34+
35+
let svg = findSVGContainer(target);
36+
let { documentId } = getMetadata(svg);
37+
let annotationId = target.getAttribute('data-pdf-annotate-id');
38+
39+
let event = e;
40+
PDFJSAnnotate.getStoreAdapter().getAnnotation(documentId, annotationId).then((annotation) => {
41+
if (annotation) {
42+
path = null;
43+
lines = [];
2344

24-
document.addEventListener('mousemove', handleDocumentMousemove);
25-
document.addEventListener('mouseup', handleDocumentMouseup);
45+
let point = convertToScreenPoint([
46+
annotation.cx,
47+
annotation.cy
48+
], svg);
49+
50+
let rect = svg.getBoundingClientRect();
51+
52+
originX = point[0] + rect.left;
53+
originY = point[1] + rect.top;
54+
55+
document.addEventListener('mousemove', handleDocumentMousemove);
56+
document.addEventListener('mouseup', handleDocumentMouseup);
57+
}
58+
});
2659
}
2760

2861
/**
@@ -60,7 +93,10 @@ function handleDocumentMouseup(e) {
6093
* @param {Event} e The DOM event to be handled
6194
*/
6295
function handleDocumentMousemove(e) {
63-
savePoint(e.clientX, e.clientY);
96+
let x = lines.length === 0 ? originX : e.clientX;
97+
let y = lines.length === 0 ? originY : e.clientY;
98+
99+
savePoint(x, y);
64100
}
65101

66102
/**

src/UI/rect.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ function saveRect(type, rects, color) {
199199
if (type === 'circle'||type === 'emptycircle'||type === 'fillcircle') {
200200
let rect = annotation.rectangles[0];
201201
delete annotation.rectangles;
202-
annotation.cx = rect.x;
203-
annotation.cy = rect.y;
204-
annotation.r = rect.width;
205-
annotation.r = rect.height;
202+
annotation.cx = rect.x + rect.width / 2;
203+
annotation.cy = rect.y + rect.height / 2;
204+
annotation.r = rect.width; // ignored right now
205+
annotation.r = rect.height; // ignored right now
206206
}
207207

208208
let { documentId, pageNumber } = getMetadata(svg);

src/UI/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ export function scaleUp(svg, rect) {
250250
return result;
251251
}
252252

253+
// Transform point by matrix
254+
//
255+
function applyTransform(p, m) {
256+
var xt = p[0] * m[0] + p[1] * m[2] + m[4];
257+
var yt = p[0] * m[1] + p[1] * m[3] + m[5];
258+
return [xt, yt];
259+
};
260+
253261
// Transform point by matrix inverse
254262
//
255263
function applyInverseTransform(p, m) {
@@ -381,6 +389,19 @@ export function convertToSvgPoint(pt, svg) {
381389
return applyInverseTransform(pt, xform);
382390
}
383391

392+
export function convertToScreenPoint(pt, svg) {
393+
let result = {};
394+
let { viewport } = getMetadata(svg);
395+
396+
let xform = [ 1, 0, 0, 1, 0, 0 ];
397+
xform = scale(xform, viewport.scale, viewport.scale);
398+
xform = rotate(xform, viewport.rotation);
399+
400+
let offset = getTranslation(viewport);
401+
xform = translate(xform, offset.x, offset.y);
402+
403+
return applyTransform(pt, xform);
404+
}
384405

385406
/**
386407
* Adjust scale from rendered scale to a normalized scale (100%).

src/render/renderCircle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export default function renderCircle(a) {
3737
function createCircle(r) {
3838
let circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
3939
setAttributes(circle, {
40-
cx: r.cx+15,
41-
cy: r.cy+15,
40+
cx: r.cx,
41+
cy: r.cy,
4242
r:30
4343
});
4444

0 commit comments

Comments
 (0)