1

我正在使用 blockly 来创建我的程序。我已经从这个 github 位置块下载,我正在尝试从这个位置替换块状图像路径:https://blockly-demo.appspot.com/static/media/sprites.png到这个(相对)位置:sprites.png。但我不知道该怎么做。

问题是:我可以在哪里设置这条路径?

这是我的页面代码(在 blockly 的根文件夹中):

<!DOCTYPE html>
<html lang="en" style="width: 100%; height: 100%;">
<head>
    <meta charset="UTF-8">
    <title>Blockly Test</title>
    <script src="blockly_compressed.js"></script>
    <script src="javascript_compressed.js"></script>
    <script src="blocks_compressed.js"></script>
    <script src="msg/js/en.js"></script>
</head>
<body style="width: 100%; height: 100%; margin: 0;">
    <xml xmlns="https://developers.google.com/blockly/xml" id="toolbox" style="display: none">
        // toolbox codes are here.
    </xml>
    <div id="blocklyArea" style="position: absolute; width: 100%; height: 100%;"></div>
    <div id="blocklyDiv" style="position: absolute;"></div>
    <script>
        var blocklyArea = document.getElementById('blocklyArea');
        var blocklyDiv = document.getElementById('blocklyDiv');
        var workspace = Blockly.inject(blocklyDiv, {toolbox: document.getElementById("toolbox")});
        var onresize = function(e) {
            // Compute the absolute coordinates and dimensions of blocklyArea.
            var element = blocklyArea;
            var x = 0;
            var y = 0;
            do {
                x += element.offsetLeft;
                y += element.offsetTop;
                element = element.offsetParent;
            } while (element);
            // Position blocklyDiv over blocklyArea.
            blocklyDiv.style.left = x + 'px';
            blocklyDiv.style.top = y + 'px';
            blocklyDiv.style.width = blocklyArea.offsetWidth + 'px';
            blocklyDiv.style.height = blocklyArea.offsetHeight + 'px';
            Blockly.svgResize(workspace);
        };
        window.addEventListener('resize', onresize, false);
        onresize();
        Blockly.svgResize(workspace);
        //document.body.innerHTML = document.body.innerHTML.replace(/https:\/\/blockly\-demo\.appspot\.com\/static\/media\/sprites\.png/gi, "sprites.png");
    </script>
    <button id="generator" onclick="try { eval(Blockly.JavaScript.workspaceToCode(workspace)); } catch (e) { alert(e); }" style="position: absolute; margin: 25px; right: 0; width: 80px; height: 80px; border-radius: 50%; border: 5px solid teal; background: darkcyan; color: white; font-size: x-large; outline: none; cursor: pointer;">⯈&lt;/button>
</body>
</html>

抱歉英语不好。

4

1 回答 1

0

您可以在注入块状时更改媒体路径。通话时参考媒体选项。Blockly.inject()

在您的情况下,您可以执行以下操作 -

var workspace = Blockly.inject(blocklyDiv, {toolbox: document.getElementById("toolbox"), media: './'});
于 2020-01-06T09:20:42.480 回答