Scratch for Java

Effects

Effects change how a sprite looks without a new costume: a flash when it is hit, a ghost that fades away, a red tint for an angry enemy.

Colour, ghost and tint

Move the mouse left and right over the stage:

Method What it does
setColorEffect(50) Scratch's colour effect: shifts every colour. 200 is all the way round.
setTransparency(50) Scratch's ghost effect: 0 is solid, 100 is invisible.
setTint(255, 0, 0) keeps only the red of the costume. White (255, 255, 255) is no tint.

changeColorEffect and changeTransparency change by an amount, as in Scratch. The stage has the same methods.

Shaders

A shader is a small program that the graphics card runs for every pixel of a sprite or of the stage. It can blur, pixelate, glow or wobble. Shaders are written in GLSL, in a .frag file:

uniform vec2 pixels;
 
void main(void) {
  vec2 p = uv;
  p.x -= mod(p.x, 1.0 / pixels.x);
  p.y -= mod(p.y, 1.0 / pixels.y);
  color = texture(costume, p);
}

uv is the position in the costume, from 0 to 1. costume is its picture. color is what the pixel becomes. pixels is a value your Java code sets:

  • getShaders().add(name, file) adds a shader and switches it on.
  • shader.set(name, value) sets one of the shader's uniform values.
  • getShaders().deactivate(name) and activate(name) switch it off and on.

The shader example has seven more shaders to copy from.

Next

Effects

Create Shareable URL

Select Sections