canvas小球落地彈動效果
分享一段程式碼例項,他事先了拋落小球,然後小球會因為重力原因出現彈動效果。
程式碼是由canvas實現,感興趣的朋友可以做一下參考。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> </head> <body> <canvas id="canvas"></canvas> <script type="text/javascript"> var ball = { x: 512, y: 100, r: 20, g: 2, vx: -4, vy: -5, color: "#005588" }; // g:速度 window.onload = function() { var canvas = document.getElementById("canvas"); width = canvas.width = 1024; height = canvas.height = 768; var context = canvas.getContext("2d"); time = 50; setInterval(function() { render(context); update(); }, time) } function update() { ball.x += ball.vx; ball.y += ball.vy; ball.vy += ball.g; if (ball.y >= 768 - ball.r) { ball.y = 768 - ball.r; ball.vy = -ball.vy * 0.5; } } function render(cxt) { cxt.clearRect(0, 0, cxt.canvas.width, cxt.canvas.height); cxt.beginPath(); cxt.arc(ball.x, ball.y, ball.r, 0, 2 * Math.PI); cxt.closePath(); cxt.fill(); } </script> </body> </html>
相關文章
- canvas小球擺動效果Canvas
- canvas小球碰壁效果Canvas
- canvas實現的彈力小球效果程式碼例項Canvas
- 奇妙的canvas:彈跳小球Canvas
- canvas小球旋轉loadding載入效果Canvas
- canvas小球碰壁隨機運動Canvas隨機
- canvas小球拋物線運動Canvas
- css3滑鼠懸浮小球彈性效果CSSS3
- canvas小球亂撞Canvas
- js隨機運動小球效果JS隨機
- css3小球上下移動效果CSSS3
- canvas實現的小球四壁碰撞效果程式碼例項Canvas
- canvas模擬彈幕效果程式碼例項Canvas
- canvas 四處亂竄的小球Canvas
- canvas動態時鐘效果Canvas
- js小球重力和碰撞效果JS
- canvas繪製小球漸隱漸現Canvas
- CSS3 實現兩端擺動的小球效果CSSS3
- canvas水平勻速運動效果Canvas
- canvas擺動效果程式碼例項Canvas
- canvas小球隨機漂浮碰撞程式碼例項Canvas隨機
- jquery小球碰撞效果程式碼例項jQuery
- JavaScript之多小球非對心彈性碰撞JavaScript
- canvas 碰撞反彈Canvas
- canvas繪製太陽系運動效果Canvas
- CSS3小球靜態環繞效果CSSS3
- canvas矩形拖拽效果Canvas
- canvas簽名效果Canvas
- canvas繪製圓盤走動鐘錶效果Canvas
- 【C語言練習題】小球反彈問題C語言
- js小球拋物線效果程式碼例項JS
- HTML5中canvas實現小球擊打小方塊遊戲HTMLCanvas遊戲
- canvas實現的隨機生成漂浮小球程式碼例項Canvas隨機
- canvas繪製滑鼠懸浮可以變大的立體小球Canvas
- canvas實現的圓形走動鐘錶效果Canvas
- js+canvas仿微信《彈一彈》小遊戲JSCanvas遊戲
- canvas繪製流星效果Canvas
- canvas實現波浪效果Canvas