Scratch3PenBlocks

Scratch3PenBlocks

Host for the Pen-related blocks in Scratch 3.0

Constructor

new Scratch3PenBlocks(runtime)

Parameters:
Name Type Description
runtime Runtime

the runtime instantiating this block package.

Members

(static) DEFAULT_PEN_STATE :PenState

The default pen state, to be used when a target has no existing pen state.

Type:

(static) PEN_SIZE_RANGE :Object

The minimum and maximum allowed pen size. The maximum is twice the diagonal of the stage, so that even an off-stage sprite can fill it.

Type:
  • Object

(static) STATE_KEY :string

The key to load & store a target's pen-related state.

Type:
  • string

(private) _penDrawableId :int

The ID of the renderer Drawable corresponding to the pen layer.

Type:
  • int

(private) _penSkinId :int

The ID of the renderer Skin corresponding to the pen layer.

Type:
  • int

runtime :Runtime

The runtime instantiating this block package.

Type:

Methods

(private) _alphaToTransparency(alpha) → {number}

Convert an alpha value to a pen transparency value. Alpha ranges from 0 to 1, where 0 is transparent and 1 is opaque. Transparency ranges from 0 to 100, where 0 is opaque and 100 is transparent.

Parameters:
Name Type Description
alpha number

the input alpha value.

Returns:

the transparency value.

Type
number

(private) _clampColorParam(value) → {number}

Clamp a pen color parameter to the range (0,100).

Parameters:
Name Type Description
value number

the value to be clamped.

Returns:

the clamped value.

Type
number

(private) _clampPenSize(requestedSize) → {number}

Clamp a pen size value to the range allowed by the pen.

Parameters:
Name Type Description
requestedSize number

the requested pen size.

Returns:

the clamped size.

Type
number

(private) _getPenLayerID() → {int}

Retrieve the ID of the renderer "Skin" corresponding to the pen layer. If the pen Skin doesn't yet exist, create it.

Returns:

the Skin ID of the pen layer, or -1 on failure.

Type
int

(private) _getPenState(target) → {PenState}

Parameters:
Name Type Description
target Target

collect pen state for this target. Probably, but not necessarily, a RenderedTarget.

Returns:

the mutable pen state associated with that target. This will be created if necessary.

Type
PenState

(private) _initColorParam() → {array}

Initialize color parameters menu with localized strings

Returns:

of the localized text and values for each menu element

Type
array

(private) _legacyUpdatePenColor(penState)

Update the pen state's color from its hue & shade values, Scratch 2.0 style.

Parameters:
Name Type Description
penState object

update the HSV & RGB values in this pen state from its hue & shade values.

(private) _onTargetCreated(newTarget, sourceTargetopt)

When a pen-using Target is cloned, clone the pen state.

Parameters:
Name Type Attributes Description
newTarget Target

the newly created target.

sourceTarget Target <optional>

the target used as a source for the new clone, if any.

Listens to Events:

(private) _onTargetMoved(target, oldX, oldY, isForce)

Handle a target which has moved. This only fires when the pen is down.

Parameters:
Name Type Description
target RenderedTarget

the target which has moved.

oldX number

the previous X position.

oldY number

the previous Y position.

isForce boolean

whether the movement was forced.

(private) _setOrChangeColorParam(param, value, penState, change)

Set or change a single color parameter on the pen state, and update the pen color.

Parameters:
Name Type Description
param ColorParam

the name of the color parameter to set or change.

value number

the value to set or change the param by.

penState PenState

the pen state to update.

change boolean

if true change param by value, if false set param to value.

(private) _transparencyToAlpha(transparency) → {number}

Convert a pen transparency value to an alpha value. Alpha ranges from 0 to 1, where 0 is transparent and 1 is opaque. Transparency ranges from 0 to 100, where 0 is opaque and 100 is transparent.

Parameters:
Name Type Description
transparency number

the input transparency value.

Returns:

the alpha value.

Type
number

(private) _updatePenColor(penState)

Update the cached color from the color, saturation, brightness and transparency values in the provided PenState object.

Parameters:
Name Type Description
penState PenState

the pen state to update.

(private) _wrapColor(value) → {number}

Wrap a color input into the range (0,100).

Parameters:
Name Type Description
value number

the value to be wrapped.

Returns:

the wrapped value.

Type
number

changePenColorParamBy(args, util)

Properties:
Name Type Description
COLOR_PARAM ColorParam

the name of the selected color parameter.

VALUE number

the amount to change the selected parameter by.

The "change pen {ColorParam} by {number}" block changes one of the pen's color parameters by a given amound.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.

changePenHueBy(args, util)

Properties:
Name Type Description
HUE number

the amount of desired hue change.

Scratch 2 "hue" param is equivelant to twice the new "color" param.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.

changePenShadeBy(args, util)

Properties:
Name Type Description
SHADE number

the amount of desired shade change.

Because "shade" cannot be backed out of hsv consistently, use the previously stored penState._shade to make the shade change.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.

changePenSizeBy(args, util)

Properties:
Name Type Description
SIZE number

the amount of desired size change.

The pen "change pen size by {number}" block changes the pen size by the given amount.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.

clear()

The pen "clear" block clears the pen layer's contents.

getInfo() → {object}

Returns:

metadata for this extension and its blocks.

Type
object

penDown(args, util)

The pen "pen down" block causes the target to leave pen trails on future motion.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.

penUp(args, util)

The pen "pen up" block stops the target from leaving pen trails.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.

setPenColorParamTo(args, util)

Properties:
Name Type Description
COLOR_PARAM ColorParam

the name of the selected color parameter.

VALUE number

the amount to set the selected parameter to.

The "set pen {ColorParam} to {number}" block sets one of the pen's color parameters to a given amound.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.

setPenColorToColor(args, util)

Properties:
Name Type Description
COLOR int

the color to set, expressed as a 24-bit RGB value (0xRRGGBB).

The pen "set pen color to {color}" block sets the pen to a particular RGB color. The transparency is reset to 0.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.

setPenHueToNumber(args, util)

Properties:
Name Type Description
HUE number

the amount to set the hue to.

Scratch 2 "hue" param is equivelant to twice the new "color" param.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.

setPenShadeToNumber(args, util)

Properties:
Name Type Description
SHADE number

the amount to set the shade to.

Use legacy "set shade" code to calculate RGB value for shade, then convert back to HSV and store those components. It is important to also track the given shade in penState._shade because it cannot be accurately backed out of the new HSV later.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.

setPenSizeTo(args, util)

Properties:
Name Type Description
SIZE number

the amount of desired size change.

The pen "set pen size to {number}" block sets the pen size to the given amount.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.

stamp(args, util)

The pen "stamp" block stamps the current drawable's image onto the pen layer.

Parameters:
Name Type Description
args object

the block arguments.

util object

utility object provided by the runtime.