快捷导航
蚂蚁部落 网站首页 实例代码 canvas实例 查看内容

canvas气泡上浮效果代码实例

2018-3-19 14:15| 发布者: admin| 查看: 585| 评论: 0|来自: 蚂蚁部落

本章节分享一段代码实例,它使用canvas实现了气泡上浮的功能。

代码实例如下:

[HTML] 纯文本查看 复制代码运行代码
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落</title>
<script>  
window.onload = function () {
  function Circle() {
    this.x = Math.random() * canvas.width;
    this.y = canvas.height;
    this.r = Math.random() * 10;
    //绘制圆形
    this.paint = function () {
      context.beginPath();
      context.arc(this.x, this.y, this.r, 0, Math.PI * 2);
      context.fillStyle = "white";
      context.globalAlpha = 0.5;
      context.fill();
    }
    //控制圆形移动
    this.step = function () {
      this.y--;
    }
  }
  var circles = [];
 
  function createCircles() {
    var circle = new Circle();
    circles[circles.length] = circle;
  }
  function paintCircles() {
    for (var i = 0; i < circles.length; i++) {
      circles[i].paint();
    }
  }
  function stepCircles() {
    for (var i = 0; i < circles.length; i++) {
      circles[i].step();
    }
  }
  var canvas = document.getElementById("canvas");
  var context = canvas.getContext("2d");
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
  var myimg = new Image();
  myimg.src = "mytest/demo/cite.jpg";
  var timer = "";
  setInterval(function () {
    context.drawImage(myimg, 0, 0);
    timer++;
    if (timer % 20 == 0) {
      createCircles();
    }
    paintCircles();
    stepCircles();
  }, 10);
}
</script>
</head>
<body>
<div id="d1">
  <canvas id="canvas"></canvas>
</div>
</body>
</html>

鲜花

握手

雷人

路过

鸡蛋

最新评论

关于我们|手机版|小黑屋| ( 鲁ICP备10022556号-3 )

GMT+8, 2018-4-21 10:11 , Processed in 0.102327 second(s), 22 queries .

Powered by Discuz! X3.2 Licensed

Copyright © 2012-2017 Design: 蚂蚁部落

返回顶部