I'm hoping someone can explain how the the below function expression works. The parameter 'p' has not been assigned a value, however it is used in the body of the function for calculations (defining the "angle" and "tail" variables) and as an argument object when draw() is called. When you step through the code, you can see that parameter 'p' does, indeed, have a value (see attached image)- but I don't understand where that value comes from. Please note that 'p' is not noted anywhere else upon review of the full code.
When you log 'p' to the console you see the following values, which continue to increase as the application runs: Image of console.log(p);
Here is the full function expression and the function call:
var draw = function(p) {
$.fillStyle = "hsla(38,5%,12%,.90)";
$.fillRect(0, 0, w, h);
$.fillStyle = "hsla(38, 25%, 90%, 1)";
$.strokeStyle = "hsla(38, 25%, 90%, 1)";
for (var i = 0; i < numh; i++)
for (var j = 0; j < numw; j++) {
var diagnalW = j * spacing +
(i % 2 ? 0 : spacing / 2);
var diagnalH = i * spacing;
var arr = [position[0] - diagnalW,
position[1] - diagnalH
],
wave = Math.sqrt(arr[0] * arr[0] +
arr[1] * arr[1]),
arr = [arr[0] / wave, arr[1] / wave],
angle = 50 * (Math.cos(p / 360 - wave / 105) - 1);
$.beginPath();
$.arc(diagnalW + arr[0] * angle, diagnalH +
arr[1] * angle, 2.8, 0, 2 * Math.PI, false);
$.closePath();
$.fill();
for (var n = 0; n < 5; n++) {
var tail = 50 * (Math.cos((p - 50 * n) /
360 - wave / 105) - 1);
$.beginPath();
$.moveTo(diagnalW + arr[0] * angle, diagnalH +
arr[1] * angle);
$.lineWidth = 5 - n;
$.lineTo(diagnalW + arr[0] * tail, diagnalH + arr[1] * tail);
$.stroke()
}
}
};
var anim = function(p) {
window.requestAnimationFrame(anim);
draw(p);
};
anim();
I understand everything about this code with the exception of how 'p' obtains the values shown in the console. Also - in case it wasn't clear, this is an html5 canvas application.
(Side note: No, $
in the above isn't jQuery. It's just what the original author uses for her canvas context object.)