Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html over the Canvas?

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.

like image 264
Phil Avatar asked May 16 '13 11:05

Phil


People also ask

Can I put HTML in canvas?

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.

How do I overlay a div in 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?

What is HTML canvas used for?

<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.

How do I view canvas in HTML?

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.


1 Answers

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.

like image 160
Woody Avatar answered Oct 22 '22 23:10

Woody