Элемент холста html5 на фоне моей страницы?
Возможно ли иметь полноэкранный элемент холста на фоне веб-страницы и "нормальных" элементов разметки, таких как таблица перед ним?
как следующий фрагмент (если он не будет использоваться как альтернативный контент):
<canvas id="imageView" width="100%" height="100%">
<table>...</table>
</canvas>
Ответы
Ответ 1
Вы можете попробовать установить стиль CSS на холсте, где он имеет position: fixed
(или absolute
, если необходимо), а затем любой контент, который следует за ним (в отличие от содержимого контейнера как вы указали в своем примере) должны сидеть на вершине.
Ответ 2
Я попробовал это для вас со следующим кодом. Див помещается поверх элемента холста, как описывает его Матфей. Так что работа для вас
<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Canvas demo</title>
<style type="text/css">
#canvasSection{ position:fixed;}
</style>
<script type="text/javascript">
function draw()
{
//paint the text
var canvas = document.getElementById('canvasSection');
var context = canvas.getContext('2d');
context.fillStyle = '#00f';
context.font = 'italic 30px sans-serif';
context.textBaseline = 'top';
context.font = 'bold 30px sans-serif';
context.strokeText('Your Text!!', 0, 0);
//paint the square
var canvasSquare = document.getElementById('canvasSquare');
var ctxSquare = canvas.getContext('2d');
ctxSquare.fillStyle='#FF0000';
ctxSquare.fillRect(0, 100,50,100);
}
</script>
</head>
<body onLoad="draw()">
<canvas id="canvasSection">Error, canvas is not supported</canvas>
<div>TestText</div>
</body>
</html>
Ответ 3
<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>