I have a problem (above). That is the error message I get when trying to run my Blockly JavaScript program. My goal of the program is that a user can drag a block (for now its a line) into the workspace (as many as the user wants) and with that block when run the program will draw a line (or lines) on user input X, Y, length. I have a program that is working like this but the problem is which one of your "helpers" told me is that I am not using Blockly correctly. He told me that I need to have a JavaScript generator instead of running the code directly. So with that I have made some adjustments and looked up on your documentation but I have still some troubles. One of that is thta error message and the other is that is my added code even correct? I would be thankful if someone will help me with this. If any more information is needed please ask. Thank you.
<html>
<head>
<title>iMathArt</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class = "Leva_stran">
<div class = "Glava">
<h1>Osnove Kvadratkov</h1>
</div>
<div class = "DelavniProstor" id="blocklyDiv"></div> <!-- Blockly prostor (pravokotnik za notr dat bloke)-->
</div>
<!-- Element za prikaz Blockly workspace -->
<div class = "Sredina_stran">
<div class = "RisanjeProstor">
<div id="canvasContainer"></div> <!--- mora biti v HTML!!! ne dela v !!!CSS!!!!-->
</div>
</div>
<div class = "Desna_stran">
<div class = "KodaProstor">
<canvas id="KanvasKodaProstor"></canvas>
</div>
</div>
<!-- Toolbox z različnimi bloki -->
<xml id="toolbox" style="display: none">
<category name="Kvadrat">
<block type = "CrtaXY"></block>
</category>
</xml>
<button id = "MainGumb" class = "Start">Zazeni</button>
<button id = "SmetnjakGumb" onclick="BrisanjeKanvas()">Zbrisi</button>
<script src="sketch.js"></script>
</body>
</html>
// SE IZVEDE SAMO ENKRAT function setup(){
var canvas = createCanvas(634,634); // ustvarim p5.js canvas !obvezno!
canvas.parent("canvasContainer"); // povem, da naj vzame css pa tut html kodo od tega diva oz. od naddiva tega diva
}
// DEFINICIJA BLOKA CrtaXY
Blockly.Blocks["CrtaXY"] = {
init: function () {
this.appendDummyInput()
.appendField("Narisi crto z X koordinato")
.appendField(new Blockly.FieldNumber(0), "X"); // vnesem X koordinato
this.appendDummyInput()
.appendField("Narisi crto z Y koordinato")
.appendField(new Blockly.FieldNumber(0), "Y"); // vnesem Y koordinato
this.appendDummyInput()
.appendField("Narisi crto z dolzino")
.appendField(new Blockly.FieldNumber(0), "DOLZINA"); // vnesem dolzino crte
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(160);
}
};
// Generacija JavaScript kode za blok CrtaXY
Blockly.JavaScript["CrtaXY"] = function(block) {
var X = block.getFieldValue("X");
var Y = block.getFieldValue("Y");
var dolzina = block.getFieldValue("DOLZINA");
var code = `NarisiCrto(${dolzina}, ${X}, ${Y});\n`;
return code;
};
// FUNKCIJA ZA BRISANJE PLATNA
function BrisanjeKanvas(){
clear(); // pobrisi kanvas
workspace.clear(); // pobrisi delovno mesto.
}
// METODA ZA RISANJE CRTE
function NarisiCrto(dolzina, X, Y) {
stroke('magenta');
strokeWeight(5);
line(X, Y, X + dolzina, Y); // p5.js naredi crto z zacetkom XY in koncem XY
}
// POSTAVITEV WORKSPACE-a ---- kantica in premikanje po njem je ze avtomatsko notri.
var workspace = Blockly.inject("blocklyDiv", {toolbox: document.getElementById("toolbox")});
document.getElementById("MainGumb").addEventListener("click", () => {
const code = Blockly.JavaScript.workspaceToCode(workspace);
try {
eval(code);
} catch (e) {
console.error(e);
}
});