HTML5 Canvas Flashcards
Why does an image get rotated around the 0,0 canvas origin, instead of it’s own center?
Because you must first translate the canvas’s origin to the center of the object you wish to rotate.
Once the origin has been translated to the center of the object, what is the next step you need to do when drawing the object?
You have to subtract half of the width and the height to account for the translation of the canvas’s origin.
What functions are used to push / pop the context’s state before transforms?
context. save
context. restore
What function do you use to get the next animation frame (i.e. update the update method)?
window.requestAnimationFrame()
window.requestAnimationFrame requires a callback function, how do you deal with the this variable, if the callback is a member of this object?
Use bind to set the value of this within the callback.
window.requestAnimationFrame(this.update.bind(this));
The update method, once called by window.requestAnimationFrame, will need to call itself to maintain the loop, how do you set this up?
update: function () {
…
window.requestAnimationFrame(this.update.bind(this));
},
start: function () {
window.requestAnimationFrame(this.update.bind(this));
}
If you want to rotate an object so it faces a second object, what function would you use to determine it’s required angle of rotation?
var dx = bx - ax; var dy = by - ay; var angle = Math.atan2(dx, dy);
How would you set the identity matrix before performing a transform?
context.setTransform(1, 0, 0, 1, 0, 0);
How would you create a smooth up and down movement of an object - from it’s starting point, to +/- 100 units from that point?
Use a sine expression. Set the ‘angle’ to 0. And then use
yPos = objectInitialPos.y + Math.sin(angle) * 100;
angle += 0.1;
This will make the y value bounce between -100 and 100 above and below the starting point.
NOTE: Angle is analogous with speed
angle += 0.5 would be five times faster than the above example.
What is the canvas resolution mapped too?
CSS Pixels
What is the gotcha with CSS pixels and hi-rez screens?
It’s possible that a high rez screen will map multiple of it’s pixels, to a single CSS pixel.
Why does using CSS to resize a canvas not work?
It works like an image - so just setting the width (say 100%) will resize the image, and maintain the aspect ratio by automatically calculating the height.
But if you set both width and height in CSS, it will set the aspect ratio as per the new values, stretching the image.
If there are no values set on the element itself, it uses some default values.
This results in a stretched image.
Describe a canvas…
A resolution dependent canvas, that draws bitmap data on the ‘fly’.
What API is used for drawing shapes?
the SVG api. Scalable Vector Graphics.
Can you attach an event handler on a raw SVG element on a canvas?
No - there is no associated DOM element, so no you cannot.
When using canvas - which is best, CSS or DOM animation?
CSS - as it’s probably done on the GPU, DOM manipulation (and hence animation) is very expensive.
What is wrong with the following code?
c. beginPath(); c. moveTo(100, 100); c. lineTo(400, 400); c. stroke(); c. endPath();
There is no such function as c.endPath
What is the tag used to create a web gl context?
canvas.getContext(‘webgl’);
If webgl doesn’t work - what can we do?
Fall back to experimental.
gl = canvas.getContext(“webgl”) || canvas.getContext(“experimental-webgl”);
How do you draw a straight line?
c. beginPath();
c. moveTo(100, 100);
c. lineTo(400, 400);
c. stroke();
What does closePath do?
If you have a number of line segments (sub paths) within a line - calling closePath will close the shape.
c. beginPath(); c. moveTo(50, 50); c. lineTo(100, 250); c. lineTo(400, 250); c. lineTo(600, 50); c. closePath(); c. stroke();