Adding Cap to even block with javascript

112 views
Skip to first unread message

Daniel Phyo

unread,
Sep 20, 2022, 5:26:45 AM9/20/22
to Blockly
Hi, 

I am spending some hours to add cap to block on Blockly with javascript. 

I have code like this below and I want to add cap on the block design. Could anyone help me? I tried to read guides but I cannot seem to do it. 

Blockly.Blocks['startblock'] = {
renderer: 'custom_renderer',

init: function () {
this.setStyle("'startHats': true");
this.appendDummyInput()
.setAlign(Blockly.ALIGN_CENTRE)
.appendField("When RUN is pressed");

this.setNextStatement(true, null);
this.setColour(135);
this.setTooltip("");
this.setHelpUrl("");



}

};

Beka Westberg

unread,
Sep 26, 2022, 12:52:30 PM9/26/22
to blo...@googlegroups.com
Hello!

Thank you for the question :D The `"startHats": true` should be contained within the theme itself, as part of one of the block styles. Then you can use `setStyle` to associate the block with the given style.

```
const blockStyles = {
  'hat_blocks': {
    'colourPrimary': '#4C97FF',
    'colourSecondary': '#4280D7',
    'colourTertiary': '#3373CC',
    'hat': 'cap',
  },
};

var theme = Blockly.Theme.defineTheme('themeName', {
  'blockStyles': blockStyles,
});

const workspace = Blockly.inject('blocklyDiv', {theme: theme});

// This code usually exists in another file.
Blockly.Blocks['startblock'] = {
  init: function () {
    this.setStyle('hat_blocks'); // Set the style to match the style *name*.

    this.appendDummyInput()
      .setAlign(Blockly.ALIGN_CENTRE)
      .appendField("When RUN is pressed");

    this.setNextStatement(true, null);\
  }
};
```

I hope that helps! If you have any further questions please reply =)
--Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/af1c95c0-8a9e-4b92-a6df-ec4fd592a3ffn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages