If you are searching for 9.7.4 Leash CodeHS Answers JavaScript Solution and Code Explanation, you are probably working on a programming exercise involving interactive graphics, mouse movements, and drawing lines. This exercise helps students understand how JavaScript can control objects on a graphical canvas and make them respond to user input.
The Leash exercise appears in Lesson 9.7, Drawing Lines, in CodeHS’s Introduction to Computer Science in JavaScript (Golden) course. The activity combines several important programming concepts, including coordinates, graphical objects, functions, and mouse events.
The main idea behind the commonly documented version is simple: create a ball and connect it to a fixed point using a line. When the mouse moves, the ball follows its position while the line stretches to maintain the connection. This guide explains the programming logic, provides a reference JavaScript implementation, and covers common errors that can make the exercise difficult for beginners.
What Is 9.7.4 Leash in CodeHS?
The 9.7.4 Leash exercise is a JavaScript graphics activity available through CodeHS. It is part of a lesson that introduces students to drawing and manipulating lines on a computer screen.
The exercise gets its name from the visual relationship between a moving object and a fixed point. Imagine holding a ball attached to a string. When you move the ball, the string changes direction and length while remaining connected to its original position.
The programming task uses a similar concept. Students create a circle, draw a line, and use mouse events to update their positions.
The exercise helps develop several practical skills:
- Creating and displaying graphical objects.
- Understanding X and Y coordinates.
- Using JavaScript variables and functions.
- Responding to mouse movement events.
- Updating existing graphics instead of repeatedly creating new ones.
These skills are useful in interactive websites, browser-based games, animations, and other graphical applications.
9.7.4 Leash CodeHS JavaScript Solution
The following reference implementation demonstrates the commonly described mouse-following version of the Leash exercise.
JavaScript Code
var BALL_RADIUS = 30;
var ball;
var leash;
function start() {
var centerX = getWidth() / 2;
var centerY = getHeight() / 2;
leash = new Line(centerX, centerY, centerX, centerY);
add(leash);
ball = new Circle(BALL_RADIUS);
ball.setPosition(centerX, centerY);
ball.setColor(Color.yellow);
add(ball);
mouseMoveMethod(moveLeash);
}
function moveLeash(e) {
var mouseX = e.getX();
var mouseY = e.getY();
leash.setEndpoint(mouseX, mouseY);
ball.setPosition(mouseX, mouseY);
}
This example creates a yellow circle and a line that begins at the center of the canvas. When the user moves the mouse, the circle follows the pointer, and the line updates its endpoint.
Important: CodeHS assignments can differ between course versions. Compare this example with the exact instructions in your assigned exercise before submitting it.
Understanding the JavaScript Code Step by Step
Understanding how the program works is more useful than simply copying the code. Each section performs a specific task that contributes to the final animation.
1. Setting the Ball Radius
var BALL_RADIUS = 30;
This statement creates a variable that stores the radius of the circle.
The value 30 determines the size of the ball. A larger number creates a bigger circle, while a smaller number creates a smaller one.
Using a named constant makes the program easier to adjust because you can change the ball’s size without searching through multiple statements.
2. Declaring the Graphics Variables
var ball;
var leash;
These variables store references to the two graphical objects.
The ball variable represents the circle, while leash represents the connecting line.
They are declared outside the functions so that both the startup function and the mouse movement function can access them.
This is important because the program must update the same objects whenever the mouse moves.
3. Creating the Start Function
function start() {
var centerX = getWidth() / 2;
var centerY = getHeight() / 2;
}
The start() function initializes the program.
The getWidth() and getHeight() methods retrieve the dimensions of the graphics canvas. Dividing each value by two calculates its center.
This gives the program a reliable starting position without requiring fixed screen coordinates.
4. Drawing the Leash Line
leash = new Line(centerX, centerY, centerX, centerY);
add(leash);
The Line() constructor creates a line using four coordinate values:
- First value: starting X coordinate.
- Second value: starting Y coordinate.
- Third value: ending X coordinate.
- Fourth value: ending Y coordinate.
Initially, both endpoints use the center coordinates. This means the line starts with zero visible length.
The add() function places the line on the canvas so the user can see it.
5. Creating the Ball
ball = new Circle(BALL_RADIUS);
ball.setPosition(centerX, centerY);
ball.setColor(Color.yellow);
add(ball);
This section creates the circle that follows the mouse.
The Circle() constructor uses the radius stored in the constant. The setPosition() method places the circle at the center of the screen.
The setColor() method changes its appearance to yellow, and add() displays it on the canvas.
Adding the line before the circle also helps the ball appear above the connecting line.
6. Registering the Mouse Movement Event
mouseMoveMethod(moveLeash);
This is one of the most important statements in the entire program.
The mouseMoveMethod() function tells CodeHS to execute another function whenever the mouse moves over the graphics canvas.
In this example, the registered function is called moveLeash.
Without this event registration, the program would create the graphics but would not respond to mouse movement.
7. Updating the Ball and Line
function moveLeash(e) {
var mouseX = e.getX();
var mouseY = e.getY();
leash.setEndpoint(mouseX, mouseY);
ball.setPosition(mouseX, mouseY);
}
The event parameter e contains information about the mouse event.
The getX() method retrieves the horizontal mouse position, while getY() retrieves its vertical position.
The program then uses these coordinates in two updates:
leash.setEndpoint(mouseX, mouseY)moves the line’s endpoint.ball.setPosition(mouseX, mouseY)moves the circle to the same location.
Because both objects use identical coordinates, the line remains connected to the moving ball.
Important JavaScript Concepts Used in the Leash Exercise
The exercise introduces several programming techniques that are useful beyond this particular assignment.
| JavaScript Concept | Purpose in the Exercise |
|---|---|
| Variables | Store the ball and line objects |
| Functions | Organize the program’s instructions |
| Circle() | Create the moving ball |
| Line() | Draw the connecting leash |
| getWidth() | Find the canvas width |
| getHeight() | Find the canvas height |
| mouseMoveMethod() | Respond to mouse movement |
| getX() and getY() | Read mouse coordinates |
| setPosition() | Change the ball’s location |
| setEndpoint() | Update the line’s endpoint |
Understanding these methods makes it easier to approach other CodeHS graphics assignments.
Common Problems With 9.7.4 Leash CodeHS
Even a short JavaScript program can produce unexpected results when a variable, event handler, or coordinate is incorrect. Checking individual sections is usually faster than rewriting the entire solution.
The Ball Does Not Follow the Mouse
Check whether mouseMoveMethod(moveLeash) is registered correctly. Also confirm that the moveLeash() function contains the correct coordinate updates.
The Line Does Not Connect to the Ball
Make sure the line endpoint and circle position use the same X and Y values. Different coordinates can create a visible gap between the two objects.
The Program Shows an Undefined Variable Error
Check where ball and leash are declared. Both variables should be accessible inside the event handler, and the graphics objects must be initialized before the handler attempts to update them.
The Line Starts From the Wrong Position
Review the four coordinates used in the Line() constructor. The first two values establish the starting point, which should remain fixed in the commonly described version.
Multiple Circles Appear on the Screen
Avoid creating a new circle inside the mouse movement function. Create the ball once in start() and update its position whenever the mouse moves.
How to Test Your CodeHS Leash Solution
Testing is an important part of programming because a solution that looks correct in the editor may not behave as expected when executed.
Use these five checks before considering your program complete:
- Check the starting position: Confirm that the ball and line begin at the intended canvas location.
- Move the mouse slowly: Observe whether the circle follows the pointer smoothly.
- Move in different directions: Test horizontal, vertical, and diagonal movements.
- Check the line connection: Make sure the line remains attached to the circle.
- Review the console: Look for JavaScript errors, undefined variables, or incorrect method names.
These simple tests can reveal problems before you submit the assignment.
Why the Leash Exercise Is Useful for Learning JavaScript
The Leash activity is more than a basic drawing task. It introduces the relationship between user interaction and program behavior.
For example, mouse events are widely used in interactive applications. A game may use mouse coordinates to move a character, while a drawing application uses them to position a brush.
Similarly, lines and circles are common elements in visual programming. Learning to create and update them helps students understand how graphical applications operate.
Another important lesson is object reuse. Instead of creating a new ball whenever the mouse moves, the program updates the existing circle. This keeps the code organized and avoids unnecessary graphical objects.
Students who understand these ideas will find it easier to work with animations, simple games, and more advanced JavaScript projects.
Final Thoughts
The 9.7.4 Leash CodeHS Answers JavaScript Solution and Code Explanation exercise provides a practical introduction to interactive graphics and event-driven programming. Its main concepts include creating circles and lines, calculating canvas coordinates, registering mouse events, and updating existing graphical objects.
The most important part of the solution is keeping the ball’s position and the line’s endpoint synchronized. Once you understand how mouseMoveMethod(), setPosition(), and setEndpoint() work together, the exercise becomes much easier to follow.
Rather than relying entirely on copied code, take time to understand each function and test the program with different mouse movements. This approach builds stronger JavaScript skills and prepares you for more challenging CodeHS assignments.
Frequently Asked Questions
1. What is 9.7.4 Leash in CodeHS?
It is a JavaScript graphics exercise in Lesson 9.7, Drawing Lines, in the Introduction to Computer Science in JavaScript course.
2. Which programming language does the Leash exercise use?
The exercise discussed in this guide uses JavaScript and the CodeHS graphics library.
3. What does mouseMoveMethod() do?
It registers a function that runs when the mouse moves over the graphics canvas.
4. Why does the line follow the ball?
The program updates the line’s endpoint using the same mouse coordinates used to position the circle.
5. Can I use the same solution in every CodeHS course?
Not necessarily. Course versions and assignment requirements can differ, so always check the instructions provided in your own CodeHS account.

1 thought on “9.7.4 Leash CodeHS Answers JavaScript Solution and Code Explanation”