-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.annotate.js
149 lines (126 loc) · 4.29 KB
/
jquery.annotate.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*!
* jQuery Annotation Plugin
* http://www.zurb.com/playground/javascript-annotation-plugin
*
* Copyright 2010, ZURB
* Released under the MIT License
*/
(function($){
$.lefunc = function(){
alert("lefunc");
}
$.fn.annotatableImage = function(annotationCallback, options) {
var defaults = {
xPosition: 'middle',
yPosition: 'middle'
};
var options = $.extend(defaults, options);
annotations = [];
var image = $('img', this)[0];
var date = new Date();
var startTime = date.getTime();
this.click(function(event){
if (event.target == image) {
// event.preventDefault();
console.log("width: ", options.width, "height: ", options.height)
var element = annotationCallback(options.width, options.height);
annotations.push(element);
$(this).append(element);
element.positionAtEvent(event, options.xPosition, options.yPosition);
var date = new Date();
element.data('responseTime', date.getTime() - startTime);
} else{
console.log("failed to display annotation");
}
});
};
$.fn.addAnnotations = function(annotationCallback, annotations, options) {
var container = this;
var containerHeight = $(container).height();
var defaults = {
xPosition: 'middle',
yPosition: 'middle',
height: containerHeight
};
var options = $.extend(defaults, options);
$.each(annotations, function() {
var element = annotationCallback(this);
element.css({position: 'absolute'});
$(container).append(element);
var left = (this.x * $(container).width()) - ($(element).xOffset(options.xPosition));
var top = (this.y * options.height) - ($(element).yOffset(options.yPosition));
if (this.width && this.height) {
var width = (this.width * $(container).width());
var height = (this.height * $(container).height());
element.css({width: width + 'px', height: height + 'px'});
}
element.css({ left: left + 'px', top: top + 'px'});
if (top > containerHeight) {
element.hide();
}
});
};
// determines actual location of annotations
$.fn.positionAtEvent = function(event, xPosition, yPosition) {
var container = $(this).parent('div');
$(this).css('position', 'absolute');
$(this).css('left', event.pageX - container.offset().left - ($(this).xOffset(xPosition)) + 'px');
$(this).css('top', event.pageY - container.offset().top - ($(this).yOffset(yPosition)) + 'px');
};
$.fn.seralizeAnnotations = function(xPosition, yPosition) {
var annotations = [];
this.each(function(){
annotations.push({x: $(this).relativeX(xPosition), y: $(this).relativeY(yPosition), response_time: $(this).data('responseTime')});
});
return annotations;
};
$.fn.relativeX = function(xPosition) {
var left = $(this).coordinates().x + ($(this).xOffset(xPosition));
var width = $(this).parent().width();
// console.log("left: ", left, "width: ", width);
return left / width;
}
$.fn.relativeY = function(yPosition) {
var top = $(this).coordinates().y + ($(this).yOffset(yPosition));
var height = $(this).parent().height();
// console.log("top: ", top, "height: ", height);
return top / height;
}
$.fn.dragRelativeX = function(x) {
var left = x;
var width = $(this).parent().width();
// console.log("dragRelativeX: ", "left: ", left, "width: ", width);
// console.log("dragRelativeX: ", left / width);
return left / width;
}
$.fn.dragRelativeY = function(y) {
var top = y;
var height = $(this).parent().height();
// console.log("dragRelativeY: ", "top: ", top, "height: ", height);
// console.log("dragRelativeY: ", top / height);
return top / height;
}
$.fn.relativeWidth = function() {
return $(this).width() / $(this).parent().width();
}
$.fn.relativeHeight = function() {
return $(this).height() / $(this).parent().height();
}
$.fn.xOffset = function(xPosition) {
switch (xPosition) {
case 'left': return 0; break;
case 'right': return $(this).width(); break;
default: return $(this).width() / 2; // middle
}
};
$.fn.yOffset = function(yPosition) {
switch (yPosition) {
case 'top': return 0; break;
case 'bottom': return $(this).height(); break;
default: return $(this).height() / 2; // middle
}
};
$.fn.coordinates = function() {
return {x: parseInt($(this).css('left').replace('px', '')), y: parseInt($(this).css('top').replace('px', ''))};
};
})(jQuery);