GeezerVison is showing github.com — fix: show the glow only when blocks are running (#57) · scratchfoundation/scratch-blocks@33e9e91
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +78Lines changed: 78 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import * as Blockly from 'blockly/core'; | | 2 | +import {Colours} from '../core/colours.js'; | | 3 | + | | 4 | +/** | | 5 | + * Glow/unglow a stack in the workspace. | | 6 | + * @param {?string} id ID of block which starts the stack. | | 7 | + * @param {boolean} isGlowingStack Whether to glow the stack. | | 8 | + */ | | 9 | +export function glowStack(id, isGlowingStack) { | | 10 | + if (id) { | | 11 | + const block = Blockly.getMainWorkspace().getBlockById(id) || | | 12 | + Blockly.getMainWorkspace().getFlyout().getWorkspace().getBlockById(id); | | 13 | + if (!block) { | | 14 | + throw 'Tried to glow block that does not exist.'; | | 15 | + } | | 16 | + | | 17 | + const svg = block.getSvgRoot(); | | 18 | + if (isGlowingStack && !svg.hasAttribute('filter')) { | | 19 | + svg.setAttribute('filter', 'url(#blocklyStackGlowFilter)'); | | 20 | + } else if (!isGlowingStack && svg.hasAttribute('filter')) { | | 21 | + svg.removeAttribute('filter'); | | 22 | + } | | 23 | + } | | 24 | +} | | 25 | + | | 26 | +export function buildGlowFilter(workspace) { | | 27 | + const svg = workspace.getParentSvg(); | | 28 | + const defs = Blockly.utils.dom.createSvgElement(Blockly.utils.Svg.DEFS, {}, svg); | | 29 | + // Using a dilate distorts the block shape. | | 30 | + // Instead use a gaussian blur, and then set all alpha to 1 with a transfer. | | 31 | + const stackGlowFilter = Blockly.utils.dom.createSvgElement('filter', | | 32 | + { | | 33 | + 'id': 'blocklyStackGlowFilter', | | 34 | + 'height': '160%', | | 35 | + 'width': '180%', | | 36 | + y: '-30%', | | 37 | + x: '-40%' | | 38 | + }, | | 39 | + defs); | | 40 | + Blockly.utils.dom.createSvgElement('feGaussianBlur', | | 41 | + { | | 42 | + 'in': 'SourceGraphic', | | 43 | + 'stdDeviation': Colours.stackGlowSize | | 44 | + }, | | 45 | + stackGlowFilter); | | 46 | + // Set all gaussian blur pixels to 1 opacity before applying flood | | 47 | + const componentTransfer = Blockly.utils.dom.createSvgElement( | | 48 | + 'feComponentTransfer', {'result': 'outBlur'}, stackGlowFilter); | | 49 | + Blockly.utils.dom.createSvgElement('feFuncA', | | 50 | + { | | 51 | + 'type': 'table', | | 52 | + 'tableValues': '0' + ' 1'.repeat(16), | | 53 | + }, | | 54 | + componentTransfer); | | 55 | + // Color the highlight | | 56 | + Blockly.utils.dom.createSvgElement('feFlood', | | 57 | + { | | 58 | + 'flood-color': Colours.stackGlow, | | 59 | + 'flood-opacity': Colours.stackGlowOpacity, | | 60 | + 'result': 'outColor' | | 61 | + }, | | 62 | + stackGlowFilter); | | 63 | + Blockly.utils.dom.createSvgElement('feComposite', | | 64 | + { | | 65 | + 'in': 'outColor', | | 66 | + 'in2': 'outBlur', | | 67 | + 'operator': 'in', | | 68 | + 'result': 'outGlow' | | 69 | + }, | | 70 | + stackGlowFilter); | | 71 | + Blockly.utils.dom.createSvgElement('feComposite', | | 72 | + { | | 73 | + 'in': 'SourceGraphic', | | 74 | + 'in2': 'outGlow', | | 75 | + 'operator': 'over' | | 76 | + }, | | 77 | + stackGlowFilter); | | 78 | +} |
Collapse file Copy file name to clipboardExpand all lines: src/index.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,7 @@ import { | 29 | 29 | ContinuousMetrics, | 30 | 30 | } from '@blockly/continuous-toolbox'; | 31 | 31 | import {CheckableContinuousFlyout} from './checkable_continuous_flyout.js'; | | 32 | +import {buildGlowFilter, glowStack} from './glows.js'; | 32 | 33 | import './scratch_continuous_category.js'; | 33 | 34 |
| 34 | 35 | export * from 'blockly'; | @@ -41,6 +42,7 @@ export * from '../core/field_matrix.js'; | 41 | 42 | export * from '../core/field_note.js'; | 42 | 43 | export * from '../core/field_number.js'; | 43 | 44 | export * from '../msg/scratch_msgs.js'; | | 45 | +export {glowStack}; | 44 | 46 | export {scratchBlocksUtils}; | 45 | 47 | export {CheckableContinuousFlyout}; | 46 | 48 |
| @@ -53,6 +55,15 @@ export function inject(container, options) { | 53 | 55 | }, | 54 | 56 | }); | 55 | 57 | const workspace = Blockly.inject(container, options); | | 58 | + workspace.getRenderer().getConstants().selectedGlowFilterId = ''; | | 59 | + | | 60 | + const flyout = workspace.getFlyout(); | | 61 | + if (flyout) { | | 62 | + flyout.getWorkspace().getRenderer().getConstants().selectedGlowFilterId = ''; | | 63 | + } | | 64 | + | | 65 | + buildGlowFilter(workspace); | | 66 | + | 56 | 67 | return workspace; | 57 | 68 | } | 58 | 69 |
| |
|