Rewrote transparency block

Wrapping of the value is now handled by a new function, _wrapTransparency;
_updatePenColor now handles the transparency value;
DEFAULT_PEN_STATE now includes a transparency value (100).
This commit is contained in:
Ken 2017-09-01 14:11:09 +08:00 committed by GitHub
parent f61366643d
commit 3185eb27e1

View file

@ -55,6 +55,7 @@ class Scratch3PenBlocks {
penDown: false,
hue: 120,
shade: 50,
transparency: 100,
penAttributes: {
color4f: [0, 0, 1, 1],
diameter: 1
@ -183,11 +184,11 @@ class Scratch3PenBlocks {
penState.penAttributes.color4f[0] = rgb.r / 255.0;
penState.penAttributes.color4f[1] = rgb.g / 255.0;
penState.penAttributes.color4f[2] = rgb.b / 255.0;
penState.penAttributes.color4f[3] = 1;
penState.penAttributes.color4f[3] = penState.transparency / 100.0;
}
/**
* Wrap a pen hue or shade values to the range [0,200).
* Wrap a pen hue or shade values to the range (0,200).
* @param {number} value - the pen hue or shade value to the proper range.
* @returns {number} the wrapped value.
* @private
@ -198,6 +199,18 @@ class Scratch3PenBlocks {
return value;
}
/**
* Wrap a pen transparency value to the range (0, 100).
* @param {number} value - the pen transparency value to the proper range.
* @returns {number} the wrapped value.
* @private
*/
_wrapTransparency (value) {
value = value % 100;
if (value < 0) value += 100;
return value;
}
/**
* Retrieve the block primitives implemented by this package.
* @return {object.<string, Function>} Mapping of opcode to Function.
@ -382,8 +395,7 @@ class Scratch3PenBlocks {
*/
changePenTransparencyBy (args, util) {
const penState = this._getPenState(util.target);
const TRANSPARENCY = args.TRANSPARENCY / 100;
penState.penAttributes.color4f[3] = penState.penAttributes.color4f[3] + TRANSPARENCY;
penState.transparency = this._wrapTransparency(penState.transparency + Cast.toNumber(args.TRANSPARENCY));
this._updatePenColor(penState);
}
@ -395,8 +407,7 @@ class Scratch3PenBlocks {
*/
setPenTransparencyTo (args, util) {
const penState = this._getPenState(util.target);
const TRANSPARENCY = MathUtil.clamp(args.TRANSPARENCY, 0, 100) / 100;
penState.penAttributes.color4f[3] = TRANSPARENCY;
penState.transparency = this._wrapTransparency(Cast.toNumber(args.TRANSPARENCY));
this._updatePenColor(penState);
}
}