Live UpdatesTrending: Wells Fargo $84M ESOP Settlement · Blue Cross Blue Shield payout window open
iPhone App Android App
Blog

Leash CodeHS Answers: Working JavaScript Code and How It Works

By Kevin Tookes · November 28, 2026

If you are hunting for leash codehs answers, the short version is that this exercise is a JavaScript graphics task. A circle follows your mouse pointer while a line stretches from a fixed point at the center of the canvas to the circle. The program needs one function that builds the two shapes and registers a mouse handler, plus one small function that updates both shapes whenever the mouse moves. The version below uses only methods listed in the official CodeHS JavaScript documentation.

javascript

var ball;
var line;

function start() {
    var centerX = getWidth() / 2;
    var centerY = getHeight() / 2;

    line = new Line(centerX, centerY, centerX, centerY);
    add(line);

    ball = new Circle(30);
    ball.setPosition(centerX, centerY);
    ball.setColor(Color.YELLOW);
    add(ball);

    mouseMoveMethod(leash);
}

function leash(e) {
    ball.setPosition(e.getX(), e.getY());
    line.setEndpoint(e.getX(), e.getY());
}

Run it and move the mouse across the canvas. The circle should track the pointer while one end of the line stays pinned to the middle. Guides usually list this exercise as 9.7.4 in the JavaScript graphics course, but lesson numbers shift between course versions, and the lesson pages sit behind a CodeHS login. The wording of your own instructions may therefore differ in small ways, such as the circle’s size, its color, or where the line is anchored. Check those details against your assignment before you submit.

What each part of the code does

The line is created first, and both of its ends get the same coordinates. A Line is built from a start point and an end point, so identical values give a leash of zero length sitting in the middle of the canvas. It only gets longer once the mouse moves and the end point is pulled away. The circle comes next: it gets a radius, is placed at the center, is colored, and is added to the canvas. Nothing is drawn until add() runs, which is the first thing to check whenever a shape seems to be missing.

The line mouseMoveMethod(leash) is the link between the mouse and the graphics. According to the CodeHS documentation, the function you register is called when the mouse event happens, and you pass its name without parentheses. That detail matters because writing leash() would run the function once, right away, before any mouse event exists to read from. The handler receives an event object, conventionally named e, and e.getX() and e.getY() return the pointer’s current coordinates.

Inside leash, two statements do all the work. One moves the circle to the pointer and the other drags the line’s free end to the same spot. The variables ball and line are declared at the top of the file, outside both functions, so start() can create them and leash() can reach the same objects later. If they were declared with var inside start(), the handler would not be able to see them.

Why setPosition and setEndpoint are not interchangeable

The official documentation describes the two methods differently for each shape. On a circle, setPosition(x, y) places the center of the circle at that point. On a line, setPosition(x1, y1) changes the starting point, and setEndpoint(x2, y2) changes the end point. The same line methods appear in both the older and the newer versions of the CodeHS docs, so this behavior is consistent across course versions.

This explains the two most common mix-ups. If you call line.setPosition() in the handler, the anchored end follows the mouse and the leash stops being a leash. And because a circle is positioned by its center, you do not need to subtract the radius from the mouse coordinates. That adjustment belongs to rectangles, whose position refers to the upper-left corner. Some tutorials apply a radius offset to circles anyway, which leaves the ball sitting a little up and to the left of the cursor.

Why your leash is not moving or stretching

Most broken attempts fail in one of a handful of ways, and each one leaves a recognizable symptom.

What you seeLikely causeFix
Nothing happens when the mouse movesmouseMoveMethod is missing, misspelled, or called with parentheses on the function nameRegister it once inside start() and pass only the name: mouseMoveMethod(leash)
The circle follows but the line stays at the centerOnly the circle is updated in the handlerAdd line.setEndpoint(e.getX(), e.getY())
The line moves but its anchored end drifts toosetPosition was used on the lineSwitch to setEndpoint
The circle sits slightly off the cursorA radius offset was subtractedPass the mouse coordinates straight to ball.setPosition
An error says ball or line is not definedThe variables were declared inside start()Declare them at the top of the file
An error says a method is not definedA capital letter is wrong, as in mousemovemethodJavaScript is case-sensitive, so match the spelling exactly

Do you need removeAll or a timer?

No. Many leash codehs answers online clear the canvas with removeAll() on every mouse move and then rebuild the circle and line. That works, but it is unnecessary here, and it creates a new problem: once everything is removed, you must re-add every shape or the screen goes blank. Because the program creates each shape once and only changes its coordinates afterward, the old drawing never has to be erased. Timers, which the CodeHS docs describe as a way to call a function repeatedly at a set interval, are also unnecessary, since the mouse event itself triggers each update.

Small changes that make it your own

A few harmless tweaks keep the logic identical while making the program look less like everyone else’s. The documentation lists setLineWidth() for lines, so a thicker leash is one extra statement. You can also change the circle’s radius when you create it, or pick another entry from the Color list for either shape. If your teacher has specified a color or size, follow that instead.

For a harder version, you could limit how far the circle can travel from the anchor. That means measuring the straight-line distance between the center and the mouse with the square root function that CodeHS documents under Math.sqrt, then stopping the update once the distance passes a maximum. I could not confirm that a maximum length is part of the standard exercise, so treat it as an optional challenge rather than a requirement.

A note on other versions you may find

A few sites describe a second form of this assignment built from Dog and Leash classes with no canvas at all. I found no confirmation of that version in the official CodeHS documentation, so it may belong to a different course or to the guides’ own inventions. If your assignment shows a graphics canvas and mentions mouse movement, the program above is the one that matches it.

Using the solution without just pasting it

Retyping the code line by line teaches more than pasting it, and it forces you to notice details like the missing parentheses on leash. Try breaking it on purpose: remove the setEndpoint line, or swap in line.setPosition, and watch what changes on screen. Being able to explain why each version behaves differently is what carries over to the next mouse-event exercises, and it is also what a teacher is likely to ask about if they have questions.

EXPLORE MORE: Schwab Corrente Settlement: No Payment for Customers, but a Compliance Program and an Open Appeal

Related Posts

Leave a Reply

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