javascript - 是否可以将 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 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);

关于如何在前台显示五彩纸屑动画但在后台保持页面功能齐全的任何建议或想法?

最佳答案

是的,有可能......

DEMO

CSS

pointer-events: none

关于javascript - 是否可以将 Canvas 设置为前景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218679/

相关文章:

javascript - Nashorn在JS中通过函数引用调用java对象的方法(通过绑定(bind)传递)

javascript - 使用javascript动态命名ID?

javascript - 如何根据国家名称获取国旗代码?

javascript - chart.js 在 div 和 Canvas 之外呈现

php - html5 Canvas 创建虚拟房间

javascript - 无法保存首次单击的状态

javascript - 尝试使用容器的 props 映射 Redux 状态时遇到困难

javascript - 自动点击iframe

javascript - Ng-Repeat 不重复(或返回任何内容)

qt - Context2D.putImageData 不起作用