From 3185eb27e15fc230796b8ebcb1099ed1d3140b00 Mon Sep 17 00:00:00 2001 From: Ken Date: Fri, 1 Sep 2017 14:11:09 +0800 Subject: [PATCH] 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). --- src/blocks/scratch3_pen.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/blocks/scratch3_pen.js b/src/blocks/scratch3_pen.js index 91eaf2909..273368e7c 100644 --- a/src/blocks/scratch3_pen.js +++ b/src/blocks/scratch3_pen.js @@ -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 @@ -197,6 +198,18 @@ class Scratch3PenBlocks { if (value < 0) value += 200; 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. @@ -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); } }