var animFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || null ;
window.addEventListener('mousemove', saveMove, false);
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
//Global vars
var clickX, clickY;
//Loops through to draw the graphics
mainLoop = function() {
drawScreen();
};
//This loops the animation frames
var recursiveAnim = function() {
mainLoop();
animFrame(recursiveAnim);//
};
var drawCanvasImage = new Image();
drawCanvasImage.onload = function(){
animFrame(recursiveAnim);
};
drawCanvasImage.src = "https://www.w3.org/html/logo/downloads/HTML5_Logo_256.png";
function drawScreen() {
//sky
ctx.fillStyle="#33ccff";
ctx.fillRect(0, 0, c.width, c.height);
//sun
ctx.beginPath();
ctx.arc(680, 150, 90, 10, Math.PI, true);
ctx.closePath();
ctx.lineWidth = 5;
ctx.fillStyle = '#ffff33';
ctx.fill();
ctx.strokeStyle = '#b2b300';
ctx.stroke();
//grass
ctx.beginPath();
ctx.arc(350, 950, 800, 0, Math.PI, true);
ctx.closePath();
ctx.lineWidth = 5;
ctx.fillStyle = '#3fff00';
ctx.fill();
ctx.strokeStyle = '#1f8000';
ctx.stroke();
ctx.drawImage(drawCanvasImage, 0, 0);
//Hey SO, thanks for checking this out! :)
}
// Mouse click stuff #############################################
{
function saveMove(e) {
var pos = getMousePos(c, e);
clickX = pos.x;
clickY = pos.y;
}
function getMousePos(c, evt) {
var rect = c.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
}
}
}
.title {
font-family: "Helvetica", sans-serif;
}
<canvas id="myCanvas" width="800" height="300" style="border:1px solid #000000"></canvas>