Technology

9.7.4 Leash CodeHS Answers: How the Animation Works

Published

on

The CodeHS Leash exercise demonstrates how to connect a moving circle to a fixed point using a line. As the mouse moves across the graphics area, the circle follows it while the line adjusts its direction and length.

The effect depends on a straightforward relationship: the circle’s center and the moving end of the line use the same coordinates.

Students searching for “9.7.4 Leash CodeHS answers” often need help connecting three ideas: creating graphics objects, responding to mouse movement, and updating objects that already exist.

CodeHS’s official course outline confirms that 9.7.4 Leash appears in the Drawing Lines lesson of its Introduction to Computer Science in JavaScript (Golden) course.

This guide explains the mouse-following effect with an original example based on documented CodeHS methods. It is an instructional implementation, not an official answer key or a claim that the code passes every classroom version’s checks.

What Is the CodeHS Leash Exercise?

Leash brings together graphics and user interaction. In the version explained here, a line has one stationary endpoint while its other endpoint follows a circle controlled by the mouse.

The program needs two visible objects:

  • A circle representing the moving ball.
  • A line representing the leash.

It also needs a function that responds to mouse movement.

The lesson’s position helps explain its purpose. In the Golden course outline, mouse-movement lessons come before Drawing Lines and Leash. Students are therefore combining concepts introduced in nearby activities.

However, the number is not universal. Another official CodeHS course lists Leash as 27.6.4. Check the title and instructions in your own course rather than assuming every search result describes identical requirements.

Preserve any supplied constants, required names, colors, or starting positions when adapting an example.

How Does the Leash Animation Work?

The program separates setup from interaction.

During setup, it creates the graphics objects and places them on the canvas. Afterward, a mouse-movement handler updates their coordinates.

Understanding the Fixed and Moving Points

For this example, the fixed point is the canvas center:

var centerX = getWidth() / 2;
var centerY = getHeight() / 2;

These calculations avoid assuming a particular canvas size.

Suppose the canvas measures 400 pixels wide and 300 pixels high. Its center is (200, 150).

When the mouse reaches (320, 90), the intended arrangement is:

Part Coordinates
Fixed line start (200, 150)
Moving line endpoint (320, 90)
Circle center (320, 90)

This keeps the moving end of the line aligned with the circle’s center.

The Mouse-Movement Handler

CodeHS provides mouseMoveMethod() to register a callback for mouse-movement events. The callback receives an event object, and the documentation demonstrates reading its coordinates with e.getX() and e.getY().

A callback is a function passed to another function for that system to execute. Here, your program supplies the handler, and CodeHS calls it when the relevant event occurs.

That means the program does not need a timer to produce this particular effect. Movement comes from incoming mouse events.

An Explained JavaScript Example

The following example uses the CodeHS JavaScript Graphics environment. Its graphics functions are supplied by CodeHS and will not work in a plain JavaScript console without the corresponding library.

The radius and colors are illustrative. Keep your assignment’s supplied values where they differ.

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
    );
    leash.setColor(Color.black);
    add(leash);

    ball = new Circle(BALL_RADIUS);
    ball.setColor(Color.blue);
    ball.setPosition(centerX, centerY);
    add(ball);

    mouseMoveMethod(updateLeash);
}

function updateLeash(e) {
    var mouseX = e.getX();
    var mouseY = e.getY();

    ball.setPosition(mouseX, mouseY);
    leash.setEndpoint(mouseX, mouseY);
}

Creating the Objects Once

The setup function creates one line and one circle.

Initially, both endpoints of the line occupy the center. This gives it zero length, so it may not be visible until the mouse moves.

The circle also begins at the center. CodeHS documents a circle’s position as its center, which is why no radius subtraction is needed.

The objects are created during setup rather than during every movement event. Each subsequent update changes the existing objects.

Making Both Objects Follow the Mouse

Inside updateLeash(), the event supplies a horizontal and vertical position.

The circle receives those coordinates through setPosition(). The line receives the same coordinates through setEndpoint().

CodeHS distinguishes the two line methods: setEndpoint() changes the ending point, while the line’s setPosition() changes its starting point. Updating the endpoint preserves the fixed anchor used in this example.

Sharing the Object References

The declarations for ball and leash appear outside both functions. This lets the setup function assign the objects and the event handler access them afterward.

JavaScript scope determines where a variable can be referenced. A variable declared only inside one function is not directly available to a separate function outside it.

Shared outer declarations are one beginner-friendly arrangement. Nested handlers and closures can also work, but this example keeps the structure explicit.

Common Leash Errors and Their Fixes

A short graphics program can fail because of one misplaced declaration or an incorrectly registered function. Diagnose the behavior before changing several lines at once.

Nothing Happens When the Mouse Moves

Check the registration statement:

mouseMoveMethod(updateLeash);

The function name is passed without calling it.

Writing updateLeash() in that position executes the function immediately. In this example, that call also lacks the mouse-event argument the handler expects.

The distinction is between supplying a function for later use and executing it now. MDN’s callback explanation describes this function-passing relationship.

The Circle Moves, but the Line Stays Still

Check whether the handler updates both objects.

A successful circle update does not automatically move an unrelated line. Compare the two update statements and confirm they receive matching coordinate values.

Also check spelling carefully. setEndpoint must use the capitalization shown in the documentation.

The Program Cannot Find the Ball

Look for a declaration such as var ball inside start() when the handler is defined separately.

That creates a local variable. Moving the intended shared declaration outside the functions, as shown in the example, makes the reference available to both. Avoid accidentally declaring another local variable with the same name.

The Circle Is Offset From the Pointer

If the program subtracts BALL_RADIUS from both coordinates, remove that adjustment when the goal is to center the ball on the pointer.

A circle’s position already refers to its center. Subtracting its radius shifts it away from the intended location.

The Canvas Fills With Extra Shapes

Inspect the movement handler for new Circle, new Line, or repeated add() calls.

In this implementation, those belong in setup. The handler should update the two existing objects rather than leave additional objects behind.

How to Check the Animation

Test the program against observable behavior instead of judging it from one movement.

First, move the pointer slowly around the canvas. The circle should follow without leaving a trail.

Next, move it to several widely separated positions. The line’s fixed end should remain stationary while its moving end stays aligned with the circle.

Then stop moving. This implementation should remain in its last position because it has no autonomous motion.

Move close to the edges as well. A circle centered near a boundary may extend partly outside the canvas. That follows from its radius; it does not necessarily indicate an incorrect center position.

Finally, compare the result with the actual assignment instructions. A visually similar animation can still differ in required size, appearance, initialization, or code structure.

These checks evaluate the example’s intended behavior. They do not establish a particular autograder result.

Conclusion

The Leash animation becomes easier to understand when you separate object creation from event handling. Setup creates the shapes; mouse events update their positions.

The key is to keep the line’s starting point fixed while updating its endpoint with the circle’s current coordinates. Understanding that relationship makes debugging more systematic and provides a foundation for other interactive graphics programs.

Frequently Asked Questions

What is 9.7.4 Leash in CodeHS?

It is an exercise listed under Drawing Lines in the Golden JavaScript course. This guide explains a mouse-following implementation using a circle and a line.

Does the Leash animation require a timer?

The implementation above does not. Mouse-movement events trigger its updates.

Why does the line stay attached to the circle?

Both moving positions receive the same coordinates, so the line endpoint and circle center remain aligned.

Why is the line invisible at the beginning?

In this example, it starts with identical endpoint coordinates, giving it zero length. Moving the mouse separates those points.

Will this code pass every Leash assignment?

That has not been verified. Follow your course’s instructions and preserve any required starter-code details.

Leave a Reply

Your email address will not be published. Required fields are marked *

Trending

Exit mobile version