Jump to content

How to display what I write inside of a canvas?


OhNoItsATornahdo
 Share

Recommended Posts

What I'm trying to accomplish:

First, an image:

3NDr59K.png

There is a textbox underneath the 'code' tab - what I want is for all the canvas-related code written inside the HTML to be displayed on the canvas on the right.

 

My problem:

I'm struggling with doing so, I tried doing this:

run.addEventListener('click', function() {
  canvas.innerHTML = '<script>' + jsarea.value + '</script>';
})

But it didn't work. I'm also getting a 'Cannot read property 'getContext' of null' error on:

var ctx = canvas.getContext('2d');

I think this might be because the canvas is in an iframe.

 

How could I achieve what I'm trying to accomplish?

Link to comment
Share on other sites

I thought this sounded straight forward enough but then you threw in the curve ball with the iframe.

Here is a fiddle that shows using an input (textarea would work) onInput event firing a render update in the canvas https://jsfiddle.net/mattstyles/mo2y80h8/ might help.

There are a couple of ways though, google for reaching in to an iframe to grab elements, as long as the iframe url and the parent url are from the same domain then you can do this, although not sure if you can grab a context for a canvas. If you can not just grab the context and then call methods on it then you might want to look into using postMessage to pass state between parent and child (iframe). Or ditch the iframe.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...