Johan Kohlin
Lecturer at School of engineering, Jönköping University.
the canvas element, and animation
chapter 7
Chapter 9
Events & Timers
Schedule a function invocation
requestAnimationFrame(callback) // modern, better
setTimeout(callback, 1000/144) // old-school method
setInterval(callback, 1000) // infinite loop
requestAnimationFrame(callback)
var scheduleId = requestAnimationFrame(callback);
if(fun == false) {
cancelAnimationFrame(scheduleId);
}
Re-schedule a function invocation. Over and over
function animationLoop(){
// change something, then repeat (with delay)
requestAnimationFrame( animationLoop )
}
animationLoop() // kickstart the function
The canvas element is just an empty container with a width and a height.
<canvas width="300" height="150" id="myCanvas">
I'm sorry about your browser, it's not very good.
But hey, they got new ones at google.com/chrome.
<img src="images/sad_emoji.png" alt="">
</canvas>
...however is where the cool stuff is.
<canvas width="300" height="150" id="myCanvas">
I'm sorry about your browser, it's not very good.
But hey, they got new ones at google.com/chrome.
<img src="images/sad_emoji.png" alt="">
</canvas>
HTML
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var width = canvas.width;
var height = canvas.height;
JavaScript
canvas {
background-image: url(game_bg.png);
background-size: cover;
}
CSS
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var width = canvas.width;
var height = canvas.height;
<canvas width="640" height="480" id="myCanvas">
</canvas>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var width = canvas.width;
var height = canvas.height;
// ctx.fillRect(startX, startY, width, height)
ctx.fillRect(0, 0, 100, 50);
ctx.strokeRect(w/2, h/2, 50, 75);
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var width = canvas.width;
var height = canvas.height;
ctx.beginPath();
ctx.moveTo(startX,startY);
ctx.lineTo(endX,endY);
ctx.closePath();
ctx.stroke();
ctx.fill();
ctx.save() //saves the default state
ctx.fillStyle=color
ctx.strokeStyle=color
ctx.lineWidth=number
ctx.lineCap="butt"
ctx.lineJoin="miter"
ctx.miterLimit=integer
ctx.restore() //resets to the last saved state
// https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/restore
beginPath() //starts drawing a a path.
arc(startX, startY, radius, startAngle, endAngle)
//draws a circle where 2*Math.PI is the end angle in radians for a full circle
stroke() //adds a stroke to your shape
fill() //fills your shape
var imgData = canvas.toDataURL("image/png");
document.querySelector('a').href = imgData
document.querySelector('img').src = imgData
<canvas width="200" height="200"></canvas>
<img src="" alt="Canvas goes here" width="400" height="400">
<a href="">download canvas as Image</a>
erase, move, draw, repeat
var canvas = document.getElementById("star");
var ctx = canvas.getContext("2d");
var x = 0;
var y = 0;
var w = canvas.width;
var h = canvas.height;
function update(){
ctx.clearRect(0,0,w,h);
ctx.fillRect(x,y, 20,20);
x++;
y++;
requestAnimationFrame(update)
}
update();
on the loop
1. The image object can be in your HTML
<body>
<img src="birds.png" id="bird-pic" width="34" height="72">
</body>
2. Or you can create one in JS
var img = new Image();
img.src = "birds.png";
var img = document.getElementById("bird-pic");
The simple way:
var img = new Image();
img.src = "birds.png";
ctx.drawImage(img, 0,0);
//(imageSource, startX, startY)
The simple way:
var img = new Image();
img.src = "birds.png";
//ctx.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)
ctx.drawImage(img, 50, 70, 50, 50, 0, 0, 100, 100)
By Johan Kohlin