html5 div inside a context of canvas - html

I started using html5 canvas object on my web site and I want to draw rectangles (or other shapes according to my application) on the canvas. I can write text on the rectangles using the context object.
What I am trying to do is that I want to put a div element in the rectangle. So I can semantically work with my objects on the canvas like put a paragraph and a border inside the rectangle etc., storing some trivial data in the objects. Is that possible?

Nothing goes inside the canvas tag except for elements that would be displayed if the browser doesn't support HTML5 and canvas. If you want to display regular HTML elements in a DIV, you could simply position it absolutely so that it floats above the canvas:
<canvas height="100" width="200" style="position:absolute;left:10;top:10"></canvas>
<div id="yourDiv" style="position:absolute;left:20;top:20">Your content</div>
Hopefully this helps.

Older Browser not supporting canvas should show those divs. As you can easily try out, other browsers will store the div in your dom tree, but not show it. So you should be able to do some dom semantics, but you'll have to paint them on your canvas for browsers supporting it.
If you want to show the divs and paint onto them using canvas, you'll have to use positioning to show them over each other.

Related

Hiding an SVG element in HTML without disabling clip paths defined inside?

With reference to the question clip-path not working in SVG sprite, it seems we can't use style="display:none" to hide an SVG element that defines a clip path that will be used elsewhere.
However, the suggested alternative for hiding it given (using width="0" height="0") is not working for me (at least in Chrome, the SVG element still gets allocated space in the page layout, which causes a vertical scroll bar to appear, as I have a div with height="100%" above it). What other was are available for hiding an SVG that won't stop it being used for clipping?
In the end, I used position:absolute to take the item out of the usual HTML document flow. I don't understand why a zero-sized element would cause scrollbars to appear, but that certainly seemed to be what was happening.

Put 3 canvases on a page, side-by-side

I need to put more than one (3) HTML5 Canvas side by side. Left and right will be 200px wide. Center will fill the rest of the space. All three need to fill the screen vertically.
What I'm doing is building a drawing program. Left and right will be drawing tools. Center will be the drawing area. I think I need a canvas specifically for the drawing area in order to zoom, pan, scroll the drawing without the image overlapping the tool windows. Essentially I'm using the center canvas as a clipping range. I don't know if this is the best way to do this or not.
What I've tried is putting 3 <canvas>es on the screen, but they just overlap. I put 3 in <div>s, but they just stack.
Thoughts?
As per the comments, <canvas> isn’t a void tag, so you need to write </canvas>.
You can also use that to provide helpful text in case somebody’s browsing in something not-quite-modern:
<canvas id="one">
Hey, your browser doesn’t support the Canvas API!
You should upgrade if you want to use this.
</canvas>

Masking a div with a canvas element

I'm attempting to make a slideshow composed mostly of divs that can be masked by a canvas element (so that it may be in a circle or strange shape, rather than a square. Is this possible? I have seen many examples of masking an image, but not an entire div or collection of divs.
Yes its certainly possible to mask divs. For instance a canvas could mask a div like this:
that's just an image, source here:
http://jsfiddle.net/r58jF/
or white, which it should be noted merely gives the illusion of a circular div and that illusion is highly contingent upon what is in or behind the div
(or something fancier)
You cannot use the rendering of HTML elements as an image source for a canvas, and so you cannot use a canvas to draw the contents of HTML. If you want to make arbitrary portions of a div fully- or partially-transparent, the answer is "No, you cannot do that with a Canvas."

How about a multi layered canvas without absolute

Hello everyone
Im trying to create a multilayered canvas and what i came up with was something like
<canvas class="canv" id="fight_layer1" width="680" height="381" style="position:relative;left:0;top:0;z-index:0">
I can't use the absolute it messes up the layers if im including the page on another one, is there any other way than this one, :)
Try to place all your canvas tags inside of the DIV with absolute positioning, then they relatively position inside of it.
Also for multi layer canvas see this script (could be useful):
http://code.google.com/p/multi-layer-canvas/

How to use an .png image as background in canvas html5?

I would like to use an image for background in the canvas element provided by HTML5.
The thing is I don't want the drawing to modify this image, I just want it to be use as a reference or guide for the user.
For example: if I draw a background image on the canvas then I add some lines and random drawings, once I use the erase tool it will erase both the lines I've made and the background image.
How can I prevent this?
Thanx.
you can use CSS to position the canvas over an <img> element with your background
this is similar to the usual trick to do layered animations: just do a different <canvas> element for each layer and put one on top of the other.
non-drawn pixels of <canvas> are transparent, not white.
You can simply use CSS.
Lets say you have a canvas such as :
<canvas id="Canvas" width="385px" height="330px"></canvas>
You can use CSS to put a background image :
#Canvas{background:url(Grid.png)}