研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
创建自定义渲染程序
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如需创建自定义渲染程序,您需要对 Renderer
类进行子类化。如需详细了解渲染程序的定义及其用途,请参阅渲染程序概念文档。
class CustomRenderer extends Blockly.blockRendering.Renderer {
constructor() {
super();
}
}
未进行任何自定义时,默认渲染程序如下所示:
您还可以对其他某个内置渲染程序进行子类化,然后替换其部分内容。
class CustomRenderer extends Blockly.thrasos.Renderer {
constructor() {
super();
}
}
派生其他渲染程序组件
块的实际形状由渲染程序的子组件决定。
默认情况下,Renderer
类提供所有渲染程序组件的有效版本。这样,您就可以修改单个组件,而无需担心其他组件。
例如,如果您想更改连接的形状,可以替换常量,而无需更改其他组件。
如需详细了解每个单独组件的作用,请参阅渲染程序组件文档。
替换工厂方法
在对渲染程序组件进行子类化后,您需要为您子类化的组件替换 Renderer
的工厂方法。这样,渲染程序就可以正确地将不同的组件连接在一起。
每种组件都有对应的方法:
注册渲染程序
最后,完成自定义渲染程序的创建后,您需要对其进行注册。这会将渲染程序与字符串相关联,以便您将其传递给配置选项。
Blockly.blockRendering.register('custom_renderer', CustomRenderer);
const workspace = Blockly.inject(blocklyDiv, {
renderer: 'custom_renderer',
});
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eTo create custom renderers in Blockly, you need to subclass the \u003ccode\u003eRenderer\u003c/code\u003e class and potentially its subcomponents like constants, render info, path objects, and drawers.\u003c/p\u003e\n"],["\u003cp\u003eCustom renderers allow you to change the visual appearance of blocks, including connection shapes and overall block structure, by overriding factory methods for specific components.\u003c/p\u003e\n"],["\u003cp\u003eAfter creating the custom renderer, register it with a unique string using \u003ccode\u003eBlockly.blockRendering.register()\u003c/code\u003e to use it within your workspace configuration.\u003c/p\u003e\n"],["\u003cp\u003eBefore starting with custom renderers, it's recommended to review the renderer concept documentation and complete the custom renderers codelab for foundational knowledge.\u003c/p\u003e\n"]]],[],null,["# Create custom renderers\n\n| **Note:** If you haven't read through the [renderer concept docs](/blockly/guides/create-custom-blocks/renderers/concepts/renderer) or completed the [custom renderers codelab](https://blocklycodelabs.dev/codelabs/custom-renderer/index.html) we recommend doing those first!\n\nTo create a custom renderer, you need to subclass the [`Renderer`](/blockly/reference/js/blockly.blockrendering_namespace.renderer_class)\nclass. Refer to the [renderer concept docs](/blockly/guides/create-custom-blocks/renderers/concepts/renderer) for more\ninformation about what a renderer is and what it does. \n\n class CustomRenderer extends Blockly.blockRendering.Renderer {\n constructor() {\n super();\n }\n }\n\nWithout any customization, the default renderer looks like this:\n\nYou can also subclass one of the other [built-in renderers](/blockly/guides/create-custom-blocks/renderers/overview#built-in_renderers)\nand then override parts of it. \n\n class CustomRenderer extends Blockly.thrasos.Renderer {\n constructor() {\n super();\n }\n }\n\nSubclass other renderer components\n----------------------------------\n\nThe actual shape of the block is determined by the\n[subcomponents](/blockly/guides/create-custom-blocks/renderers/concepts/overview) of the renderer.\n\nBy default, the [`Renderer`](/blockly/reference/js/blockly.blockrendering_namespace.renderer_class) class provides working versions of\nall the [renderer components](/blockly/guides/create-custom-blocks/renderers/concepts/overview). This lets you modify a\nsingle component, without having to worry about the others.\n\nFor example, if you want to\n[change the shapes of connections](/blockly/guides/create-custom-blocks/renderers/create-custom-renderers/connection-shapes), you can override the\n[constants](/blockly/guides/create-custom-blocks/renderers/concepts/constants) without having to touch the other components.\n\nCheck out the [renderer component docs](/blockly/guides/create-custom-blocks/renderers/concepts/overview) for more\ninformation about what each individual component does.\n\nOverride factory methods\n------------------------\n\nAfter subclassing the [renderer components](/blockly/guides/create-custom-blocks/renderers/concepts/overview), you need to\noverride the [`Renderer`](/blockly/reference/js/blockly.blockrendering_namespace.renderer_class)'s factory methods for the components you\nsubclassed. This lets the renderer properly wire the different components\ntogether.\n\nThere is a method for each kind of component:\n\n- [`makeConstants_`](/blockly/reference/js/blockly.blockrendering_namespace.renderer_class.makeconstants__1_method)\n- [`makeRenderInfo_`](/blockly/reference/js/blockly.blockrendering_namespace.renderer_class.makerenderinfo__1_method)\n- [`makePathObject`](/blockly/reference/js/blockly.blockrendering_namespace.renderer_class.makepathobject_1_method) (note there is no underscore)\n- [`makeDrawer_`](/blockly/reference/js/blockly.blockrendering_namespace.renderer_class.makedrawer__1_method)\n\nRegister the renderer\n---------------------\n\nFinally, once you've completed the creation of your custom renderer, you need to\nregister it. This associates the renderer with a string so that you can pass it\nto your [configuration options](/blockly/guides/configure/web/configuration_struct). \n\n Blockly.blockRendering.register('custom_renderer', CustomRenderer);\n\n const workspace = Blockly.inject(blocklyDiv, {\n renderer: 'custom_renderer',\n });"]]