A renderer in Blockly controls the shape of a block, including height, padding, border thickness, and connection shape.
Custom renderers
If you want to customize the shape of blocks, you will need to create a custom renderer. You can learn more about this process by completing the codelab or reading the reference documentation. It may be helpful to read the code for any of Blockly's built-in renderers to understand how they work.
To create a custom renderer, you will need to:
- Define a new renderer. You can subclass either the base renderer class or any of the existing renderers depending on where you'd like to start from.
- Override the parts you wish to change.
- For example, to add more padding to blocks, you can subclass a
ConstantProvider
(again, either the base or any existing renderer), and override the relevant constant. All other values will remain the same as your chosen base class. - In your custom
Renderer
subclass, you need to hook up the newConstantProvider
class. Override themakeConstants_
function to return a new instance of your customConstantProvider
instead of the base class. - Follow the same process when overriding other classes such as
PathObject
orDrawer
.
- For example, to add more padding to blocks, you can subclass a
Register your renderer:
Blockly.blockRendering.register('custom_renderer', CustomRenderer);
Use your renderer in your application:
Blockly.inject('blocklyDiv', { renderer: 'custom_renderer' });
Built-in renderers
Blockly provides several pre-built renderers. You can use these as-is or you can use them as the base of a custom renderer.
- geras (default)
- thrasos (more modern take on geras)
- zelos (Scratch-like)
- minimalist (the base renderer classes)
To use one of these renderers, pass the name into the injection options:
Blockly.inject('blocklyDiv', {
renderer: 'thrasos'
});
To subclass them, extend the appropriate class(es):
class CustomRenderer extends Blockly.geras.Renderer {}
class CustomConstantProvider extends Blockly.geras.ConstantProvider {}