Web browsers have come a long way from their humble text origins to being able to display visually appealing graphics and animations. In the early 2000’s, browsers began to support an element called a “canvas,” which is exactly what it sounds like – a space for developers to create graphics, animations, and other visualizations.
We’re going to learn about the CodeHS canvas and some basic graphics in this tutorial.
The CodeHS canvas is essentially a blank rectangle with horizontal (x) and vertical (y) coordinates that we use to place our graphics objects. Click through the slides below to see how the coordinates are mapped onto the canvas and learn about two functions you will use often!
Remember, x and y both increase as you move away from the top left corner, which is different than the typical x-y coordinates in math, and the functions getWidth()
and getHeight()
will return the dimensions of the canvas.
Below is an example of a live CodeHS canvas! Drag your mouse around it to see the coordinates of where your mouse is on the canvas. See if you can match the width and height that is printed beneath.
With the CodeHS canvas and graphics library, it’s very easy to add graphics and edit their properties (e.g. size, position, color, etc). We’ll discuss the following graphics objects right now: text, circle, rectangle, and line. All of them are created in a very similar way:
Creating the Object:
Declare a variable and initialize it with the word “new” and the type of object you’re creating – Text(), Circle(), Rectangle(), or Line(). The required arguments for each of them vary by object; see the code below for details:
Adding it to the Canvas:
Once the object is created and stored in a variable, all we need to do is use the function add(graphicVariable)
to add it to the canvas! The code below adds all four objects created above to our canvas.
Check out the following example to see this in action! After you press Run, answer these questions:
As you can see above, just adding the graphics objects to our canvas ends in a confusion of black shapes, partially in view. In order to really use these objects effectively, we need to adjust their properties. The syntax for the these properties all follow the same pattern of graphicVariable.property(arguments)
. Each object type has its own set of slightly different properties we can change, but almost all of them fall into one of three categories: position, size, and color.
Text
textObject.setPosition(x, y)
moves the position of the lower left hand corner of the text to (x, y)
textObject.setText("[New Text]")
changes the value of the text to [New Text]
Circle
circleObject.setPosition(x, y)
moves the center of the circle to (x, y)
circleObject.setRadius(radius)
changes the radius of the circle, where radius can be any real number
Rectangle
rectangleObject.setPosition(x, y)
moves the position of the upper left hand corner of the rectangle to (x, y)
Line
lineObject.setPosition(x1, y1)
changes the starting position of the line to (x1, y1)
lineObject.setEndpoint(x2, y2)
changes the ending position of the line to (x2, y2)
All of the objects also share the following properties:
graphicVariable.setColor(Color.color)
where Color.color can be one of 10 main colors, like Color.red, Color.blue, Color.green, etc.
graphicVariable.getX()
gets the x-coordinate of the position of the object
graphicVariable.getY()
gets the y-coordinate of the position of the object
Take a look at the following example to see how these properties can be used in a program. After running the code, try completing the modifications below:
With a few shapes and properties, the creations are endless! Here’s a rainbow to make you happy.
What can YOU create?? The blank canvas awaits your creative mind and code! To start, try drawing a rectangle, circle, and a line that don’t overlap and fit on the canvas.