Drawing in the HTML Canvas¶
Initializing a Canvas¶
The Canvas API provides a means for drawing graphics via JavaScript and the canvas
element.
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API
Drawing in the Canvas¶
Writing some text:
Filling a rectangle:
Drawing an arc:
Free drawing:
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API
Transformations in the Canvas¶
Transformations enables more powerful ways to translate the origin to a different position, rotate the grid and even scale it.
Canvas states are stored on a stack: - When the save()
method is called, the current drawing state is pushed onto the stack. - When the restore()
method is called, the last saved state is popped off the stack and all saved settings are restored.
When you perform transformations on the grid to draw an object you often want to restore a prior state to draw the next object.
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Transformations
Rendering Loop and Game Loop¶
The setTimeout method sets a timer which executes a function or specified piece of code once the timer expires.
The setInterval method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.
The requestAnimationFrame method tells the browser that you wish to perform an animation and requests that the browser call a specified function to update an animation before the next repaint.
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals