Sign in to follow this  
Mythros

how to programatically create multiple canvases?

Recommended Posts

Like any others dom elements:

var canvas = document.createElement("canvas");
document.children[0].appendChild(canvas);

And then use it as usual. Be simply careful cause the number of contexts you can create is limited.

Share this post


Link to post
Share on other sites

var canv = document.createElement ( 'canv1' );

document.children [ 0 ].appendChild ( canv );

alert ( document.getElementById ( 'canv1' ) );

Uncaught TypeError: Cannot read property 'getContext' of null

Why am I receiving this error? :( I just wanna create multiple canvases...

Share this post


Link to post
Share on other sites

You can read about createElement in MDN (for example): https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

the 1st variable should be a tag name (canvas) and not an id or a name. MDN is your friend, search there and you will see that you can find answers to all of your JS questions.

Share this post


Link to post
Share on other sites
5 hours ago, Mythros said:

@RaananW

ok : 

var canv = document.createElement ( 'canvas' );

document.children [ 0 ].appendChild ( canv );

this works but why is it showing a white screen when all my files are loading correctly?

Sorry but this question is so void of detail it is unanswerable even by Derren Brown (English Mindreader).

Share this post


Link to post
Share on other sites

for(var i=0; i < 10;  i++){
var cvas = document.createElement('canvas');
cvas.setAttribute('id', 'renderCanvas'+i);
cvas.classList.add('canvas-thingy');
var target = document.body || what ever...
target.appendChild(cvas);

}


Errors on your page:
3infinityfree.net/errors/404 Failed to load resource: the server responded with a status of 404 ()
main.php:1 Failed to load https://infinityfree.net/errors/404: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://babylontesting.epizy.com' is therefore not allowed access. The response had HTTP status code 404.
2infinityfree.net/errors/404:1 GET https://infinityfree.net/errors/404 404 ()
main.php:1 Failed to load https://infinityfree.net/errors/404: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://babylontesting.epizy.com' is therefore not allowed access. The response had HTTP status code 404.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.