Skip to content

Clicks

How a drawing does something when you click a shape. Things that happen over time (moving, waiting, forever, named animations) are on their own page: Animation, and so is the keyboard: Keys.


when ... is clicked

when name is clicked {
  ...
}
Canvas paper having width 400, length 300
draw circle balloon at 200, 150 having radius 40, fill "tomato"
when balloon is clicked {
  balloon.fill = "gold"
  grow balloon by 2 in 1 second
  shrink balloon by 2 in 1 second
  balloon.fill = "tomato"
}

Each click runs the lines inside the { }, one after another.

Rules:

  • The lines inside aren't part of the picture. The picture is drawn first, and then it waits for clicks.
  • Inside when, every line waits for its turn, like the lines of an animation: a change without a time happens at once when its turn comes.
  • when doesn't take time itself, so it doesn't start the animation, and a when in the program's own lines listens for clicks from the very start.
  • The shape has to be on the drawing: a shape that is made but never drawn or shown can't be clicked, and is an error.
  • clicked is the only word that goes there: when ball is pressed is an error.
  • otherwise goes with if, not with when.

Tutorial: Click to Make Things Happen.


Clicking the empty drawing

when the drawing is clicked {
  ...
}

Runs when the drawing is clicked (or tapped) where there is no shape that listens for the click. mouse.at is the place that was clicked, so a click can put something there:

Canvas field having width 400, length 300, fill "lightyellow"
when the drawing is clicked {
  draw circle at mouse.at having radius 12, fill "tomato"
}

A click on a shape with a when ... is clicked of its own goes to that shape, not to the drawing. A shape you can drag (draggable true) takes the press too. A click on a shape that does nothing goes through to the drawing.


Clicking again

Every click runs the lines inside when, to the end, even if the last click's lines are still going. Both go on at the same time.

Two clicks change different things both changes happen
Two clicks move, turn, grow or shrink the same shape by an amount the changes add up
A click changes something to a value (box.angle = 0 in 1 second) it starts from wherever the older changes had got to and ends on its value; a by change after it adds on top
Two different shapes are clicked they don't wait for each other

What a second click does is up to you. To stop the first run, or to ignore a click while something is going, use a named animation with if ... is playing, or a variable:

Canvas paper having width 400, length 300
draw rectangle fan at 150, 140 having width 100, length 20, fill "skyblue"
animation spin {
  forever {
    turn fan by 360 in 2 seconds
  }
}
when fan is clicked {
  if spin is playing {
    stop spin
  }
  otherwise {
    play spin
  }
}

A forever inside when starts another one on each click.


More than one when for a shape

Canvas paper having width 400, length 300
draw circle ball at 100, 150 having radius 30, fill "purple"
when ball is clicked {
  move ball by 200, 0 in 1 second
}
when ball is clicked {
  move ball by -200, 0 in 1 second
}

A click runs all of a shape's whens one after another, in the order they were written. The ball goes across, then comes back.

A when that is set up later, as the animation runs (see below), runs after the ones that were there from the start.


Shapes with parts

Clicking any part of a shape clicks the whole shape. A part can have its own when, using its full name:

Canvas paper having width 400, length 300
draw circle face at 200, 150 having radius 80, fill "gold" {
  circle eye1 at -30, -20 having radius 10, fill "black"
  circle eye2 at 30, -20 having radius 10, fill "black"
}
when face is clicked {
  turn face by 360 in 2 seconds
}
when face.eye1 is clicked {
  face.eye1.fill = "blue"
}
You click What runs
the face, or eye2 the face's when
eye1 eye1's when and the face's when, at the same time

A shape made from a recipe works the same way: when sam is clicked { }. Inside, change it through its settings, like sam.size = sam.size + 20. Its parts can't be changed from outside the recipe.


when inside repeat

Canvas paper having width 400, length 300
repeat 3 times {
  draw circle balloon at 100 * count, 200 having radius 30, fill "tomato"
  when balloon is clicked {
    move balloon by 0, -50 * count in 1 second
  }
}

Each time round sets up its own when, for the shape drawn that time round and with that time round's count. Each balloon floats up a different height.


when inside something that takes time

A when inside a forever, a repeat that takes time, or a named animation is set up when the animation gets there. Before that, clicks do nothing.

Canvas paper having width 400, length 300
draw circle egg at 200, 150 having radius 40, fill "white"
play animation hatching {
  egg.fill = "khaki" in 3 seconds
  when egg is clicked {
    egg.fill = "gold"
  }
}

Clicking the egg does nothing for 3 seconds. After that, a click turns it gold.

Rules:

  • A when is set up once for each when line and each shape: a forever going round again doesn't set up a second one.
  • A when in a forever or a repeat that takes time, for a shape drawn there, belongs to the shape drawn that time round, and remembers that time round's values, like count.

Drawing in a click

Canvas paper having width 400, length 300
draw rectangle grass at 0, 220 having width 400, length 80, fill "green"
flowers = 0
when grass is clicked {
  flowers = flowers + 1
  draw circle flower at 40 * flowers, 200 having radius 12, fill "pink" {
    circle at 0, 0 having radius 4, fill "gold"
  }
  grow flower by 1.5 in 0.5 seconds
}

A draw inside when draws a new shape on every click.

Rules:

  • Each click makes its own shape. Its name means the shape that click drew, so grow flower grows the new flower and leaves the others as they are.
  • The shape's properties, its parts' numbers and a line's or polygon's points can use values only that click knows, like flowers here, or where another shape is right then.
  • A shape made outside the when can't be drawn inside it, because a draw there makes a new shape every time. Make it outside without drawing it, and use show in the click.
  • A shape drawn in a click can't be used by name outside the when, and can't have a when of its own (that when would have to go inside the click's when).
  • Making a shape without drawing it (circle dot at 50, 50) can't go inside when yet.

Hidden shapes

A hidden shape can't be clicked: you can't click what nobody can see.

Canvas paper having width 400, length 300
draw rectangle button at 30, 230 having width 100, length 40, fill "green"
circle lamp at 250, 120 having radius 50, fill "gold"
when button is clicked {
  show lamp in 1 second
}
when lamp is clicked {
  hide lamp in 1 second
}

The lamp is made but not drawn, so it starts hidden, and clicking where it will be does nothing. Once the button shows it, clicking the lamp hides it again.

Rules:

  • A shape is hidden once it has faded all the way out. While it is fading, it can still be clicked.
  • The parts of a hidden shape are hidden too.

Variables and clicks

Canvas paper having width 400, length 300
draw rectangle fan at 150, 140 having width 100, length 20, fill "skyblue"
speed = 0
forever {
  turn fan by 90 * speed in 1 second
}
when fan is clicked {
  speed = speed + 1
}

Each click makes the fan faster.

Rules:

  • A variable changed in a click keeps its new value, ready for the next click.
  • A forever, an animation or another click that reads the variable sees what the clicks did.
  • A variable changed in a click has to be made at the top first, like speed = 0.

Tutorial: Remembering Things.


Where when can't go

Where What VeeLang says
inside another when ... is clicked 'when ... is clicked' can't go inside another 'when ... is clicked'.

Inside another when, use a variable instead: set ready = true in one, and check if ready { } in the other.

A when ... is clicked can also go inside a shape's { } or a recipe, for one of its parts: there, the part's own name is enough. Every face made from the recipe gets its own:

shape face having size 100 {
  circle head having radius size / 2, fill "gold"
  circle nose at 0, 5 having radius 8, fill "pink"
  when nose is clicked {
    nose.fill = "red"
  }
}
draw face at 120, 150
draw face at 300, 150
draw circle ball at 100, 100 having radius 30, fill "tomato"
when ball is clicked {
  when ball is clicked {
    ball.fill = "blue"
  }
}