I am trying to draw canvas in wordpress editor.
In “Pages” with a custom made button for my plugin I can add the canvas using:
wp.media.editor.insert('<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">canvas test </canvas>');
but when I am trying to draw something in the canvas it does not work.
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "30px Georgia";
ctx.fillText("test", 90, 50);
I’m not sure why it cannot find the canvas in order to draw it.
Maybe this plugin useful for you.
https://wordpress.org/plugins/wc-shortcodes/
Please try it.
Ok, so I found the right way to do it.
The editor in “Pages” and “Posts” is in an iframe.
So this is the way to access and draw in the canvas: