﻿// Particles 1.0
// John Allwright

var plugin
var main
var myInt
// how many particles to animate
var maxParticles = 200;
var mainCanvasWidth
var mainCanvasHeight
// global speed switch
var scale=10;
// how fast does it fade
var decay;
// Where the particles are being emitted from
var originX
var originY
// the effect of gravity - how many pixels does the Y velocity lose each refresh
var gravity=1;
// friction applies on the baseline
var friction=0.95;
// duration of a particle
var timeToLiveMilliseconds=200
// frame refresh rate
var refreshMilliseconds=5

// called when plugin loaded event from interface.xaml
function mainCanvasLoaded(s) {	
    // get the canvas
	main=s.findName("mainCanvas");
	// get the usable area
	drawingSurface=main.findName("drawingSurface");
	// scale and find the centre to start our origin as we don't know the mouse start position
	mainCanvasHeight=drawingSurface["Height"];
	originY=Math.round(mainCanvasHeight/2);
	mainCanvasWidth=drawingSurface["Width"];
	originX=Math.round(mainCanvasWidth/2);
	//Calculate how the particle dies
	decay=refreshMilliseconds/timeToLiveMilliseconds;
	// get the plugin
	plugin = s.getHost()
    // create the particles
	for (i=0;i<maxParticles;i++)
	{
	    // create the XAML for a particle
	    var xaml_str = '<Canvas Name="canvas_dot_'+i+'" Canvas.Left="'+(originX)+'" Canvas.Top="'+(originY)+'">';
		xaml_str +=   	'<Ellipse Fill="white" Height="4" Width="4" Opacity="1"></Ellipse>'
		xaml_str += '</Canvas>';
	    //create a new object from the XAML
		var newParticle = plugin.content.createFromXaml(xaml_str);
		//add it to the canvas
		main.children.add(newParticle);
		//give it a random direction
	    particleXVel=Math.round((Math.random()-0.5)*scale);
		particleYVel=Math.round((Math.random()-0.5)*scale);
        //set up the callback passing velocities. Stagger the start using the loop index
		var animateCallString="animateParticle( '"+newParticle.name+"',"+particleXVel+","+particleYVel+")";
		setTimeout(animateCallString,refreshMilliseconds*i);
	}
}

//animate the particle
function animateParticle(particleName,XVel,YVel)
{
    //get the particle object from its name
    particle = main.findname(particleName);
    //get its X coord
    particleX = particle["Canvas.Left"];
    //move it X
    particle["Canvas.Left"] = particleX + XVel;
    //get the Y coord
    particleY = particle["Canvas.Top"];
    //Move it, let it rest on the botton of the canvas til death, 10 is fudge factor for bottom margin
    if(particleY<(mainCanvasHeight-10))
        particle["Canvas.Top"] = particleY + YVel;
    else
        XVel=XVel*friction;
    //Get its opacity
    particleOpacity = particle["Opacity"];
    //fade it
    particle["Opacity"] = (particleOpacity - decay);
    // If particle has vanished then time to start it off again
    if (particle["Opacity"]<0.005)
    {
        // reset opacity and start point
        particle["Opacity"]=1;
        particle["Canvas.Left"]=originX;
        particle["Canvas.Top"]=originY;
        // new random direction
        particleXVel=Math.round((Math.random()-0.5)*scale);
		particleYVel=Math.round((Math.random()-0.5)*scale);
    }
    else
    // pass the velocities for the next callback, add in gravity effects
    {   particleXVel=XVel;
        particleYVel=YVel+gravity;
    }
    // set up the callback once more
    var animateCallString="animateParticle( '"+particleName+"',"+particleXVel+","+particleYVel+")";
    setTimeout(animateCallString,refreshMilliseconds);
    
}
//grab the mouse coordinates
function whenMouseMoves(sender, mouseEventArgs)
{
    originX = mouseEventArgs.getPosition(null).x;
    originY = mouseEventArgs.getPosition(null).y;
}