Call canvas functions from parent

0 favourites
  • 6 posts
From the Asset Store
Medeival & Viking War Horns Sounds / 22 Unique Calls / 2:40 minutes of audio
  • 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. :)

  • 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.

  • Nepeo

    Hm, on chrome and firefox globlaThis works.. but Microsoft Edge don't find globalThis!?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • globalThis is quite new. It's not supported in Edge yet. It was created to workaround the fact if you wrote JS for multiple environments ( like a server and the browser ) they used different names for the global object.

    From MDN:

    Historically, accessing the global object has required different syntax in different JavaScript environments. On the web you can use window, self, or frames - but in Web Workers only self will work. In Node.js none of these work, and you must instead use global.

    The this keyword could be used inside functions running in non–strict mode, but this will be undefined in Modules and inside functions running in strict mode. You can also use Function('return this')(), but environments that disable eval(), like CSP in browsers, prevent use of Function in this way.

    The globalThis property provides a standard way of accessing the global this value (and hence the global object itself) across environments. Unlike similar properties such as window and self, it's guaranteed to work in window and non-window contexts. In this way, you can access the global object in a consistent manner without having to know which environment the code is being run in.To help you remember the name, just remember that in global scope the this value is globalThis.

    I think the C3 runtime will create globalThis if it doesn't exist. But it depends where you're reading it from I guess.

    If you're only doing this in the browser it makes sense to just use window/frame.contentWindow.

    Edge is being replaced with a Chromium based version very soon, so it will have support for it then.

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