NodeRed: Creating a canvas with ui_template and displaying it in the associated ui page

I am trying to create a canvas with a ui_template node, with the following code:

<div style="width: 200px; height: 200px;">
  <canvas id="myCanvas"></canvas>
</div>

<script>
  (function() {

    // Obtain canvas
    var canvas = document.getElementById("myCanvas");
    var ctx = canvas.getContext("2d");

    // Draw the dots
    function drawPoint(x, y, color) {
      ctx.beginPath();
      ctx.arc(x, y, 5, 0, 2 * Math.PI);
      ctx.fillStyle = color;
      ctx.fill();
    }

    
    var nodes = ["function_7", "function_8", "function_9"];
    for (var i = 0; i < nodes.length; i++) {
      (function(node, index) {
        node.on("input", function(msg) {
          if (msg.payload.x !== undefined && msg.payload.y !== undefined) {
            drawPoint(msg.payload.x, msg.payload.y, "red");
          }
        });
      })(RED.nodes.getNode(nodes[i]), i);
    }

  })();
</script>

Also, each node specified in the nodes array is a function node, which sends two values: msg.payload.x and msg.payload.y, with known values; the objective is to create a canvas where the three dots from each function are displayed

The thing is I can't visualize the created canvas in the corresponding ui page, and I don't know why, if I also specified a Group where the template is displayed. Thus, I ask for help. Thank you

Hello and welcome :slight_smile:

You can't access server side functions any way at front-end side. But you can watch incoming messages.
Modified your code to receive an array of points and draw those points on canvas.

[{"id":"c74cc62dfa492aea","type":"function","z":"09deee63c1b960f7","name":"function 2","func":"msg.payload = [\n    {x:10,y:20},\n    { x: 30, y: 50 },\n    { x: 60, y: 80 },\n    \n]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":2180,"wires":[["b402ba7bc4ae9cc5"]]},{"id":"811d4a6bd0ee686e","type":"inject","z":"09deee63c1b960f7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":2180,"wires":[["c74cc62dfa492aea"]]},{"id":"b402ba7bc4ae9cc5","type":"ui_template","z":"09deee63c1b960f7","group":"08ac9b242ae51e8c","name":"","order":15,"width":"6","height":"6","format":"\n<canvas id=\"myCanvas\"></canvas>\n\n\n<script>\n(function(scope) {\n\n\n\n    const canvas = document.getElementById(\"myCanvas\");\n    const ctx = canvas.getContext(\"2d\");\n\n    // Draw the dots\n    const drawPoint = function(x, y, color) {\n        ctx.beginPath();\n        ctx.arc(x, y, 5, 0, 2 * Math.PI);\n        ctx.fillStyle = color;\n        ctx.fill();\n    }\n\n    scope.$watch('msg', function(msg) {\n        if (msg) {\n\n            msg.payload.forEach(el => {\n                drawPoint(el.x,el.y,'red')\n            })\n           \n            \n        }\n    });\n})(scope);\n</script>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":480,"y":2180,"wires":[[]]},{"id":"08ac9b242ae51e8c","type":"ui_group","name":"Default","tab":"2acb95821445c8af","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"2acb95821445c8af","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Thank you!! I will try with the code you wrote! :grin:

Are you using any of Dashboards features or just ui_template? If not, you might find this easier to do with uibuilder which doesn't load all of the heavyweight AngularJS stuff but still offers plenty of interaction between node-red and your front-end.

I was trying to use only the "ui_template" node to attain my objective, which is also a node from the dashboard group of nodes. As you specify a group to display what you create, that's why I was trying with it. Thank you for your answer, I will look up into the uibuilder node !!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.