Skip to content

The Mouse

Where the mouse is, whether it is over a shape, and shapes you can drag. Clicking shapes is on its own page: Clicks.


mouse.x, mouse.y and mouse.at

mouse.at

Where the mouse is on the drawing. mouse.x and mouse.y are numbers, and mouse.at is a place. Follow it with @ and a shape goes wherever the mouse goes:

draw circle dot at @mouse.at having radius 10, fill "gold"

When the mouse leaves the drawing, it keeps the last place it was. Before it has been on the drawing at all, it is in the middle of it.


if mouse is over ...

if mouse is over ball {
  ...
}

Checks whether the mouse is over a shape right now. A shape with parts counts all of its parts, and a hidden shape is never under the mouse. is not over checks that it isn't, and mouse is over the drawing checks whether the mouse is on the drawing at all.

draw circle ball at 400, 300 having radius 60, fill "gray"
forever {
  if mouse is over ball { ball.fill = "gold" } otherwise { ball.fill = "gray" }
  wait 0.02 seconds
}

when ... (any check)

when mouse is over ball {
  ...
}

when takes any check that is true or false, and runs its lines each time the check turns from false to true:

draw circle ball at 400, 300 having radius 60, fill "skyblue"
when mouse is over ball {
  turn ball by 45 in 0.2 seconds
}

The ball turns once each time the mouse moves onto it, not again while the mouse stays there. Move the mouse off and back on to turn it again. Any check works, like when score > 10 or when ball is touching wall:

number score = 0
draw text label at 20, 40 having content "Click the ball"
draw circle ball at 400, 300 having radius 40, fill "tomato"
when ball is clicked {
  score = score + 1
}
when score > 4 {
  label.content = "You win!"
}

A check that is already true when the drawing starts runs once at the start. To do something the whole time a check is true, use forever and if.


mouse is down

if mouse is down { ... }

Is yes while the mouse button is held, or a finger is on the drawing, wherever on the drawing it went down:

draw circle lamp at 200, 150 having radius 40, fill "gray"
forever {
  if mouse is down {
    lamp.fill = "gold"
  }
  otherwise {
    lamp.fill = "gray"
  }
  wait 0.05 seconds
}

mouse is not down is the opposite.


when ... is released

when ball is released {
  ...
}

Runs when a press that began on ball ends: after a drag, or a plain click. It runs when the button comes up (or the finger lifts), wherever the pointer is by then. A release by itself, from a press that began somewhere else, does nothing.

To drop a shape on something, check what it is touching when it is let go:

draw rectangle bin at 300, 100 having width 80, length 80, fill "lightgray"
draw circle ball at 60, 60 having radius 20, fill "tomato", draggable true
when ball is released {
  if ball is touching bin {
    ball.fill = "gold"
  }
  otherwise {
    ball.fill = "tomato"
  }
}

when any n in nodes is released { } works for every shape in a bunch, and for shapes added later. Like when any f in garden is clicked, it runs for the one that was let go, with n naming it.


when ... is double clicked

when ball is double clicked {
  ...
}

Runs on the second click when ball is clicked twice quickly (within about half a second). The first click still runs when ball is clicked, as every click does. A double tap with a finger is a double click too. Two clicks on different shapes, or clicks that are further apart in time, are not.

draw circle ball at 200, 150 having radius 50, fill "lightblue"
when ball is double clicked {
  ball.fill = "gold"
}

when any n in nodes is double clicked { } works for every shape in a bunch, like is clicked and is released. It runs for the one that was clicked, with n naming it.


draggable

draw circle ball at 200, 200 having radius 40, draggable true

A shape with draggable true can be dragged with the mouse, or with a finger on a tablet. A shape with parts moves as one piece, and so does whatever follows it with @:

draw circle ball at 200, 300 having radius 40, fill "tomato", draggable true
draw circle shadow at @ball.center having radius 10, fill "maroon"

A drag is a real move, so when ball touches goal runs when you drag the ball onto the goal:

draw circle ball at 150, 300 having radius 30, fill "tomato", draggable true
draw square goal at 550, 250 having size 100, fill "lightgreen"
when ball touches goal {
  goal.fill = "gold"
}

ball.draggable = false stops it being dragged, and true lets it be dragged again. A hidden shape can't be grabbed, and a part is dragged with its whole shape unless the part has draggable true of its own.


On a tablet

A finger clicks, double clicks (a double tap) and drags, like the mouse's button
mouse.x, mouse.at follow the finger, and keep the last place when it lifts
mouse is over is never true: a finger can't hover, so code that waits for the mouse to be over a shape doesn't run
One finger on a shape you can drag drags it; anywhere else it scrolls the page
Two fingers zoom the drawing in the playground