Canvas放置反弹效果随机图形(实例)-创新互联

Canvas放置反弹效果随机图形(实例)

天镇ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
var raf;
  var arror = [];
  var running = false;
  //绘制圆形
  function createBall() {
    return {
      x: 0,
      y: 0,
      vx: 10-Math.random()*10,
      vy: 10-Math.random()*10,
      radius: 25,
      color:"red",
      draw: function() {
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
        ctx.closePath();
        ctx.fillStyle = this.color;
        ctx.fill();
      }
    }
  }
  //绘制正方形
  function createSquare() {
    return {
      x: 0,
      y: 0,
      vx: 10-Math.random()*10,
      vy: 10-Math.random()*10,
      color:"red",
      draw: function() {
        ctx.beginPath();
        ctx.fillStyle = this.color;
        ctx.fillRect(this.x, this.y,30, 30);
        ctx.closePath();
      }
    }
  }
  //绘制五角星
  function createStar() {
    return {
      x: 0,
      y: 0,
      vx: 10-Math.random()*10,
      vy: 10-Math.random()*10,
      color:"red",
      draw: function() {
        ctx.font = "24px serif";
        ctx.textBaseline = "hanging";
        ctx.fillStyle=this.color;
        ctx.fillText("五角星",this.x, this.y);

      }
    }
  }
  //绘制三角形
  function createTriangle() {
    return {
      x: 0,
      y: 0,
      vx: 10-Math.random()*10,
      vy: 10-Math.random()*10,
      color:"red",
      draw: function() {
        ctx.beginPath();
        ctx.moveTo(this.x,this.y);
        ctx.lineTo(this.x+25,this.y+25);
        ctx.lineTo(this.x+25,this.y-25);
        ctx.fillStyle=this.color;
        ctx.fill();
      }
    }
  }
  //清除
  function clear() {
    ctx.fillStyle = 'rgba(255,255,255,0.3)';
    ctx.fillRect(0,0,canvas.width,canvas.height);
  }
    //判断图形坐标是否超出画布范围
  function draw() {
    clear();
    arror.forEach(function(ball, i){
      ball.draw();
      ball.x += ball.vx;
      ball.y += ball.vy;
      if (ball.y + ball.vy > canvas.height || ball.y + ball.vy < 0) {
        ball.vy = -ball.vy;
      }
      if (ball.x + ball.vx > canvas.width || ball.x + ball.vx < 0) {
        ball.vx = -ball.vx;
      }
    });

    raf = window.requestAnimationFrame(draw);
  }
  canvas.addEventListener('click',function(e){
    if (!running) {
      raf = window.requestAnimationFrame(draw);
      running = true;
    }
    var colorarr=["#000000","#7F7F7F","#880015","#ED1C24","#FF7F27","#FFF200","#22B14C","#00A2E8","#3F48CC","#A349A4","#B97A57","#FFAEC9","#B5E61D"];
    var Graphics = ["Round","Square","Star","Triangle"];
    var typexz=Graphics[Math.floor(Math.random()*4)];
    var ball;
    switch(typexz){
      case "Round":
        ball = createBall();
        break;
      case "Square":
        ball = createSquare();
        break;
      case "Star":
        ball = createStar();
        break;
      case "Triangle":
        ball = createTriangle();
        break;
    }
    ball.x = e.clientX;
    ball.y = e.clientY;
    ball.color = colorarr[Math.floor(Math.random() * 10 + 3)];
    arror.push(ball);
  });
  draw();
 document.addEventListener('keydown',function (e) {
   if(e.keyCode==32){
     event.preventDefault();
     window.cancelAnimationFrame(raf);
     running = false;
   }
 })

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

当前标题:Canvas放置反弹效果随机图形(实例)-创新互联
新闻来源:https://www.cdcxhl.com/article32/cchpsc.html

成都网站建设公司_创新互联,为您提供响应式网站网站内链网站策划定制网站全网营销推广网站设计

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都app开发公司