Skip to main content
Jeffery.Wright
Inspiring
May 16, 2025
Question

Call a JS function on HTML page to populate Canvas dynamic text.

  • May 16, 2025
  • 0 replies
  • 107 views

I am trying to use a very simple, single frame canvas element to dynamically display text from variables defined in a Javascript file.   

This works fine if I call the function on the timeleine in the editor, but not when I call the same function on the page itself. 

Since the canvas is to be a blank container to display whatever JS content is connected, the function cannot be on the timeline (which compiles to the main Animate JS file) 

 

On the web page I have a link to an external JS file 

<script src="PRODUCT1.js"></script>

that contains this code: 

this.m_PRODUCT1=function() {
exportRoot.PDisplay.title.text = t_PRODUCT1; 
exportRoot.PDisplay.copy.text = c_PRODUCT1;
PP_URL = u_PRODUCT1;
var p = new createjs.Bitmap(p_PRODUCT1);
exportRoot.PDisplay.pic.removeAllChildren(); exportRoot.PDisplay.pic.addChild(p);
}

Which works fine when I place m_PRODUCT1(); on the editor timeline.

I need to be able to call the function on the web page itself, this is placed after all the Animate scripts:

<script>
m_PRODUCT1();
// I tried exportRoot.m_PRODUCT1(); and this.m_PRODUCT1(); no dice
</script>
</head>

Which produces the error: Uncaught TypeError: Cannot read properties of undefined (reading 'PDisplay') which is the line: exportRoot.PDisplay.title.text = t_PRODUCT1; 

at m_PRODUCT1 (PRODUCT1.js:54:12) at productDisplayMOB.html:125:1 Which is the function call on the page...

 

On the timeline in the editor, I have this code:, if that makes a difference 

createjs.Touch.enable(stage);
var _this = this;
var root = this;

 

One note: This function does work on the page if I place it within the Animate script generated on the page, at the end after fnStartAnimation(); but I don't want users to have to hunt through all that code to change the function name to another JS function.

I am sure this is a scoping issue which always stops me in my tracks, does anyone have an idea how to make this work?  

Thank you and have a great weekend. 

-JW