HTML5 Canvas Flashcards

1
Q

Why does an image get rotated around the 0,0 canvas origin, instead of it’s own center?

A

Because you must first translate the canvas’s origin to the center of the object you wish to rotate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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?

A

You have to subtract half of the width and the height to account for the translation of the canvas’s origin.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What functions are used to push / pop the context’s state before transforms?

A

context. save

context. restore

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What function do you use to get the next animation frame (i.e. update the update method)?

A

window.requestAnimationFrame()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

window.requestAnimationFrame requires a callback function, how do you deal with the this variable, if the callback is a member of this object?

A

Use bind to set the value of this within the callback.

window.requestAnimationFrame(this.update.bind(this));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The update method, once called by window.requestAnimationFrame, will need to call itself to maintain the loop, how do you set this up?

A

update: function () {

window.requestAnimationFrame(this.update.bind(this));
},

start: function () {
window.requestAnimationFrame(this.update.bind(this));
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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?

A
var dx = bx - ax;
var dy = by - ay;
var angle = Math.atan2(dx, dy);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you set the identity matrix before performing a transform?

A

context.setTransform(1, 0, 0, 1, 0, 0);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you create a smooth up and down movement of an object - from it’s starting point, to +/- 100 units from that point?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the canvas resolution mapped too?

A

CSS Pixels

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the gotcha with CSS pixels and hi-rez screens?

A

It’s possible that a high rez screen will map multiple of it’s pixels, to a single CSS pixel.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why does using CSS to resize a canvas not work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe a canvas…

A

A resolution dependent canvas, that draws bitmap data on the ‘fly’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What API is used for drawing shapes?

A

the SVG api. Scalable Vector Graphics.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can you attach an event handler on a raw SVG element on a canvas?

A

No - there is no associated DOM element, so no you cannot.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When using canvas - which is best, CSS or DOM animation?

A

CSS - as it’s probably done on the GPU, DOM manipulation (and hence animation) is very expensive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is wrong with the following code?

c. beginPath();
c. moveTo(100, 100);
c. lineTo(400, 400);
c. stroke();
c. endPath();
A

There is no such function as c.endPath

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the tag used to create a web gl context?

A

canvas.getContext(‘webgl’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

If webgl doesn’t work - what can we do?

A

Fall back to experimental.

gl = canvas.getContext(“webgl”) || canvas.getContext(“experimental-webgl”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How do you draw a straight line?

A

c. beginPath();
c. moveTo(100, 100);
c. lineTo(400, 400);
c. stroke();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does closePath do?

A

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();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What constitutes a sub path within a path?

A

Any separate line within a beginPath and strokePath set.

Keep in mind - if you close a shape, that’s whole shape is a single sub path, any subsequent drawing of lines within that path, even if it joins from the last point on that sub path, constitutes a new subpath.

23
Q

if you use moveTo when drawing lines - does the path become broken?

A

There will be a gap, but it’s a legitimate path.

24
Q

Does ‘closePath’ actually close the path?

A

No - in fact it only closes the current subpath, and it starts a new subpath from the point where it is ended.

25
Q

What is wwrong with the following code?

c. beginPath();
c. moveTo(100, 100);
c. lineTo(400, 400);
c. lineTo(100, 400);
c. closePath();
c. beginPath();
c. lineTo(350, 100);
c. stroke();
A

Because the c.beginPath is called prior to c.stroke, nothing will draw. Secondly, the second subpath won’t draw because there was not start position.

26
Q

What is the current default path?

A

The current subpath that follows the beginPath call. The context only keeps track of one subpath, so any calls to beginPath before stroke is called will kill the current subpath, and create a new default subpath.

27
Q

What is the issue with the following bit of code?

c. beginPath();
c. moveTo(100, 100);
c. lineTo(100, 400);
c. lineWidth = 41;
c. stroke();
A

Because the linewidth is spread across the center line of a line - the number 41 will cause blurring as the edges of the line are not on the grid.

28
Q

What is the default lineCap?

A

‘butt’

29
Q

How do you make a linecap rounded?

A

c.lineCap = ‘round’

30
Q

What is the radius of a round linecap?

A

half the thickness of the width of the line

31
Q

What does lineCap = ‘square’ do?

A

Adds a square to the top of the line - that is the same width and height as the line’s width. (basically makes it taller)

32
Q

What is the default c.lineJoin?

A

miter

33
Q

What kind of lineJoin’s can we use?

A

miter, round, bevel

34
Q

Can you fill an unclosed path?

A

Yes - the default colour is blakc.

35
Q

How would you set the stroke color of a path?

A

c.strokeStyle = ‘rgb(255, 255, 0)’;

36
Q

How would you set the fill colour of a path?

A

c.fillStyle = ‘rgb(255, 255, 0)’;

note - it’s in a string, and you can use rgba, hsl and hsla

37
Q

How do you draw a filled rectangle in canvas?

A

c. beginPath()
c. fillStyle = ‘hsl(50, 100%, 50%)’;
c. fillRect(100, 100, 24, 24);

38
Q

How do you draw a stroked rectangle in canvas

A

c. beginPath()
c. strokeStyle = ‘hsl(50, 100%, 50%)’;
c. strokeRect(100, 100, 24, 24);

39
Q

How do you clear a section of the canvas of all pixels?

A

c.clearRect(0, 0, 10, 10);

40
Q

Does using any rectangle drawing function affect the current default path?

A

No - you can draw a line and stroke it - then draw a bunch of squares - if you draw another line segment (using lineTo and stroke) it will continue from where the line left off.

41
Q

How does an arc work?

A

context.arc(x, y, radius, startangle, endangle, [anticlockwise])

42
Q

How would you draw a circle using the arc function?

A

c. beginPath();
c. arc(50, 50, 18, 0, Math.PI * 2, false);
c. strokeStyle = ‘hsl(40, 100%, 50%)’;
c. lineWidth = 3;
c. lineCap = ‘round’;
c. stroke();

NOTE: Math.PI * 2 - gives a full circle in radians

43
Q

What is the difference between strokeRect, fillRect and rect?

A

strokeRect/fillRect will draw immediately to the canvas - and is not part of a path.

Rect on the other hand requires a beginPath() call - and is part of the path, allowing it to become part of other shapes.

44
Q

How do you reset a translation?

A

There are technically two methods:

context. save()
context. restore()

OR

context.setTransform(1, 0, 0, 1, 0, 0);

45
Q

Is a translation cumalitive?

A
Yes - if you apply
context.translate(400, 400);
// then
context.translate(400, 400);
The origin will now be at 800, 800
46
Q

How do you translate the origin before drawing?

A

context.translate(400, 400);

This will give you the ability to use atan2 in a polar sense, with the origin in the middle of the page.

47
Q

What do you have to keep in mind with results from atan2?

A

The range is between [-π, π] - which means you don’t know the 360 value. To change this, (assuming radians)…

if (angle

48
Q

What happens when you apply a scale transform?

A

c.scale(2, 2);

It’s not actually scalling the image - rather it’s scaling the coordinates, so 2 in the x direction means, 1 pixel is now represented by 2 pixels.

49
Q

How do you create and use a linear gradient?

A
// Assume c is our context
var lg = c.createLinearGradient(0, 0, 800, 0);
// You need to add the colors to lg
lg.addColorStop(0, 'rgb(200, 100, 23)');
lg.addColorStop(0.5, 'rgb(3, 3, 43)');
c.fillStyle = lg; // Assign the gradient
c.beginPath();
c.fillRect(0, 0, 800, 800);

NOTE: that the first parameter to addColorStop is the index for where the gradient begins - it’s a value between 0 - 1

50
Q

When you create a linear gradient, but it’s less than the width of the object you are filling, what happens?

A

The gradient stops, and the last colour drawn is repeated.

NOTE: This means gradients don’t fill the object, they are essentially ‘under’ the object.

51
Q

Can you use a gradient on the stroke?

A

Yes. It’s exactly the same as using it on the fill - but you just call stroke.

52
Q

What is the global alpha?

A

It’s a value that determines how transparent everything is.

c.globalAlpha = 0.5; // 50% transparency

53
Q

How do you prevent things from being drawn with the same alpha later on?

A

You can do the alpha between a ‘save / restore’ pair.