OhNoItsATornahdo Posted October 25, 2017 Share Posted October 25, 2017 What I'm trying to accomplish: First, an image: 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? Quote Link to comment Share on other sites More sharing options...
mattstyles Posted October 26, 2017 Share Posted October 26, 2017 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.