Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An html5 canvas element in the background of my page?

Tags:

html

canvas

Is it possible to have a full screen canvas element in the background of a webpage and "normal" markup elements like a table in front of it?

like the following snippet (if it wouldn't be used as alternative content):

<canvas id="imageView" width="100%" height="100%">     <table>...</table> </canvas> 
like image 554
user306708 Avatar asked Apr 27 '10 08:04

user306708


People also ask

What is the canvas element in HTML5?

The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript. The <canvas> element is only a container for graphics. You must use JavaScript to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.

What is the default background color of a canvas element on the browser?

The default color is black.

Can a canvas have a background image?

There are two ways available in FabricJS, using which we can change the background image of the canvas. First method is by using the Canvas class itself and passing backgroundImage in the second parameter of the class. Second method is to use the setBackgroundColor method. Let's see into each of them with an example.


2 Answers

You could try setting a CSS style on the canvas where it has a position: fixed (or absolute as appropriate), and then any content that follows it (as opposed to container content as you've given in your example) should sit on top of it.

like image 101
Matthew Scharley Avatar answered Sep 29 '22 11:09

Matthew Scharley


<html>   <style>     body {       margin:0;       padding:0;     }     canvas{       position:absolute;       left:0;       top:0;       z-index:-1;     }     div{       position:absolute;       z-index:0;       left:12px;       top:10px;     }   </style>   <body>     <canvas id="myCanvas" width="600" height="600" style="border:1px solid #c3c3c3;">       Your browser does not support the canvas element.     </canvas>     <div>hello is floating div</div>      <script type="text/javascript">       var c = document.getElementById("myCanvas");       var ctx = c.getContext("2d");       var grd = ctx.createLinearGradient(0, 0, 600, 600);        grd.addColorStop(0, "#FF0000");       grd.addColorStop(1, "#00FF00");        ctx.fillStyle = grd;       ctx.fillRect(0, 0, 600, 600);     </script>   </body> </html>
like image 23
Rj Lakhani Avatar answered Sep 29 '22 11:09

Rj Lakhani