Call canvas functions from parent

  • I have create a iframe. And inside the iframe is the construct 3 canvas.

    But how can i call any canvas functions from the parent?

    This is how i get the canvas inside the iframe from parent:

    var iframe = document.getElementById("iframeheadermain"); // Works!
    iframe.onload= function() {
    	
    var iframe_canvas = iframe.contentDocument || iframe.contentWindow.document; // Works!
    
    var icanvas = iframe_canvas.getElementsByTagName("canvas")[0]; // Works!
    icanvas.MyFunctionInsideTheConstruct3Canvas(); // Here i call my custom construct 3 function - Dont work..
    };

    And this is the function inside a script from my construct 3 project:

    MyFunctionInsideTheConstruct3Canvas = function() {
    	alert('Works!');
    }
    	
    

    But, don't work, the scripts MyFunctionInsideTheConstruct3Canvas not exist... ?

  • Ok, i have found a solution..

    Call this on parent:

    globalThis[0].MyFunctionInsideTheConstruct3Canvas();
    
    

    It works now. :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Your script

    MyFunctionInsideTheConstruct3Canvas = function() {
    	alert('Works!');
    }
    

    Is a declaration in the global scope of the iframe. The global scope is unrelated to the canvas, which is why you cannot access the declaration.

    Looking at some of the labels here I think it's worth going over some definitions:

    • HTML element: a component of a document, there's lots of types and they in turn can contain other HTML elements. examples include images, iframes, canvas, text and buttons.
    • document: a collection of HTML elements, a browser tab shows 1 document.
    • iframe: a type of HTML element that can contain a document, allowing to place a document within a document.
    • canvas: a type of HTML element which is similar to an image, but has various methods that allow you to modify the contents of the image.
    • variable: (JavaScript) a container for a value
    • function: (JavaScript) a unit of code that can be run repeatedly, by another piece of code
    • scope: (JavaScript) an area in which variables and functions are declared
    • nested scope: (JavaScript) a scope, within another scope. it can see the contents of all the parent scopes, but the parent scopes cannot see the contents of a child scope.
    • global scope: (JavaScript) the root scope for a JavaScript context. Each document has a different JavaScript context, and therefore global scope.

    So let's break down what the world looks like to your code. You have 2 documents; the first contains an iframe, and the second contains a canvas. Let's call them A and B. Document A is the one that contains the iframe, and document B has been loaded into the iframe.

    You have the following code in document B

    function mynestedfunction () {
    	alert('Works!');
    }
    

    which has declared a variable in the global scope of document B, and placed a function into that variable.

    Document A has a piece of code that wants to get that function from document B and call it. First it needs to get hold of a reference to the iframe, by searching it's document for the iframe.

    // get a list of all the iframes in my document, then take the first one and place it into a variable
    var iframe = document.getElementsByTagName("iframe")[0];
    

    Now that we have the iframe we want to get the global scope of document B, which is available in the contentWindow property of the iframe, and then get the variable from it.

    // get the "Window" of the iframe, this is the global scope of it's document
    var iframe_scope = iframe.contentWindow;
    // get the function by it's name from scope
    var iframe_scope_function = iframe_scope.mynestedfunction;
    // call the function
    iframe_scope_function()
    

    There's a few interesting things to note here. Firstly if you aren't in a nested scope, any variables you declare will be in the global scope. Secondly the global scope is special in that it's contents can be read from the variable "window", you cannot do this with normal scopes. Thirdly window is declared in the global scope, so you can do silly things like window.window.window === window. Finally there is an alias to the window object called "globalThis", the reasons for using this are kinda complicated, but all you need to care is that it will give you the same value as "window".

  • Nepeo thanks for the extensive explanation. :)

    With globalThis it works for me.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)