-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml5eyeball.js
More file actions
560 lines (515 loc) · 21.5 KB
/
html5eyeball.js
File metadata and controls
560 lines (515 loc) · 21.5 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Html5 Canvas based eyeball
// 20140518 Rob: v1.0 release to Github
/*
The MIT License (MIT)
[OSI Approved License]
The MIT License (MIT)
Copyright (c) 2014 "Rob Davis", "rob_on_earth"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Eyeball class from here on
function Eyeball(elementId) {
var eyeball = this; // context
// settings
eyeball.targetId = elementId;
eyeball.centerX = 0;
eyeball.centerY = 0;
eyeball.irisColours = [{"red":0, "green":255, "blue":0}, {"red":255, "green":255, "blue":0}, {"red":0, "green":255, "blue":255}]
eyeball.hideRenderLayers = true;
eyeball.hideMarkers = true;
eyeball.useCircleScaling = true;
eyeball.numLinesIris = 400;
eyeball.eyeColour = {"red":0, "green":255, "blue":0};
eyeball.irisOuterColour = {"red":100, "green":100, "blue":100, "alpha":0.0 };
eyeball.irisInternalColour = {"red":0, "green":0, "blue":0};
eyeball.eyeballGradient1 = {"red":255, "green":255, "blue":255};
eyeball.eyeballGradient2 = {"red":255, "green":238, "blue":238};
eyeball.eyeballGradient3 = {"red":238, "green":221, "blue":221};
eyeball.veinColour = {"red":255, "green":180, "blue":180};
eyeball.veinWidth = 1.0;
eyeball.veinBranches = 5;
eyeball.travel = 1.0;
eyeball.pupilScale = 1.0;
eyeball.animatePupil = true;
eyeball.pupilColour = {"red":0, "green":0, "blue":0};
eyeball.highlight1 = {"x":.35, "y":.35, "width":.16, "height":.1, "colour1" : {"red":200, "green":200, "blue":200, "alpha" : 0.4}, "colour2" : {"red":0, "green":0, "blue":0, "alpha" : 0.0}};
eyeball.highlight2 = {"x":.64, "y":.64, "width":.04, "height":.02, "colour1" : {"red":200, "green":200, "blue":200, "alpha" : 0.2}, "colour2" : {"red":0, "green":0, "blue":0, "alpha" : 0.3}};
// internal vars
var isDrawing=false;
// all the different layers that are created dynamically
var canvases = [{"name":'canvasMain'}, {"name":'canvasBackground'}, {"name":'canvasVeins'}, {"name":'canvasIris'}, {"name":'canvasHighlights'}];
var animatedPupilScale = 0.0;
var pupilAnimationIntervalId = 0;
var pupilChange = 0.0;
var lastMouseX = 0;
var lastMouseY = 0;
var diameter = 0;
var maxDistanceFromCenter = 0;
var offsetLocationX = 0;
var offsetLocationY = 0;
// Colour can be supplied or pulled from the irisColours bank randomly
// If "animatePupil" setting is not disabled the pupil will also animate
eyeball.setIris = function(colour) {
if (colour) {
this.eyeColour.red = (colour.red) ? colour.red : 0;
this.eyeColour.green = (colour.green) ? colour.green : 0;
this.eyeColour.blue = (colour.blue) ? colour.blue : 0;
} else {
this.eyeColour = this.irisColours[Math.floor(this.getRandomRange(0, this.irisColours.length))];
}
var iris = this.getCanvas('canvasIris');
this.doDrawIris(iris, this.getCanvas('canvasMain').canvas.width);
if (this.animatePupil) {
pupilChange = 1.0;
clearInterval(pupilAnimationIntervalId);
pupilAnimationIntervalId = setInterval(function (eyeball) {
animatedPupilScale = 1.0 - eyeball.circ(1.0 - pupilChange);
pupilChange -= 0.02;
if (pupilChange < 0.0) {
pupilChange = 0.0;
clearInterval(pupilAnimationIntervalId);
}
eyeball.doDraw(lastMouseX, lastMouseY, eyeball.centerX, eyeball.centerY);
}, (1000 / 30), eyeball);
}
}
// allow backward compatibilty
eyeball.setPupil = function(colour) {
this.setIris(colour);
};
// Toggle the visibility of layers
// optionally force state
eyeball.toggle = function(name, forceShow){
var layer = this.getCanvas(name);
if (!layer) return;
var current = layer.display;
if (!current || forceShow) {
layer.display = true;
}
if (current || !forceShow) {
layer.display = false;
}
};
// Create a new canvas wrapper object
// or return the existing one
eyeball.newCanvas = function(targetId, canvasName) {
var canvasObject = this.getCanvas(canvasName);
if (!canvasObject) {
var canvas = document.createElement('canvas');
canvas.className = canvasName;
var context = canvas.getContext("2d");
return { "name": canvasName, "canvas":canvas, "context":context, "display":true};
}
return canvasObject;
};
// Get a canvas wrapper object based on the name
eyeball.getCanvas = function(name) {
for (var i = 0; i < canvases.length; i++) {
if (canvases[i].name == name && canvases[i].canvas) {
return canvases[i];
}
}
return;
};
// Initialise the canvases and render the components
// this needs to be done before calling the doDraw() method
eyeball.init = function() {
var targetDiv = document.getElementById(this.targetId);
for (var i=0;i<canvases.length;i++) {
if (this.getCanvas(canvases[i].name)) break;
canvases[i]=this.newCanvas(this.targetId, canvases[i].name);
if (this.hideRenderLayers && canvases[i].name!=='canvasMain' ) {
canvases[i].canvas.style.display = 'none';
}
targetDiv.appendChild(canvases[i].canvas);
}
// setup the canvas parameters and pre-renders
// Main
var main = this.getCanvas('canvasMain');
main.canvas.width = targetDiv.offsetWidth;
main.canvas.height = targetDiv.offsetHeight;
// Background
var background = this.getCanvas('canvasBackground');
background.canvas.width = main.canvas.width;
background.canvas.height = main.canvas.height;
this.doDrawBackground (background, background.canvas.width);
// Veins
var veins = this.getCanvas('canvasVeins');
veins.canvas.width = main.canvas.width * 2;
veins.canvas.height = main.canvas.height * 2;
this.doDrawVeins(veins, veins.canvas.width);
// Iris
var iris = this.getCanvas('canvasIris');
iris.canvas.width = main.canvas.width / 2;
iris.canvas.height = main.canvas.height / 2;
this.doDrawIris(iris, main.canvas.width);
// Highlights
var highlights = this.getCanvas('canvasHighlights');
highlights.canvas.width = main.canvas.width;
highlights.canvas.height = main.canvas.height;
this.doDrawHighlights(highlights, main.canvas.width)
diameter = main.canvas.width/2;
this.centerX = diameter; // set eyeball looking -
this.centerY = diameter; // - as if the mouse was at its center
maxDistanceFromCenter=diameter * 0.75; // can be altered with the "travel" setting
var offset = this.getOffset(targetDiv);
offsetLocationX = offset.Left;
offsetLocationY = offset.Top;
};
// Draw all the required canvas layers making up the "eye"
// Most commonly called from a "OnMouseMove" type event but can called directly
eyeball.doDraw = function(mX, mY) {
if (isDrawing) return; // already trying to draw this eyeball
var cX = this.centerX;
var cY = this.centerY;
lastMouseX = mX;
lastMouseY = mY;
var main = this.getCanvas('canvasMain');
var canvas = main.canvas;
var context = main.context;
var eyeBallDiameter = canvas.width;
var irisWidth = eyeBallDiameter / 2;
var offset = this.calculateScaleAndOffset(mX, mY, cX, cY);
var newPoint = this.getNewPoint(offset.radians, offset.distance)
var background = this.getCanvas('canvasBackground');
if (background.display)
context.drawImage(background.canvas, 0, 0);
var veins = this.getCanvas('canvasVeins');
if (veins.display) {
context.drawImage(veins.canvas, -diameter + newPoint.x, -diameter + newPoint.y);
// Use a circlular mask to hide the veins not on the eyeball
context.save();
context.globalCompositeOperation = 'destination-in';
context.beginPath();
context.arc(cX, cY, diameter, 0, 2 * Math.PI, false);
context.fill();
context.restore();
}
var iris = this.getCanvas('canvasIris');
if (iris.display)
this.updateIris(context, iris.canvas, cX, cY, offset, newPoint, diameter / 2);
var highlights = this.getCanvas('canvasHighlights');
if (highlights.display) {
context.save();
context.globalCompositeOperation = 'lighter';
context.drawImage(highlights.canvas, 0, 0);
context.restore();
}
isDrawing = false; // clear flag
};
// Draw the iris onto the requested layer
eyeball.doDrawIris = function(layer, eyeBallDiameter)
{
var canvas = layer.canvas;
var context = layer.context;
var irisDiameter = eyeBallDiameter / 2;
canvas.width = irisDiameter;
canvas.height = irisDiameter;
var numberOfLines = this.numLinesIris;
var offset = 0;
context.translate(offset, offset);
this.drawIris(context,irisDiameter, this.eyeColour, this.irisOuterColour, this.irisInternalColour, numberOfLines);
};
// Draw the veins onto the requested layer
eyeball.doDrawVeins = function(layer, eyeBallDiameter){
var context = layer.context;
context.save();
var veinColour = this.obj2Colour(this.veinColour);
context.translate(eyeBallDiameter / 2, eyeBallDiameter / 2);
var a = this.getRandomRange(0, 60);
for (var c = 0; a < 360; c += 1) {
this.genArm(context, 0, eyeBallDiameter / 2, eyeBallDiameter / 10, 90, 0.24, 0, this.veinBranches + 1, veinColour, this.veinWidth);
context.rotate(this.deg2Rad(a));
a += this.getRandomRange(25, 60);
}
context.restore();
};
// Draw the eyeball "ball" onto the requested layer
eyeball.doDrawBackground = function(layer, width)
{
var context = layer.context;
context.beginPath();
var radius = width / 2;
var gradient = context.createRadialGradient(radius, radius, 0, radius, radius, radius);
var eyeballGradient1 = this.obj2Colour(this.eyeballGradient1);
var eyeballGradient2 = this.obj2Colour(this.eyeballGradient2);
var eyeballGradient3 = this.obj2Colour(this.eyeballGradient3);
gradient.addColorStop(0.75, eyeballGradient1);
gradient.addColorStop(0.85, eyeballGradient2);
gradient.addColorStop(1.0, eyeballGradient3);
context.arc(radius, radius, radius, 0, 2 * Math.PI, false);
context.fillStyle = gradient;
context.fill();
context.closePath();
};
// Draw the two highlights onto the requested layer
eyeball.doDrawHighlights = function(layer, diameter)
{
var context = layer.context;
// top highlight
var centerX = diameter * this.highlight1.x;
var centerY = diameter * this.highlight1.y;
var hlWidth = diameter * this.highlight1.width;
var hlHeight = diameter * this.highlight1.height;
// correct coords once in the drawEllipse method
var topHighlight = function(context) {
gradient = context.createRadialGradient(1, 1, 0, 1, 1, 1);
gradient.addColorStop(0.5, eyeball.obj2Colour(eyeball.highlight1.colour1));
gradient.addColorStop(1.0, eyeball.obj2Colour(eyeball.highlight1.colour2));
return gradient;
};
this.drawEllipse(context, centerX, centerY, hlWidth, hlHeight, topHighlight);
// bottom highlight
var centerX = diameter * this.highlight2.x;
var centerY = diameter * this.highlight2.y;
var hlWidth = diameter * this.highlight2.width;
var hlHeight = diameter * this.highlight2.height;
// correct coords once in the drawEllipse method
var bottomHighlight = function(context) {
gradient = context.createRadialGradient(1, 1, 0, 1, 1, 1);
gradient.addColorStop(0.5, eyeball.obj2Colour(eyeball.highlight2.colour1));
gradient.addColorStop(1.0, eyeball.obj2Colour(eyeball.highlight2.colour2));
return gradient;
};
this.drawEllipse(context, centerX, centerY, hlWidth, hlHeight, bottomHighlight);
};
// Render the iris with background colour and the iris pattern
eyeball.drawIris = function (contextIris, diameter, eyeColour, irisOuterColour, irisInternalColour, numberOfLines)
{
radius = diameter / 2;
pupilRadius = diameter / 6;
this.drawEyeIrisBackground(contextIris, eyeColour, irisInternalColour, radius);
this.drawEyeIrisForeground(numberOfLines, contextIris, pupilRadius, radius, eyeColour, irisOuterColour);
};
// Render the iris background as a shaded circle
eyeball.drawEyeIrisBackground = function(context, eyeColour, irisOuterColour, radius)
{
context.beginPath();
gradient = context.createRadialGradient(radius, radius, 0, radius, radius, radius);
gradient.addColorStop(0.3, this.obj2Colour(eyeColour));
gradient.addColorStop(1.0, this.obj2Colour(irisOuterColour));
context.arc(radius, radius, radius, 0, 2 * Math.PI, false);
context.fillStyle = gradient;
context.fill();
context.closePath();
};
// Render the iris pattern by adding lines of different angles and length
eyeball.drawEyeIrisForeground = function(numberOfLines, context, pupilRadius, radius, eyeColour, irisOuterColour)
{
var angle = 0;
for (var c = 0; c < numberOfLines; c++) {
angle = Math.random()*360;
this.drawLine (angle, context, 0, pupilRadius, 0, radius, radius, eyeColour, irisOuterColour);
}
};
// Render the pupil
eyeball.drawPupil = function(context, radius, pupilRadius)
{
context.beginPath();
context.arc(radius, radius, pupilRadius, 0, 2 * Math.PI, false);
var pupilColour = this.obj2Colour(this.pupilColour);
context.fillStyle = pupilColour;
context.fill();
context.closePath();
};
// Render a single line with two blended colours and rotate to the specified angle
eyeball.drawLine = function(angle, context, lineX1, lineY1, lineX2, lineY2, radius, colour1, colour2, lineWidth) {
context.save();
context.beginPath();
context.translate(radius,radius);
context.rotate(this.deg2Rad(angle));
gradient = context.createLinearGradient(lineX1, lineY1, lineX2, lineY2);
gradient.addColorStop(0, this.obj2Colour(colour1));
gradient.addColorStop(this.getRandom(), this.obj2Colour(colour2));
context.strokeStyle = gradient;
context.moveTo(lineX1,lineY1);
context.lineWidth = lineWidth;
context.lineJoin = "round";
context.lineTo(lineX2, lineY2);
context.stroke();
context.closePath();
context.restore();
};
// Render an "arm" and try and create two branches at the end
// This is recursive, large values of maxCount (greater than 5) WILL slow your machine
eyeball.genArm = function(context, pPointX, pPointY, pLength, pAngle, pAngleRange, count, maxCount, style, lineWidth) {
count += 1;
if (count >= maxCount) return; // no more branches to make
var length = (pLength * this.getRandomRange(0.2, 1));
var angle = pAngle ;
var angleRad = this.deg2Rad(angle);
var x = pPointX + (0 - length) * Math.cos(angleRad);
var y = pPointY + (0 - length) * Math.sin(angleRad);
context.save();
context.beginPath();
context.moveTo(pPointX, pPointY);
context.lineTo(x, y);
context.strokeStyle=style;
context.lineWidth = lineWidth;
context.stroke();
context.restore();
// two new arms from this point, note the +/- angle changes
this.genArm (context, x, y, length, angle - (angle * this.getRandomRange(0.1, pAngleRange)), pAngleRange, count, maxCount, style, lineWidth);
this.genArm (context, x, y, length, angle + (angle * this.getRandomRange(0.1, pAngleRange)), pAngleRange, count, maxCount, style, lineWidth);
};
// Render a correctly scaled, rotated and located iris
// If setting "hideMarkers" is overridden then also renders the "spikes" indicating direction and "travel"
eyeball.updateIris = function(context, canvasIris, cX, cY, offset, newPoint, radius) {
context.save();
context.translate(cX, cY);
if (!this.hideMarkers) {
context.fillStyle = 'yellow';
context.fillRect(-3, -3 , 6, 6);
this.drawSpike(context, offset.angle, 'red', 1.0);
}
context.translate(newPoint.x, newPoint.y);
context.save();
context.rotate(this.deg2Rad(offset.angle));
context.scale(1.0, offset.scale);
context.rotate(this.deg2Rad(-offset.angle));
context.drawImage(canvasIris, -radius, -radius);
var pupilScale = (radius / 3) * this.pupilScale;
this.drawPupil(context, 0, pupilScale * (1.0 + animatedPupilScale) );
context.restore();
if (!this.hideMarkers) {
this.drawSpike(context, offset.angle, 'green', offset.scale);
}
context.restore();
};
// Calculate the scale and offset from mX,mY (mouse) to cX,cY(center)
// The "useCircleScaling" setting set to false allows you to see the old linear scaling method
eyeball.calculateScaleAndOffset = function(mX, mY, cX, cY) {
// offset
cX += offsetLocationX;
cY += offsetLocationY;
// set angle
var radians = Math.atan2(mY - cY, mX - cX);
var angle = 90 + this.rad2Deg(radians);
// new location based on distance restricted to the edge taking into account the "travel" setting
var userMaxDistanceFromCenter = maxDistanceFromCenter * this.travel;
var distance = this.getDistance(mX, mY, cX, cY);
if (distance > userMaxDistanceFromCenter) {
distance = userMaxDistanceFromCenter;
}
// the percentage of distance to maxDistanceFromCenter
// defines the inverse scale
// now with linear to circular curve
// FYI: I do see all those 1.0- inverters, but it works
var perct = 1.0-this.circ(1.0 - (distance / maxDistanceFromCenter));
var scale = 1.0-(perct * .50);
if (!this.useCircleScaling) {
perct = distance / maxDistanceFromCenter;
scale = 1.0 - ((perct == 0) ? 0 : (perct * .50));
}
return { "radians":radians, "angle":angle, "distance":distance, "percentageFromCenter":perct, "scale":scale}
};
// Render a triangle, used as markers when "hideMarkers" setting is false
eyeball.drawSpike = function(context, angle, style, yScale) {
context.save();
context.rotate(this.deg2Rad(angle));
context.scale(1.0, yScale);
context.beginPath();
context.moveTo(-10, 0);
context.lineTo(10, 0);
context.lineTo(0, -80);
context.fillStyle = style;
context.closePath();
context.fill();
context.strokeStyle = 'black';
context.stroke();
context.restore();
};
// Utility functions from here onwards
// return random value between 0 and 1.0
eyeball.getRandom = function() {
return Math.random();
};
// return random float value between min and max (including min and up to but not including the whole number max)
eyeball.getRandomRange = function(min,max) {
return min + ( max * this.getRandom());
};
// Get the distance between two points
eyeball.getDistance = function(p1X, p1Y, p2X, p2Y) {
var xDist = p1X - p2X;
var yDist = p1Y - p2Y;
return Math.sqrt(xDist * xDist + yDist * yDist);
};
// Get a new point relative to 0,0 based on supplied angle and distance
// object returned as {x,y}
eyeball.getNewPoint = function(radians, distance) {
var x = distance * Math.cos(radians);
var y = distance * Math.sin(radians);
return {"x":x, "y":y};
};
// Convert degrees to radians
eyeball.deg2Rad = function(degrees) {
return degrees * Math.PI / 180;
};
// Convert radians to degrees
eyeball.rad2Deg = function(radians) {
return radians * (180 / Math.PI);
};
// Draw an Ellipse
// fill can be a simple style or a function that returns a gradient
eyeball.drawEllipse = function(context, cx, cy, rx, ry, fill){
context.save();
context.beginPath();
context.translate(cx-rx, cy-ry);
context.scale(rx, ry);
var ellipseFill;
if (typeof(fill)==='function') {
ellipseFill=fill(context);
} else {
ellipseFill=fill;
}
context.arc(1, 1, 1, 0, 2 * Math.PI, false);
context.fillStyle = ellipseFill;
context.fill();
context.closePath();
context.restore();
};
// plots n as x on a semi circle and returns the y
// n cannot be outside the range 0.0 - 1.0 or the fabric of space maybe imperceptibly altered.
// work to create this was done visually here http://jsfiddle.net/rob_on_earth/7b2bx/ and http://jsfiddle.net/rob_on_earth/BLASN/
eyeball.circ = function(n) {
var angle90 = Math.PI / 2;
var radius = 1;
var y = radius * Math.sin(angle90 * n);
return y;
};
// Convert an object into a colour string
// avoid sending around seperate variables for colour components.
eyeball.obj2Colour = function(obj) {
var isAlpha = (typeof obj.alpha !== "undefined");
var colour = '(' + obj.red + ',' + obj.green + ',' + obj.blue + (isAlpha? ',' + obj.alpha : '') + ')';
return (isAlpha? 'rgba' : 'rgb') + colour;
};
// Get the pixel offset of the DOM element taking into account the page being scrolled.
// from http://javascript.info/tutorial/coordinates
eyeball.getOffset = function(elem) {
var box = elem.getBoundingClientRect()
var body = document.body
var docElem = document.documentElement
var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft
var clientTop = docElem.clientTop || body.clientTop || 0
var clientLeft = docElem.clientLeft || body.clientLeft || 0
var top = box.top + scrollTop - clientTop
var left = box.left + scrollLeft - clientLeft
return { "Top": Math.round(top), "Left": Math.round(left) }
};
} // end of Eyeball class