Skip to content

Instantly share code, notes, and snippets.

@iloveluce
Last active July 12, 2016 02:44
Show Gist options
  • Save iloveluce/0b4390ae1d9524028cf082f7279002c2 to your computer and use it in GitHub Desktop.
Save iloveluce/0b4390ae1d9524028cf082f7279002c2 to your computer and use it in GitHub Desktop.
Eyes
license: none
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
.Eye{
fill: white;
}
.pupil{
fill: black;
}
</style>
<body>
</body>
<script>
var dimensions = {"width": 800 , "height": 800, "eye_spread": 40, "eye_size": 25}
body = d3.select("body")
svg = body.append("svg")
.attr("width", dimensions["width"])
.attr("height", dimensions["height"])
.style("background-color","lightgray");
LeftEye = svg.append("circle")
.attr("r", dimensions["eye_size"])
.attr("cx", dimensions["height"]/2 - dimensions["eye_spread"])
.attr("cy", dimensions["height"]/2)
.attr("class","Eye")
LeftPupil = svg
.append("circle")
.attr("r", dimensions["eye_size"]/4)
.attr("cx", dimensions["height"]/2 - dimensions["eye_spread"])
.attr("cy", dimensions["height"]/2)
.attr("class","pupil")
RightEye = svg.append("circle")
.attr("r", dimensions["eye_size"])
.attr("cx", dimensions["height"]/2 + dimensions["eye_spread"])
.attr("cy", dimensions["height"]/2)
.attr("class","Eye")
RightPupil = svg
.append("circle")
.attr("r", dimensions["eye_size"]/4)
.attr("cx", dimensions["height"]/2 + dimensions["eye_spread"])
.attr("cy", dimensions["height"]/2)
.attr("class","pupil")
eyeXScale = d3.scale.linear()
.domain([0, body.node().clientWidth])
.range([-dimensions["eye_size"]/2, dimensions["eye_size"]/2])
eyeYScale = d3.scale.linear()
.domain([0, body.node().clientHeight])
.range([-dimensions["eye_size"]/2, dimensions["eye_size"]/2])
Eyes = d3.selectAll(".pupil")
body.on("mousemove", function () {
x = eyeXScale(d3.event.pageX);
y = eyeYScale(d3.event.pageY);
Eyes
.attr("transform", function(){
return "translate(" + x + "," + y + ")";
})
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment