調査アンケート: 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 Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-25 UTC。
[null,null,["最終更新日 2025-07-25 UTC。"],[[["\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 });"]]