Skip to content

Instantly share code, notes, and snippets.

@chagan
Created February 20, 2012 02:10
Show Gist options
  • Save chagan/1867248 to your computer and use it in GitHub Desktop.
Save chagan/1867248 to your computer and use it in GitHub Desktop.
Javascript from TweetFace
$(document).ready(function(){
// Calls Twitter API, parses resulting JSON and sends info to GetPhoto()
var tweetFace = function() {
$("#eyer").attr("src","googlyeye_small.png");
$("#eyel").attr("src","googlyeye_small.png");
query=$("#query").val();
$("#text").html("Looking for a face and googly eyes.");
twitter = "https://api.twitter.com/1/users/show.json?screen_name="
twitter_json = 'http://twitter.com/users/show/'+query+'.json?callback=?';
$.ajax({
url: twitter_json,
dataType: 'json',
timeout : 1000,
success: function(data) {
name = data.name;
image_url = data.profile_image_url.replace("normal","bigger");
getPhoto(name,image_url);
},
complete: function(xhr, data) {
if (xhr.status === 0)
$("#text").html("Ah! Something went wrong! Twitter has failed us! Panic?");
//Delays showing Panic image. Otherwise image is shown while complete is working.
setTimeout(function() {
if (xhr.status === 0)
$("#avatar").attr("src", "panic.jpg");
},250);
hide_eyes();
}
});
//function to hide both eye images at one time
var hide_eyes = function(){
document.getElementById("eyel").style.visibility = "hidden";
document.getElementById("eyer").style.visibility = "hidden";
};
//Sets position of right and left eye
var eyeSet = function(eye, x, y, eye_size) {
jid = "#"+eye;
$(jid).css("left",x+'px');
$(jid).css("top",y+'px');
document.getElementById(eye).style.visibility = "visible";
$(jid).height(eye_size);
$(jid).width(eye_size);
};
//Calls Face API, cycles through tags in JSON, sets eye images and at end puts up Twitter image
var getPhoto = function (name,image_url) {
face_api = "API_KEY"
face_start = "http://api.face.com/faces/detect.json?api_key="
face_end = "&urls="
face = face_start+face_api+face_end+image_url;
$.getJSON(face, function(json) {
//loading height and width of photo
$.each(json.photos, function(i,photo) {
width = photo.width;
height = photo.height;
//checking if there is a face
if (photo.tags.length>0) {
//loop through tags, set eye positions and hide if no eye found
$.each(photo.tags, function(i,tag) {
eye_size = 40*(tag.width/100);
a2 = Math.pow(eye_size,2);
pythag = (Math.sqrt(a2))/2;
if (tag.eye_left != null) {
leftx = ((tag.eye_left.x/100)*width)-pythag;
lefty = ((tag.eye_left.y/100)*height)-pythag;
eyeSet("eyel",leftx,lefty,eye_size);
} else {
document.getElementById("eyel").style.visibility = "hidden";
};
if (tag.eye_right != null) {
rightx = ((tag.eye_right.x/100)*width)-pythag;
righty = ((tag.eye_right.y/100)*height)-pythag;
eyeSet("eyer",rightx,righty,eye_size);
} else {
document.getElementById("eyer").style.visibility = "hidden";
};
$("#text").html("We found a face! Huzzah!");
});
} else {
$("#text").html("We don't think there's a face in that photo. We may be wrong.");
hide_eyes();
};
//set twitter image
$("#avatar").attr("src", image_url);
});
});
};
};
$('#search').click(tweetFace);
$('#query').keyup(function(event){
if(event.keyCode == 13){
tweetFace();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment