🎨 What is a Canvas?
In programming, a canvas is like a digital blank space where all the visual elements of your game come to life. Think of it as the screen or window where your game runs. Without a canvas, sprites have nowhere to exist—it's like trying to paint without having any surface to paint on!
Click anywhere on the canvas to move the golf ball!
🖥️ Purpose of a Coding Canvas
The primary purpose of a coding canvas is to display code execution results. It's where your code comes to life visually! When you write code to create a sprite, draw a shape, or animate a character, the canvas shows the output of that code.
True or False? Game canvas focuses on user experience while coding canvas emphasizes code comprehension. Answer: TRUE!
🎯 Canvas in Our Mini Golf Game
In our mini golf game, the canvas will display multiple important elements:
✅ Golf Course
The playing field with obstacles, walls, and the hole
✅ Ball Position
The golf ball sprite that players control
✅ Score & UI Elements
Text showing strokes, level, and other game info
❌ Source Code
This is NOT displayed on the game canvas—only visual elements!
📊 Coordinate System
A game canvas uses a 2D coordinate system (x, y) for positioning elements. Understanding this is crucial for placing sprites exactly where you want them!
- Origin (0, 0): Top-left corner
- X-axis: Horizontal position (increases to the right)
- Y-axis: Vertical position (increases downward)
- Not 3D or 4D: Games use 2D coordinates for positioning
🎨 Canvas Features & Design
The canvas uses pixel-based drawing (not vector-based, text-based, or audio-based). Every element is drawn using pixels on your screen.
Essential Features for Good Game Canvas Design:
✅ Clear Visual Hierarchy
This is the most essential feature! Players should easily understand what's important on screen. Important elements (like the ball or goal) should stand out, while background elements should be less prominent.
Not essential: Complex animations only, maximum number of colors, or smallest possible size. Good design prioritizes clarity and user experience!
🎲 Interactive Gameplay Elements
What is Collision Detection?
Collision detection determines when objects interact with each other. In our mini golf game, collision detection tells us when:
- The ball hits a wall
- The ball falls into the hole
- The ball touches an obstacle
Canvas as an Interactive Playground
For students, the game canvas serves as an interactive playground where you can experiment, create, and see your code come to life! It's not a text editor, file storage system, or database—it's your creative space for building games.
🔧 Canvas and Color - Hands-On Task Activities
Let's set up your first canvas! Follow these step-by-step instructions:
🎨 Two Ways to Add Colors
There are multiple ways to add colors to your JavaScript programs. Here are the two easiest methods:
| Method | Example | When to Use |
|---|---|---|
| 1. Common Color Name | background("red") |
Quick and easy for basic colors |
| 2. HEX Color Code | background("#fc1303") |
Choose from infinite color options! |
💡 Pro Tip: Finding HEX Colors
Using HEX codes allows you to choose from infinite colors! Just Google search "hex color picker" to bring up a variety of options, choose your desired color on the picker, then copy the HEX code into your background command.
📝 Complete Canvas Setup Code
Click the button to see different background colors!
📏 Canvas Width and Height Variables
The p5play library has special built-in variables that automatically store the width and height of the canvas once it is set up. These variables are named width and height.
width/2 and height/2 instead of hard-coding numbers!
🖨️ Your Task: Print Width and Height
Using console.log(), print out the values of the width and height variables at the end of the setup() function (after you create your new canvas).
✅ Task Checklist:
- ✓ Create a canvas with size 450 x 450 pixels
- ✓ Set the background to your favorite color (name or HEX code)
- ✓ Use console.log() to print the width variable
- ✓ Use console.log() to print the height variable
- ✓ Test different canvas sizes to see what happens
- ✓ Return to 450x450 before submitting
🎯 Expected Console Output:
450
// Or with labels:
Canvas width: 450
Canvas height: 450
📹 Required Video Resource
Watch with headphones and take personal notes!
Video Link: Canvas Setup Tutorial
Remember to present your completed notes to your teacher for daily grading points!