The 9.7.4 Leash exercise is a small JavaScript graphics project that demonstrates how objects can respond to mouse movement. Commonly associated with CodeHS’s introductory JavaScript graphics lessons, the activity involves a circle that follows the cursor while a line connects it to a fixed point on the screen.
The idea is straightforward, but the programming concepts behind it are useful. The exercise brings together mouse events, coordinate positions, graphic objects, and line endpoints to create an interactive visual effect.
For someone learning JavaScript, 9.7.4 Leash offers a practical way to understand how a program can respond to user input instead of displaying a static image. It also introduces a useful distinction between moving an entire object and changing only one part of it.
What Is 9.7.4 Leash?
9.7.4 Leash refers to a JavaScript graphics exercise in which a moving circle remains visually connected to a fixed point through a line.
The word “leash” describes the relationship between the two elements. One end of the line stays attached to an anchor point, while the other follows the circle as it moves around the canvas.
In the commonly described CodeHS version, the mouse controls the circle’s position. Moving the cursor changes the circle’s location and updates the line’s endpoint at the same time.
This is not a special JavaScript feature or a separate programming language. It is a visual demonstration built with ordinary JavaScript instructions and the CodeHS Graphics Library.
The exercise is useful because it turns abstract programming ideas into something visible. Instead of simply reading about coordinates or event handling, learners can observe how their instructions affect objects on a screen.
How the JavaScript Cursor Tracking Demo Works
The demonstration depends on three connected elements: an anchor point, a line, and a moving circle.
Each element has a different purpose.
The Fixed Anchor Point
The anchor point provides a permanent starting position for the line. In the commonly described exercise, it sits at the center of the graphics canvas.
Its coordinates are calculated using the canvas width and height. Dividing each dimension by two identifies the horizontal and vertical center.
The anchor does not follow the cursor. Keeping this position unchanged creates the visual effect of an object being attached to something stationary.
The Moving Circle
The circle represents the object being controlled by the mouse.
It begins near the anchor point and changes position whenever the cursor moves. The program updates its center using the mouse’s horizontal and vertical coordinates.
This is where the circle’s position becomes connected to user input. The graphic is not moving independently; its location depends on the latest mouse event.
The Connecting Line
The line visually connects the anchor to the moving circle.
A line has two endpoints. One identifies where the line begins, and the other identifies where it ends. For the leash effect, the starting endpoint remains fixed while the ending endpoint changes with the cursor.
Both the circle and the moving endpoint must receive the same coordinates. Otherwise, the line may appear disconnected from the circle.
The JavaScript Concepts Behind 9.7.4 Leash
Although the finished animation looks simple, it introduces several important programming concepts.
1. Mouse Event Handling
JavaScript programs can respond to different mouse actions, including movement, clicking, and dragging.
CodeHS provides the mouseMoveMethod() function for registering a callback that responds when the pointer moves across the graphics canvas.
The callback receives an event object containing information about the mouse position.
This approach is known as event-driven programming. Rather than repeatedly checking the cursor position without a specific reason, the program responds when the relevant event occurs.
2. Reading Mouse Coordinates
The mouse event provides two important values: the X coordinate and the Y coordinate.
The X coordinate represents the horizontal position, while the Y coordinate represents the vertical position.
CodeHS uses getX() and getY() to retrieve these values from the event object.
Together, they identify the cursor’s position on the canvas. The program can then use those coordinates to update the circle and line.
3. Creating Graphics Objects
The CodeHS Graphics Library provides constructors for creating visual objects.
A Circle object represents the moving ball, while a Line object represents the connection between the ball and its anchor.
These objects can be created once and added to the canvas. Their properties can then be updated as the program runs.
This is an important programming habit. Reusing an existing object is generally more appropriate than creating a completely new circle every time the mouse moves.
4. Updating Positions and Endpoints
Two methods are particularly important to the leash effect.
- setPosition() changes the position of a graphic object, such as the center of a circle.
- setEndpoint() changes the ending coordinates of a line without requiring its starting point to move.
The distinction matters. Moving the circle and updating the line’s endpoint are separate operations, even though both use the same mouse coordinates.
5. Understanding Object Relationships
The circle and line are separate graphics objects, but their positions are coordinated.
This demonstrates a common idea in interactive programming: multiple objects can work together to create one visual effect.
The circle responds to the cursor, and the line responds to the same event. Their shared coordinates keep them visually connected.
What Happens When You Move the Mouse?
The interaction follows a simple sequence.
- The program creates the anchor point, line, and circle.
- The initial line and circle are positioned at the center of the canvas.
- The program registers a mouse movement event.
- The user moves the cursor.
- The event provides the latest X and Y coordinates.
- The circle moves to those coordinates.
- The line’s endpoint updates to match the circle’s position.
These operations happen whenever another mouse movement event is received.
The result is a continuously changing visual connection. The anchor remains stationary while the circle follows the pointer.
Common Problems in the 9.7.4 Leash Exercise
A program can run without displaying the expected behavior. Understanding common mistakes makes debugging easier.
| Common problem | Possible reason | What to check |
|---|---|---|
| Circle does not move | Mouse event is not registered correctly | Check the event handler |
| Line stays in one position | Its endpoint is not updated | Review setEndpoint() |
| Line moves away from its anchor | Both endpoints are being changed | Keep the starting coordinates fixed |
| Multiple circles appear | New circles are created during mouse movement | Reuse the existing Circle object |
| Line and circle appear disconnected | They receive different coordinates | Use the same X and Y values |
| Graphics are missing | Objects were not added to the canvas | Check the add() calls |
One particularly important issue is the difference between move() and setEndpoint().
The move() method shifts a line as a whole. The leash effect requires the starting point to stay in place, so changing only the endpoint is the more appropriate approach.
Another common mistake is registering the mouse handler incorrectly. CodeHS documentation explains that the callback function name should be passed without parentheses when registering a mouse event.
Why Is the 9.7.4 Leash Exercise Useful for Beginners?
The value of this exercise goes beyond creating a moving circle.
It introduces practical skills that appear in many interactive applications.
Understanding coordinates: Learners see how horizontal and vertical positions control objects on a screen.
Working with events: The exercise demonstrates how a program reacts to mouse movement instead of following a fixed sequence of instructions.
Managing graphic objects: Creating an object once and updating it later helps learners understand object references and reusable graphics.
Debugging: A disconnected line or incorrectly positioned circle provides a visible clue that something in the program needs attention.
Building interactive projects: The same general ideas can be applied to cursor-following animations, drawing applications, simple games, and other graphics projects.
These concepts provide a useful foundation for more advanced JavaScript work.
Is 9.7.4 Leash a JavaScript Feature?
No. The term 9.7.4 Leash refers to an educational exercise, not a built-in JavaScript command or browser feature.
JavaScript provides the programming language, while CodeHS supplies a graphics environment with functions and methods for creating and controlling objects.
The cursor-following behavior comes from combining these features. Mouse events provide the input, coordinates describe the pointer’s location, and graphics methods update the displayed objects.
Understanding this distinction helps avoid confusion when moving from CodeHS exercises to regular JavaScript projects.
Final Thoughts
The 9.7.4 Leash exercise demonstrates how a few JavaScript graphics operations can produce an interactive result. A fixed anchor, a moving circle, and a line with an adjustable endpoint work together to create the cursor-tracking effect.
Its main learning value is the connection between user input and visual updates. Once learners understand how mouse coordinates control the circle and line, they have a clearer foundation for building more interactive JavaScript projects.
Frequently Asked Questions
What does 9.7.4 Leash mean?
It refers to a JavaScript graphics exercise in which a circle follows the mouse while a line connects it to a fixed anchor point.
What is the purpose of the leash line?
The line visually connects the moving circle to a stationary point. Its starting position remains fixed while its endpoint follows the circle.
Which JavaScript method handles mouse movement in CodeHS?
CodeHS provides mouseMoveMethod() to register a function that responds to mouse movement events.
What does setEndpoint() do?
The setEndpoint() method changes the ending coordinates of a line. It is useful when one end needs to move while the other remains stationary.
Why should the circle be created only once?
Creating a new circle during every mouse movement can produce multiple objects. Reusing the original circle allows the program to update its position without adding unnecessary graphics.
Is 9.7.4 Leash suitable for JavaScript beginners?
Yes. The exercise introduces mouse events, coordinates, graphics objects, and basic event-driven programming through a relatively simple visual example.