H5canvas绘制404未找到页面动画代码

  • 内容
  • 相关

html5 canvas绘制404未找到页面动画模板

代码如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>404页未找到</title>

<style>
html,body {
	margin:0;
	padding:0;
}
canvas {
	display:block;
}
</style>
</head>
<body>

<div></div>

<script>
var canvas = document.createElement('canvas');
      var height = canvas.height = window.innerHeight;
      var width = canvas.width = window.innerWidth;
      var ctx = canvas.getContext('2d');
      document.body.appendChild(canvas);

      function random(min,max)
      {
          return Math.random()*(max-min+1)+min;
      }

      function range_map(value,in_min, in_max, out_min, out_max) {
        return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
      }

      var word_arr = [];
      var txt_min_size = 5;
      var txt_max_size = 25;
      var keypress = false;
      var acclerate = 2;
      for (var i = 0; i < 25; i++) {
        word_arr.push({
          x : random(0,width),
          y : random(0,height),
          text : '404',
          size : random(txt_min_size,txt_max_size)
        });

        word_arr.push({
          x : random(0,width),
          y : random(0,height),
          text : 'page',
          size : random(txt_min_size,txt_max_size)
        });


        word_arr.push({
          x : random(0,width),
          y : random(0,height),
          text : 'not found',
          size : random(txt_min_size,txt_max_size)
        });

        word_arr.push({
          x : random(0,width),
          y : random(0,height),
          text : '404',
          size : Math.floor(random(txt_min_size,txt_max_size))
        });
      }

      function render()
      {
        ctx.fillStyle = "rgba(0,0,0,1)";
        ctx.fillRect(0,0,width,height);

        ctx.fillStyle = "#fff";
        for (var i = 0; i < word_arr.length; i++) {
          ctx.font = word_arr[i].size+"px sans-serif";
          var w = ctx.measureText(word_arr[i].text);
          ctx.fillText(word_arr[i].text,word_arr[i].x,word_arr[i].y);

          if(keypress)
          {
            word_arr[i].x += range_map(word_arr[i].size,txt_min_size,txt_max_size,2,4) * acclerate;
          }
          else {
            word_arr[i].x += range_map(word_arr[i].size,txt_min_size,txt_max_size,2,3);
          }

          if(word_arr[i].x >= width)
          {
            word_arr[i].x = -w.width*2;
            word_arr[i].y = random(0,height);
            word_arr[i].size =  Math.floor(random(txt_min_size,txt_max_size));

          }
        }

        ctx.fill();

        requestAnimationFrame(render);
      }

      render();

      window.addEventListener('keydown',function(){
        keypress = true;
      },true);
      window.addEventListener('keyup',function(){
        keypress = false;
      },true);</script>

</body>
</html>

 在线演示

您阅读这篇文章共花了:  

本文标签:

版权声明:若无特殊注明,本文皆为《Admei》原创,转载请保留文章出处。

本文链接:H5canvas绘制404未找到页面动画代码 - https://www.zadmei.com/canvas.html

发表评论

提示:QQ和电子邮件地址不会被公开,昵称填写不能带有特殊符号。