国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Canvas放置反彈效果隨機(jī)圖形(實(shí)例)

2024-05-06 15:12:47
字體:
供稿:網(wǎng)友

Canvas放置反彈效果隨機(jī)圖形(實(shí)例)

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);  }    //判斷圖形坐標(biāo)是否超出畫布范圍  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;   } })            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 远安县| 错那县| 林甸县| 南陵县| 当阳市| 白城市| 社会| 扬州市| 云林县| 华安县| 绥阳县| 巴青县| 宝坻区| 芦溪县| 本溪| 布尔津县| 永川市| 高陵县| 皋兰县| 紫金县| 文昌市| 盘山县| 勐海县| 宁晋县| 枣阳市| 东莞市| 新安县| 尚志市| 辉县市| 富锦市| 乌鲁木齐县| 舞阳县| 常州市| 吴桥县| 顺义区| 凤凰县| 武陟县| 册亨县| 峨山| 库车县| 金塔县|