HTML5+JS

Effects are created using vanilla JavaScript on an HTML5 canvas element. In this section, we will cover the basics: drawing shapes, drawing with iteration, and adding motion.

Drawing Shapes

For this tutorial I will be using a generic lightscript template:

Template
Copy

Lines

Whether using one line or many, the process remains the same.

  1. ctx.beginPath() starts your shape
  2. ctx.moveTo(x, y) sets the first point
  3. ctx.lineTo(x, y) sets the next point and a line back to the first. Use multiple lineTo's to draw larger shapes.
  4. Set ctx.strokeStyle and/or ctx.fillStyle (if the shape is closed)
  5. Use ctx.stroke() to draw the lines, and ctx.fill() to fill any closed shapes. It’s important to note that up until this point, everything is hypothetical - nothing is drawn until these commands are executed.

Here are three examples using lines:

Lines
Copy

It's important to note the draw order here: the filled triangle is drawn last, so it would cover the other two shapes if they were in the same position. Additionally, the stroke in the triangle is drawn after the fill. If we reversed these steps, the interior half of the stroke would be covered by the red fill.

Rectangles

Rectangles are simple and useful, especially for the average resolution of an RGB keyboard. The process is similar to drawing lines - just replace the moveTo and lineTo commands with rect(x, y, width, height).

Rectangles
Copy

Arcs

Arcs are drawn by rotating an outer point around a central point, and can be used to create full circles. The arc command is ctx.arc(x, y, radius, start angle, end angle), where both angle measure are given in radians. Unlike rectangles, an arc is drawn with the shape's origin (x, y) in the center of the arc.

Arcs
Copy

As you can see, arcs are drawn clockwise by default. This can be reversed with an end angle that is less than the start angle, or by adding "true" as a final argument to the ctx.arc() method.

Drawing With Iteration

Drawing three shapes by hand can easily take twenty lines of code, so if you need to draw one hundred shapes, we need a more efficient approach. Loops in JavaScript are a simple way to repeat tasks, and they can be applied to drawing as well.

Here’s an example using a for-loop to draw a row of checkered squares:

Checker Row
Copy

Here is a while-loop example to draw a checkered grid. I will change the lightness of the black squares and the hue of the white squares slightly in each iteration using template literals.

Checkered Grid
Copy

Adding Motion

Animations in the canvas are limited only by your imagination and your mathematical knowledge. If you feel shaky with trigonometry or geometry I would recommend doing some light research in order to push your skills to the next level. Over the next few examples I will take a stationary circle and attach every possible variable to a simple oscillating motion.

The gold standard for oscillation (regular back-and-forth movement) is to take the sin() or cosine() of a timer. Either operation will return a value between -1 and 1 for ANY value passed in, even if it is only counting up. The only difference between the two is that they are slightly out of sync - when cos is 0 sin is -1 or 1, and vice versa. We can use this to our advantage in this animation -

Circley Circles
Copy

Side-to-Side

Up-and-Down

Full Circle

We only have a few more attributes to alter over time - circle radius, fill, and stroke. I'll be leaving the arc angles alone for now to keep the shape simple because the rest will be pretty flashy.

Flashy
Copy

Every attribute of every shape we have created so far can be altered over time, and every animation I make is a combination of the above skills. For more detail on crafting re-usable, efficient animations, check out our Callbacks page!

Type to search, ESC to discard
Type to search, ESC to discard
Type to search, ESC to discard