If you’re working through the CodeHS JavaScript graphics unit, you’ve likely encountered exercise 9.7.4 Leash. This interactive exercise tests your understanding of mouse events and how to dynamically update objects on the canvas. Many students search for answers hoping for a quick copy-paste solution, but understanding the underlying concepts will help you succeed in future programming assignments.
What Is the 9.7.4 Leash Exercise?
The 9.7.4 Leash exercise is part of the CodeHS JavaScript curriculum that teaches graphics and animation concepts. In this exercise, you need to create a program where a “leash” (a line) follows a ball or pet object across the canvas. The leash typically starts from a fixed point (often the center of the canvas or the ball’s center) and extends to follow mouse movements .
This exercise tests several key programming concepts:
- Mouse event handling – Responding to mouse movements in real-time
- Canvas graphics – Drawing and updating lines and shapes
- Object properties – Accessing and modifying object positions
- Continuous updates – Creating smooth, responsive animations
Complete Solution for 9.7.4 Leash
Based on the exercise requirements, here’s a clean, working solution for the 9.7.4 Leash exercise :
function drawLeash(ball) {
// Get the current center position of the ball
const center = ball.getCenter();
// Create the end point for the leash (100 pixels below the ball)
const end = {
x: center.x,
y: center.y - 100
};
// Draw the line from ball center to the end point
drawLine(center, end);
}
// Set up mouse tracking to update the leash position
function start() {
// Get the ball object (assuming it's already created)
var ball = new Circle(30);
ball.setPosition(200, 200);
add(ball);
// Mouse move event handler
mouseMoveMethod(function(e) {
// Clear previous drawing
removeAll();
// Redraw ball at current mouse position
ball.setPosition(e.getX(), e.getY());
add(ball);
// Draw leash from ball to fixed point (example: canvas center)
var center = ball.getCenter();
var fixedPoint = {x: 250, y: 250}; // Canvas center
drawLine(center, fixedPoint);
});
}
How the Code Works: Step-by-Step Explanation
Understanding the drawLeash Function
The core function drawLeash(ball) demonstrates several important JavaScript concepts :
1. Getting the Ball’s Center
const center = ball.getCenter();
The getCenter() method returns an object containing the x and y coordinates of the ball’s current position. This is essential because the leash needs to attach to the ball wherever it moves.
2. Creating the End Point
const end = {
x: center.x,
y: center.y - 100
};
This creates a new point object that will serve as the other end of the leash. In this example, the end point is positioned 100 pixels above the ball (notice the y: center.y - 100). The subtraction moves upward because the canvas coordinate system starts at the top-left corner .
3. Drawing the Line
drawLine(center, end);
The drawLine() function takes two points and draws a line between them on the canvas. This creates the visual “leash” connecting the ball to the end point .
The Mouse Movement Logic
In the complete solution, the leash updates continuously as the mouse moves:
- Clear the canvas –
removeAll()removes previous drawings to prevent visual artifacts - Update ball position – Move the ball to the current mouse coordinates
- Redraw ball – Add the ball back at its new position
- Draw leash – Create a line from the ball to a fixed point (or to another dynamic point)
Key Concepts You Need to Understand
Mouse Events in JavaScript
The exercise relies heavily on mouse events. In CodeHS JavaScript, you’ll typically use:
mouseMoveMethod()– Triggers continuously as the mouse movesmouseClickMethod()– Triggers once per clickmouseDragMethod()– Triggers while clicking and moving
These event handlers allow your program to respond in real-time to user interaction.
Canvas Coordinate System
Understanding how coordinates work on the canvas is crucial:
- The top-left corner is (0, 0)
- X increases as you move right
- Y increases as you move down
This explains why y: center.y - 100 moves the leash upward – you’re decreasing the Y value.
Object References
The code passes the ball object to functions and accesses its properties. This demonstrates object-oriented thinking – the ball has methods like getCenter() that provide useful information about its current state.
Common Mistakes to Avoid
1. Forgetting to Clear Previous Drawings
If you don’t clear the canvas before each redraw, you’ll end up with multiple lines trailing behind the ball:
// WRONG - creates line trails
mouseMoveMethod(function(e) {
ball.setPosition(e.getX(), e.getY());
drawLine(ball.getCenter(), fixedPoint);
// Missing removeAll()!
});
// RIGHT - clean animation
mouseMoveMethod(function(e) {
removeAll(); // Clear previous frame
ball.setPosition(e.getX(), e.getY());
add(ball);
drawLine(ball.getCenter(), fixedPoint);
});
2. Misunderstanding the End Point
The exercise may specify different leash behaviors. Read carefully:
- Fixed leash – One end attached to ball, other end fixed in place
- Following leash – Both ends move (leash follows ball completely)
- Stretching leash – Leash length changes based on distance
3. Off-by-One Errors with Coordinates
Double-check your coordinate calculations. Remember that getCenter() returns the center point, not the top-left corner of the ball.
Variations and Extensions
Once you’ve mastered the basic leash, you can experiment with these variations :
Making the Leash Follow Smoothly
function drawLeash(ball) {
const center = ball.getCenter();
const end = { x: center.x, y: center.y - 100 };
function update() {
end.y = center.y - 100; // Update end point position
}
drawLine(center, end);
window.addEventListener("keydown", update);
}
Creating a Stretching Leash
function drawLeash(ball) {
const center = ball.getCenter();
const end = { x: center.x, y: center.y - 100 };
function update() {
end.length = ball.radius; // Leash length matches ball radius
}
drawLine(center, end);
window.addEventListener("keydown", update);
}
Changing Leash Color
function drawLeash(ball) {
const center = ball.getCenter();
const end = { x: center.x, y: center.y - 100 };
function update() {
fillStyle = ball.color; // Leash color matches ball color
}
drawLine(center, end, fillStyle);
window.addEventListener("keydown", update);
}
Frequently Asked Questions (FAQs)
Q: How can I make the leash follow the ball more smoothly?
A: Use the update() method to refresh the leash’s end point every frame. Attach an event listener that continuously updates the position rather than recalculating only on specific events .
Q: Why does my leash leave a trail behind the ball?
A: You’re likely missing the removeAll() function call before redrawing. Without clearing the canvas, previous lines remain visible, creating a trail effect.
Q: How do I attach the leash to the mouse instead of a fixed point?
A: Use the mouse coordinates directly from the event object:
mouseMoveMethod(function(e) {
var mouseX = e.getX();
var mouseY = e.getY();
drawLine(ball.getCenter(), {x: mouseX, y: mouseY});
});
Q: What’s the difference between mouseMoveMethod and mouseDragMethod?
A: mouseMoveMethod triggers whenever the mouse moves anywhere on the canvas. mouseDragMethod only triggers when the mouse moves while a button is pressed.
Q: Can I make the leash change length based on distance?
A: Yes! Calculate the distance between two points using the Pythagorean theorem and adjust the leash endpoint accordingly.
Summary
The 9.7.4 Leash exercise teaches fundamental concepts in interactive JavaScript graphics:
- Event-driven programming – Responding to user input
- Canvas manipulation – Drawing and updating visual elements
- Coordinate geometry – Working with the canvas coordinate system
- Object methods – Using built-in functions like
getCenter()
The key to success is understanding how to continuously update the canvas based on mouse position. The solution involves clearing previous frames, updating object positions, and redrawing the leash connection.

