Javascript 是否可以将画布设置为前景?
我有一个简单的html页面,如下所示:Javascript 是否可以将画布设置为前景?,javascript,html,canvas,Javascript,Html,Canvas,我有一个简单的html页面,如下所示: <html> <head> <title></title> <link rel="icon" type="image/vnd.microsoft.icon" href="images/icon.png" /> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body bgcol
<html>
<head>
<title></title>
<link rel="icon" type="image/vnd.microsoft.icon" href="images/icon.png" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body bgcolor="ffffff"><br>
<img src="images/1.png" style="position:relative; left:4%; width:85%"></img>
<meta http-equiv="refresh" content="7; home.html" />
</body>
</html>
显示图像并在7秒钟后重定向到主页。我在网上找到了一个很酷的五彩纸屑代码,它使用canvas,我想把它添加到前台的这个页面中,但是我总是能够看到我将来可以添加到页面中的图像或文本,但是我找不到这样做的方法。五彩纸屑代码如下:
(function() {
var COLORS, Confetti, NUM_CONFETTI, PI_2, canvas, confetti, context, drawCircle, i, range, resizeWindow, xpos;
NUM_CONFETTI = 350;
COLORS = [[85, 71, 106], [174, 61, 99], [219, 56, 83], [244, 92, 68], [248, 182, 70]];
PI_2 = 2 * Math.PI;
canvas = document.getElementById("world");
context = canvas.getContext("2d");
window.w = 0;
window.h = 0;
resizeWindow = function() {
window.w = canvas.width = window.innerWidth;
return window.h = canvas.height = window.innerHeight;
};
window.addEventListener('resize', resizeWindow, false);
window.onload = function() {
return setTimeout(resizeWindow, 0);
};
range = function(a, b) {
return (b - a) * Math.random() + a;
};
drawCircle = function(x, y, r, style) {
context.beginPath();
context.arc(x, y, r, 0, PI_2, false);
context.fillStyle = style;
return context.fill();
};
xpos = 0.5;
document.onmousemove = function(e) {
return xpos = e.pageX / w;
};
window.requestAnimationFrame = (function() {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) {
return window.setTimeout(callback, 1000 / 60);
};
})();
Confetti = (function() {
function Confetti() {
this.style = COLORS[~~range(0, 5)];
this.rgb = "rgba(" + this.style[0] + "," + this.style[1] + "," + this.style[2];
this.r = ~~range(2, 6);
this.r2 = 2 * this.r;
this.replace();
}
Confetti.prototype.replace = function() {
this.opacity = 0;
this.dop = 0.03 * range(1, 4);
this.x = range(-this.r2, w - this.r2);
this.y = range(-20, h - this.r2);
this.xmax = w - this.r;
this.ymax = h - this.r;
this.vx = range(0, 2) + 8 * xpos - 5;
return this.vy = 0.7 * this.r + range(-1, 1);
};
Confetti.prototype.draw = function() {
var _ref;
this.x += this.vx;
this.y += this.vy;
this.opacity += this.dop;
if (this.opacity > 1) {
this.opacity = 1;
this.dop *= -1;
}
if (this.opacity < 0 || this.y > this.ymax) {
this.replace();
}
if (!((0 < (_ref = this.x) && _ref < this.xmax))) {
this.x = (this.x + this.xmax) % this.xmax;
}
return drawCircle(~~this.x, ~~this.y, this.r, "" + this.rgb + "," + this.opacity + ")");
};
return Confetti;
})();
confetti = (function() {
var _i, _results;
_results = [];
for (i = _i = 1; 1 <= NUM_CONFETTI ? _i <= NUM_CONFETTI : _i >= NUM_CONFETTI; i = 1 <= NUM_CONFETTI ? ++_i : --_i) {
_results.push(new Confetti);
}
return _results;
})();
window.step = function() {
var c, _i, _len, _results;
requestAnimationFrame(step);
context.clearRect(0, 0, w, h);
_results = [];
for (_i = 0, _len = confetti.length; _i < _len; _i++) {
c = confetti[_i];
_results.push(c.draw());
}
return _results;
};
step();
}).call(this);
(函数(){
var颜色、五彩纸屑、NUM_五彩纸屑、PI_2、画布、五彩纸屑、上下文、绘图圈、i、范围、大小窗口、XPO;
五彩纸屑数量=350;
颜色=[[85,71,106],[174,61,99],[219,56,83],[244,92,68],[248,182,70];
PI_2=2*Math.PI;
canvas=document.getElementById(“世界”);
context=canvas.getContext(“2d”);
窗口w=0;
h=0;
resizeWindow=函数(){
window.w=canvas.width=window.innerWidth;
返回窗口.h=canvas.height=window.innerHeight;
};
addEventListener('resize',resizeWindow,false);
window.onload=函数(){
返回setTimeout(resizeWindow,0);
};
范围=功能(a,b){
return(b-a)*Math.random()+a;
};
drawCircle=功能(x、y、r、样式){
context.beginPath();
弧(x,y,r,0,PI_2,false);
context.fillStyle=样式;
返回context.fill();
};
xpos=0.5;
document.onmousemove=函数(e){
返回xpos=e.pageX/w;
};
window.requestAnimationFrame=(函数(){
返回window.requestAnimationFrame | | window.webkitRequestAnimationFrame | | window.mozRequestAnimationFrame | | | window.oRequestAnimationFrame | | window.msRequestAnimationFrame | | |函数(回调){
返回窗口。设置超时(回调,1000/60);
};
})();
五彩纸屑=(函数(){
功能五彩纸屑(){
this.style=颜色[~~范围(0,5)];
this.rgb=“rgba(“+this.style[0]+”、“+this.style[1]+”、“+this.style[2]”);
这个.r=~~范围(2,6);
this.r2=2*this.r;
这个。替换();
}
Confetti.prototype.replace=函数(){
这是不透明度=0;
这个.dop=0.03*范围(1,4);
该.x=范围(-this.r2,w-this.r2);
this.y=范围(-20,h-this.r2);
this.xmax=w-this.r;
this.ymax=h-this.r;
这个.vx=范围(0,2)+8*xpos-5;
返回this.vy=0.7*this.r+范围(-1,1);
};
Confetti.prototype.draw=函数(){
var_ref;
this.x+=this.vx;
this.y+=this.vy;
this.opacity+=this.dop;
如果(this.opacity>1){
这是不透明度=1;
这个参数为.dop*=-1;
}
if(this.opacity<0 | | this.y>this.ymax){
这个。替换();
}
如果(!((0<(\u ref=this.x)和&\u ref
CSS
是的,有可能
CSS
是的,有可能
CSS
是的,有可能
CSS
像这样
<canvas id="world" style="position:absolute;z-index:3;pointer-events:none;"></canvas>
只需将其添加到html中,并将五彩纸屑部分添加到a.js(您自定义的js)中,如下所示
<canvas id="world" style="position:absolute;z-index:3;pointer-events:none;"></canvas>
只需将其添加到html中,并将五彩纸屑部分添加到a.js(您自定义的js)中,如下所示
<canvas id="world" style="position:absolute;z-index:3;pointer-events:none;"></canvas>
只需将其添加到html中,并将五彩纸屑部分添加到a.js(您自定义的js)中,如下所示
<canvas id="world" style="position:absolute;z-index:3;pointer-events:none;"></canvas>
只需将其添加到html中,并将五彩纸屑部分添加到a.js(自定义的js)你能解释得更清楚一点吗,你是想把画布元素放在背景图像上?还是确切地说什么?是的,对不起。我有一个带有图像的简单页面,但将来可能我想添加一些文本或其他东西,这是一个可能要升级的页面。我在这个网站上找到了五彩纸屑动画:我在它应该看起来很酷作为前景(例如,在一个祝贺页面或什么的,它看起来更好与下降的五彩纸屑)。所以我需要的是五彩纸屑的效果,因为它是一个透明的层在一切,但不影响页面的功能(例如文本,图像,单选按钮…)通过制作指针事件,你已经有了一个答案:无你可以点击画布元素,只需在你的html页面中添加画布id并放置指针事件:无那就是所有的doneYes,非常感谢!你能解释得更清楚一点吗,你想让画布元素保持在背景图像之上吗?或者确切地说什么?是的,对不起。我有一个带有图像的简单页面,但将来可能我想添加一些文本或其他内容,这是一个可能会升级的页面。我在这个网站上找到了五彩纸屑动画:我认为它作为前景会很酷(例如,在祝贺页面或其他内容中,它与掉落的五彩纸屑一起看起来更好)所以我需要的是五彩纸屑效果,因为它是所有东西顶部的透明层,但不会影响页面的功能(例如文本、图像、单选按钮…)通过制作指针事件,你已经有了一个答案:无你可以点击画布元素,只需在你的html页面中添加画布id并放置指针事件:无那就是所有的doneYes,非常感谢!你能解释得更清楚一点吗,你想让画布元素保持在背景图像之上吗?或者确切地说什么?是的,对不起。我有一个带有图像的简单页面,但将来可能我想添加一些文本或其他内容,这是一个可能会升级的页面。我在这个网站上找到了五彩纸屑动画:我认为它作为前景会很酷(例如,在祝贺页面或其他内容中,它与掉落的五彩纸屑一起看起来更好).所以我需要的是五彩纸屑效果