Can someone clear up some some confusion on my part.
Is it possible to have html over the top of a canvas? I keep reading that you can't have GUI elements such as Jquery with Canvas, but then I read you can have HTML over the canvas, why can you have one but not the other?
What I ideally want eventually is a good GUI over the top of my canvas, so just need to know what's possible and what isn't.
Because browsers will display either a canvas element or HTML controls that you put inside that element, but not both, you must place your controls outside of your canvas elements. To make it appear as though HTML controls are inside a canvas, you can use CSS to place the controls above the canvas.
We wrap the canvas and div in a container element, which is position: relative . Then the canvas and div are set to position: absolute . This is a great answer... but... When I have a large <div> the canvas element bleeds right thru the div, no matter what the z-index, the background color or background-image?
<canvas> is an HTML element which can be used to draw graphics via scripting (usually JavaScript). This can, for instance, be used to draw graphs, combine photos, or create simple animations.
In chrome type chrome://flags/ into the URL and once the page loads, find "Enable Developer Tools experiments". Close out of Chrome and once you load it back up, in the inspector you will find a new option under Profiles for "Capture Canvas Frame". Run that and it will output a list of every call performed.
Sure - you can put the HTML "on top" of the canvas by using absolute positioning.
http://jsfiddle.net/stevendwood/5sSWj/
You cannot have HTML "in" the canvas. But supposing that the canvas and the HTML use the same coordinates then you can use top and left to position elements in the canvas using the same offsets as you draw with.
#picture {
position: relative;
}
.blob, .blob1, .blob2 {
position: absolute;
width: 30px;
height: 30px;
border-radius: 20px;
background-color: green;
border: 2px solid #ccc;
}
var canvas = document.querySelector('canvas'),
context = canvas.getContext('2d');
context.beginPath();
context.moveTo(100, 150);
context.lineTo(350, 50);
context.stroke();
And the HTML...
<div id="picture">
<canvas id="canvas" width="500" height="500">
</canvas>
<div class="blob1"></div>
<div class="blob2"></div>
</div>
In this fetching example you can connect two positioned divs with a line drawn on a canvas element that is underneath them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With