#include "Canvas.as"

var C = new Canvas();
var n_count = 0;
var n_duration = 30 * 2;
var scalex = .2;
var scaley = .1;

this.onEnterFrame = function() {
n_count = (n_count + 1) % n_duration;
draw(n_count);
}

function draw(n) {
C.setBackgroundColor(1,1,1);
C.setpenWeight(.1)
C.setpenColor(1,0,0);
for(h=.01;h<.5;h+=.1) {
C.drawOval(false, .5, .5, h, h);
}
var A = new Array(1-(n_count/n_duration),.25+(n_count/(n_duration*2))); //move arrow position each frame with a scale of .5
if(1-(n/n_duration) > .8) { //at the start draw a line to the edge of the page
offpage_character(C, A);
}

else {
if(1-(n/n_duration) > .5) {// then animate character
moving_character(C, A, scalex, scaley);
}
else {//then stop the character in the bullseye
still_character(C,scalex,scaley);
}
}
}

function offpage_character(C, A) {
C.setpenColor(0,0,0);
C.setpenWeight(.05);
C.drawLine(A[0],A[1],1,.25);//draw arrow
feather(C, A, scalex, scaley); //add feather at end
eyes(C, A, scalex, scaley); // add 2 eyes
mouth(C, A, scalex, scaley); //add a mouth
nose(C, A, scalex, scaley); //add a nose
}

function still_character(C, scalex,scaley) {
var bullseye = new Array(.5,.5);
C.setpenColor(0,0,0);
C.setpenWeight(.01);
C.drawLine(.47,.48,.52,.54);//draw arrow
C.setpenWeight(.05);
C.drawLine(bullseye[0],bullseye[1], bullseye[0] + scalex, bullseye[1] - scaley);
C.setpenWeight(.01);
var x = bullseye[0] + scalex; //set starting point for feathers from old ending point on the line for x
var y = bullseye[1] - scaley; //and for y
var t = scalex/20;
var u = scaley/20;
for(r=-5;r<0;r+=.5) {//draw feathers
C.drawLine(x + (t*r), y - (u*r), x + (t*r) + .05, y - (u*r)); // ensure feather angle matches shaft angle
C.drawLine(x + (t*r), y - (u*r), x + (t*r), y - (u*r)- .05); // by using a multiple of scalex and scaley
C.drawLine(x + (t*r), y - (u*r), x + (t*r) + .03, y - (u*r)-.03);//for translating
}
C.setpenWeight(scalex/2);
C.setpenColor(1,1,1);
var x = bullseye[0] + (scalex/2);// draw eyes from midpoint of arrow for x
var y = bullseye[1] - (scaley/2);// and y
C.drawLine(x+.01,y+.01,x+.011,y+.01);//use glitch for eyeballs
C.drawLine(x-.01,y-.01,x-.011,y-.01);
C.setpenWeight(scalex/4);
C.setpenColor(0,0,0);
C.drawLine(x+.01,y+.01,x+.0105,y+.01);//use glitch again
C.drawLine(x-.01,y-.01,x-.0105,y-.01);
var a = bullseye[0] + (((.3+ scalex) - .3)/4);//draw mouth a guarter from the bottom of arrow for x
var b = bullseye[1] - ((.6-(.6-scaley))/4);// and y
C.setpenWeight(scalex/6);
C.setpenColor(0,0,0);
C.drawLine(a-scalex/10,b-scaley/10,a+scalex/10,b+scaley/10);
C.setpenWeight(scalex/30);
C.setpenColor(1,1,1);
C.drawLine(a-scalex/10,b-scaley/10,a+scalex/10,b+scaley/10);
var m = bullseye[0] + (scalex/2.5);//draw nose a little less than 1/2 the way up the arrow
var s = bullseye[1] - (scaley/2.5);
C.setpenWeight(scalex/3);
C.setpenColor(0,0,1);
C.drawLine(m,s,m+.005,s);
}

function moving_character(C, A, scalex, scaley) {
C.setpenColor(0,0,0);
C.setpenWeight(.05);
C.drawLine(A[0], A[1], A[0] + scalex, A[1] - scaley);//draw arrow shaft
feather(C, A, scalex, scaley); //add feather at end
eyes(C, A, scalex, scaley); // add 2 eyes
mouth(C, A, scalex, scaley); //add a mouth
nose(C, A, scalex, scaley); //add a nose

}

//draw end feather
function feather(C, A, scalex, scaley) {
C.setpenWeight(.01);
var x = A[0] + scalex; //set starting point from old ending point on the line for x
var y = A[1] - scaley; //and for y
var t = scalex/20;
var u = scaley/20;
for(r=-5;r<0;r+=.5) {
C.drawLine(x + (t*r), y - (u*r), x + (t*r) + .05, y - (u*r)); // ensure feather angle matches shaft angle
C.drawLine(x + (t*r), y - (u*r), x + (t*r), y - (u*r)- .05); // by using a multiple of scalex and scaley
C.drawLine(x + (t*r), y - (u*r), x + (t*r) + .03, y - (u*r)-.03);//for translating
}
}

//draw eyes
function eyes(C, A, scalex, scaley) {
C.setpenWeight(scalex/2);
C.setpenColor(1,1,1);
var x = A[0] + (scalex/2);// draw eyes from midpoint of arrow for x
var y = A[1] - (scaley/2);// and y
C.drawLine(x+.01,y+.01,x+.011,y+.01);//use glitch for eyeballs
C.drawLine(x-.01,y-.01,x-.011,y-.01);
C.setpenWeight(scalex/4);// size of eyes based on scale
C.setpenColor(0,0,0);
C.drawLine(x+.01,y+.01,x+.0105,y+.01);//use glitch again
C.drawLine(x-.01,y-.01,x-.0105,y-.01);
}

//draw mouth
function mouth(C, A, t, u) {
var x = A[0] + (t/4);//draw mouth a guarter from the bottom of arrow for x
var y = A[1] - (u/4);// and y
C.setpenWeight(t/6);// size of mouth based on scale
C.setpenColor(0,0,0);
C.drawLine(x-t/10,y-u/10,x+t/10,y+u/10);
C.setpenWeight(t/30);// size of mouth based on scale
C.setpenColor(1,1,1);
C.drawLine(x-t/10,y-u/10,x+t/10,y+u/10);
}

//draw nose
function nose(C, A, scalex, scaley) {
var x = A[0] + ( scalex/2.5);
var y = A[1] - (scaley/2.5);
C.setpenWeight(scalex/3);
C.setpenColor(0,0,1);
C.drawLine(x,y,x+.005,y);
}