canvas小球亂撞
本章節分享一段程式碼例項,它利用canvas實現了小球亂撞的效果。
想要對canvas有所瞭解的朋友可以自行分析一下程式碼。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> body{ font-family: 微軟雅黑; } body,h1{ margin:0; } canvas{ display:block; margin-left:auto; margin-right: auto; border:1px solid #DDD; background:-webkit-linear-gradient(top, #222,#111); } </style> </head> <body> <canvas id="canvas" ></canvas> <button id="stop">stop</button> <button id="run">run</button> <button id="addBall">addBall</button> <script src="jquery-1.6.2.min.js"></script> <script> var nimo={ aniamted:null, content:null, data:{ radiusRange:[5,20], speedRange:[-5,5], scrollHeight:null, scrollWdith:null }, balls:[], ele:{ canvas:null }, fn:{ creatRandom:function(startInt,endInt){//生產隨機數 var iResult; iResult=startInt+(Math.floor(Math.random()*(endInt-startInt+1))); return iResult }, init:function(){ nimo.data.scrollWdith=document.body.scrollWidth; nimo.data.scrollHeight=document.body.scrollHeight; nimo.ele.canvas=document.getElementById('canvas'); nimo.content=nimo.ele.canvas.getContext('2d'); nimo.ele.canvas.width=nimo.data.scrollWdith-50; nimo.ele.canvas.height=nimo.data.scrollHeight-100; }, addBall:function(){ var aRandomColor=[]; aRandomColor.push(nimo.fn.creatRandom(0,255)); aRandomColor.push(nimo.fn.creatRandom(0,255)); aRandomColor.push(nimo.fn.creatRandom(0,255)); var iRandomRadius=nimo.fn.creatRandom(nimo.data.radiusRange[0],nimo.data.radiusRange[1]); var oTempBall={ coordsX:nimo.fn.creatRandom(iRandomRadius,nimo.ele.canvas.width-iRandomRadius), coordsY:nimo.fn.creatRandom(iRandomRadius,nimo.ele.canvas.height-iRandomRadius), radius:iRandomRadius, bgColor:'rgba('+aRandomColor[0]+','+aRandomColor[1]+','+aRandomColor[2]+',0.5)' }; oTempBall.speedX=nimo.fn.creatRandom(nimo.data.speedRange[0],nimo.data.speedRange[1]); if(oTempBall.speedX===0){ oTempBall.speedX=1 } if(oTempBall.speedY===0){ oTempBall.speedY=1 } oTempBall.speedY=nimo.fn.creatRandom(nimo.data.speedRange[0],nimo.data.speedRange[1]); nimo.balls.push(oTempBall) }, drawBall:function(bStatic){ var i,iSize; nimo.content.clearRect(0,0,nimo.ele.canvas.width,nimo.ele.canvas.height) for(var i=0,iSize=nimo.balls.length;i<iSize;i++){ var oTarger=nimo.balls[i]; nimo.content.beginPath(); nimo.content.arc(oTarger.coordsX,oTarger.coordsY,oTarger.radius,0,10); nimo.content.fillStyle=oTarger.bgColor; nimo.content.fill(); if(!bStatic){ if(oTarger.coordsX+oTarger.radius>=nimo.ele.canvas.width){ oTarger.speedX=-(Math.abs(oTarger.speedX)) } if(oTarger.coordsX-oTarger.radius<=0){ oTarger.speedX=Math.abs(oTarger.speedX) } if(oTarger.coordsY-oTarger.radius<=0){ oTarger.speedY=Math.abs(oTarger.speedY) } if(oTarger.coordsY+oTarger.radius>=nimo.ele.canvas.height){ oTarger.speedY=-(Math.abs(oTarger.speedY)) } oTarger.coordsX=oTarger.coordsX+oTarger.speedX; oTarger.coordsY=oTarger.coordsY+oTarger.speedY; } } }, run:function(){ nimo.fn.drawBall(); nimo.aniamted=setTimeout(function(){ nimo.fn.drawBall(); nimo.aniamted=setTimeout(arguments.callee,10) },10) }, stop:function(){ clearTimeout(nimo.aniamted) } } } window.onload=function(){ nimo.fn.init(); var i; for(var i=0;i<10;i++){ nimo.fn.addBall(); } nimo.fn.run(); document.getElementById('stop').onclick=function(){ nimo.fn.stop() } document.getElementById('run').onclick=function(){ nimo.fn.stop() nimo.fn.run() } document.getElementById('addBall').onclick=function(){ var i; for(var i=0;i<10;i++){ nimo.fn.addBall(); } nimo.fn.drawBall(true); } } </script> </body> </html>
相關文章
- canvas小球碰壁效果Canvas
- canvas小球擺動效果Canvas
- 奇妙的canvas:彈跳小球Canvas
- canvas小球碰壁隨機運動Canvas隨機
- canvas小球拋物線運動Canvas
- canvas繪製小球漸隱漸現Canvas
- HTML5中canvas實現小球擊打小方塊遊戲HTMLCanvas遊戲
- 小球彈彈樂
- 什麼是“撞庫”?如何避免撞庫攻擊?
- JavaScript小球碰壁詳解JavaScript
- 撞庫是什麼意思?如何有效防範撞庫攻擊?
- ccf碰撞的小球(100分)
- CCF-CSP 20180302:小球碰撞
- 小球運動-常規1
- js小球重力和碰撞效果JS
- 什麼是撞庫?撞庫攻擊常見的方法有哪些?
- 什麼是撞庫及撞庫攻擊的基本原理
- 從校園到騰訊工作一年的那些跌跌撞撞!
- HTML5畫布-小球碰撞HTML
- 撞庫掃號防範
- css3小球上下移動效果CSSS3
- [SDOI2008] Sue 的小球 題解
- 戶外服裝品牌TheNorthFace遭遇撞庫 撞庫究竟如何成功竊取賬戶資訊
- 7-22 用天平找小球 (10分)
- JavaScript之多小球非對心彈性碰撞JavaScript
- CSS3小球靜態環繞效果CSSS3
- 什麼是“撞庫”?一定要小心!
- canvasCanvas
- 【不可思議的前端】動如脫兔的小球前端
- 【C語言練習題】小球反彈問題C語言
- WPF C# create canvas and draw ellipse in canvasC#Canvas
- canvas createPattern()Canvas
- canvas createImageData()Canvas
- canvas restore()CanvasREST
- canvas translate()Canvas
- canvas rotate()Canvas
- canvas scale()Canvas
- canvas measureText()Canvas