-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgooglyeyes.js
71 lines (60 loc) · 1.98 KB
/
googlyeyes.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
function googlyEye(canvasId, posX, posY, relSize)
{
var eyeContainer = document.getElementById("eyesContainer");
eyeContainer.style.setProperty("position", "relative");
var canvas = document.createElement("canvas");
canvas.id = canvasId;
canvas.width = relSize/100 * eyeContainer.clientWidth;
canvas.height = canvas.width;
canvas = eyeContainer.appendChild(canvas);
var canvasStyle = canvas.style;
var context = canvas.getContext("2d");
var halfWidth = canvas.width/2;
canvasStyle.setProperty("position", "absolute");
canvasStyle.setProperty("top", Math.floor(eyeContainer.clientWidth*(posY-relSize/2)/100) + "px");
canvasStyle.setProperty("left", Math.floor(eyeContainer.clientWidth*(posX-relSize/2)/100) + "px");
var eyeLineWidth = 0.2 * halfWidth;
var eyeRadius = halfWidth - eyeLineWidth/2;
var pupilRadius = 0.25 * eyeRadius;
var sideEyeRatio = 0.4; // How far the pupil can sink into the line around the eye
function drawWhite()
{
// Clear all
canvas.width = canvas.width;
// Eye white
context.beginPath();
context.arc(halfWidth, halfWidth, eyeRadius, 0, 2*Math.PI, false);
context.fillStyle = "#ffffff";
context.fill();
context.lineWidth = eyeLineWidth;
context.strokeStyle = "#000000";
context.stroke();
}
function d(x,y)
{
return Math.sqrt(x*x + y*y);
}
function relDistFromCentre(x, y)
{
var r = d(x,y);
return (eyeRadius-pupilRadius-(1-sideEyeRatio)*eyeLineWidth) * (1 - Math.exp( -r/(3*eyeRadius) )) / r;
}
function drawEye(x, y)
{
var r = relDistFromCentre(x, y);
drawWhite();
// Pupil
context.beginPath();
context.arc(halfWidth+r*x, halfWidth+r*y, pupilRadius, 0, 2*Math.PI, false);
context.fillStyle = "#000000";
context.fill();
context.lineWidth = 0;
context.strokeStyle = "#000000";
context.stroke();
}
window.addEventListener("mousemove", function(mousePos){
var x = mousePos.pageX - canvas.offsetLeft - halfWidth;
var y = mousePos.pageY - canvas.offsetTop - halfWidth;
drawEye(x, y);
}, false);
}