I am making an interactive animation where bubbles appear on the screen and float around, and when the user moves the cursor a fish image (.png) will trail behind it.
I have gotten the bubbles to appear the way I would like them to, but the fish image will not appear on the screen for some reason.
I'm not sure what I did wrong?
Here is the code:
<body>
<!--Header explaining how to interact with the code-->
<div class="background">
</div>
<div class="header">
<h1>Just Keep Swimming</h1>
<h3>Oh no! The ocean is empty! Move your cursor to fill it with some fishy friends :)</h3>
</div>
<!--Canvas to hold all canvas javascript-->
<canvas id="canvas" width="1435" height="710">
</canvas>
<script>
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
//for the bubbles
var bubbles = new Array();
for (var i = 0; i < 50; i++) {
bubbles[i] = new Array();
bubbles[i]["size"] = Math.random() * 40 + 10;
bubbles[i]["xPos"] = Math.random() * 1435;
bubbles[i]["xDir"] = Math.random() < 0.5 ? 1 : -1;
bubbles[i]["xVel"] = Math.random() * 5;
bubbles[i]["yPos"] = Math.random() * 710;
bubbles[i]["yDir"] = Math.random() < 0.5 ? 1 : -1;
bubbles[i]["yVel"] = Math.random() * 5;
}
//xPos is the x position, xDir is x direction, xVel is x velocity
//and that is the same for yPos, yDir, and yVel
context.lineWidth = 5;
context.strokeStyle = "#6BCFE5";
context.fillStyle = "#F5EEDC";
context.globalAlpha = 0.5;
function frame(timestamp) {
context.clearRect(0, 0, 1435, 710);
for (var i = 0; i < 50; i++) {
context.beginPath();
context.arc(bubbles[i]["xPos"], bubbles[i]["yPos"], bubbles[i]["size"], 0, 2 * Math.PI);
context.stroke();
context.fill();
bubbles[i]["xPos"] = bubbles[i]["xPos"] + (bubbles[i]["xVel"] * bubbles[i]["xDir"]);
if (bubbles[i]["xPos"] > 1425) bubbles[i]["xDir"] = -1;
if (bubbles[i]["xPos"] < 20) bubbles[i]["xDir"] = 1;
bubbles[i]["yPos"] = bubbles[i]["yPos"] + (bubbles[i]["yVel"] * bubbles[i]["yDir"]);
if (bubbles[i]["yPos"] > 1425) bubbles[i]["yDir"] = -1;
if (bubbles[i]["yPos"] < 20) bubbles[i]["yDir"] = 1;
}
//this function allows the browser to animate
//the frame is gonna draw circle at 0 which is counter and 150 and then ask browser when your ready draw this circle here
window.requestAnimationFrame(frame);
};
frame();
//for the fish trailing image
//load image
var fish_image = new Image();
fish_image.src = 'img/fish-are-friends-not-food.png';
fish_image.onload = function() {}
// for the image to load when the mouse moves
canvas.addEventListener('mousemove', function(event) {
//get position of the mouse, this position is based on ...
//browser window, so need to adjust for that position
var xMouse = event.clientX;
var yMouse = event.clientY;
//adjust for the psoition of the canvas
xMouse -= (canvas.offsetLeft - canvas.scrollLeft + canvas.clientLeft);
yMouse -= (canvas.offsetTop - canvas.scrollTop + canvas.clientTop);
//adjust for the image
xMouse -= 65;
yMouse -= 65;
context.drawImage(fish_image, xMouse, yMouse);
});
</script>
</body>