google / blockly

The web-based visual programming editor.
https://developers.google.com/blockly/
Apache License 2.0
12.35k stars 3.7k forks source link

Create block with own icon from assets. #760

Closed edi233 closed 7 years ago

edi233 commented 7 years ago

Hello, I want to create custom block where I want to add my own icon from assets. I create block:

{
    "type": "left_arrow",
    "message0": "%1",
    "args0": [
      {
        "type": "field_image",
        "src": "file:///android_asset/ic_arrow_left.png",
        "width": 15,
        "height": 15,
        "alt": "*"
      }
    ],
    "previousStatement": null,
    "nextStatement": null,
    "colour": 210,
    "tooltip": "",
    "helpUrl": "http://www.example.com/"
  }

but I can't see my arrow. Any help?
bkisdi commented 7 years ago

Is your page served from the file system or a (local) web server? I guess file:// is not applicable in your case. Can you reference the image with a relative path?

edi233 commented 7 years ago

yes, I want to load image from internal storage my phone with android. On Blockly page I find advice to use Blockly.pathToBlockly but I don't know how can I do that. I don't want to use web server for icons, I need load them from internal storage my phone.

bkisdi commented 7 years ago

I see, but is the blockly file served from the same/parent /sibling directory as the image?

See Blockly.Blocks['text'] lines 32-33 and 35-36 for an example of Blockly.pathToBlockly. (You can use it after the "src": as well.) Warning! This is an older version of Blockly from 2013 (NOT the current branch by Google), but the example should work.

AnmAtAnm commented 7 years ago

Assuming you are loading your .html containing Blockly from the assets, also, can you use a relative URL? No leading slash. Parent directory references (../) should be allowed.

Alternatively, use a data: URI. Try a converter like this one: http://websemantics.co.uk/online_tools/image_to_data_uri_convertor/

This is how we do the quotes on the text blocks: https://github.com/google/blockly/blob/7928fac3cebd4ba310d94727e7b32ead06cb16b0/blocks/text.js#L68

edi233 commented 7 years ago

ok, I find what was wrong in code. I use new .arr files from blockly, but only with release .arr file works. For debug .arr nothing happen in code. So when I build release version my app I could see icons, for debug I can't see changes.

AnmAtAnm commented 7 years ago

It sounds like you're using the Android build. If so, let's move the discussion to blockly-android or to the general mailing list. This repo is for the web version.