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小球拋物線運動Canvas
- 小球彈彈樂
- canvas小球亂撞Canvas
- css3小球上下移動效果CSSS3
- js小球重力和碰撞效果JS
- canvas動態時鐘效果Canvas
- canvas繪製小球漸隱漸現Canvas
- canvas水平勻速運動效果Canvas
- JavaScript之多小球非對心彈性碰撞JavaScript
- CSS3小球靜態環繞效果CSSS3
- canvas模擬eharts首頁動畫效果Canvas動畫
- canvas矩形拖拽效果Canvas
- canvas簽名效果Canvas
- canvas繪製圓盤走動鐘錶效果Canvas
- SpringAnimator彈簧聯動效果的實現Spring
- 【C語言練習題】小球反彈問題C語言
- canvas蔚藍星空效果Canvas
- canvas繪製流星效果Canvas
- canvas實現波浪效果Canvas
- 讓View具有彈性效果的動畫——SpringAnimationView動畫Spring
- 小球運動-常規1
- HTML5中canvas實現小球擊打小方塊遊戲HTMLCanvas遊戲
- canvas設定陰影效果Canvas
- canvas圓形時鐘效果Canvas
- canvas簡單畫板效果Canvas
- canvas繪製風車效果Canvas
- canvas環形進度條效果Canvas
- canvas實現 漂亮的下雨效果Canvas
- canvas立體效果的圓環Canvas
- canvas漫天飛雪效果程式碼Canvas
- canvas具有漸變效果的矩形Canvas
- Canvas系列之一——濾鏡效果Canvas
- canvas透明度的矩形效果Canvas
- js+canvas仿微信《彈一彈》小遊戲JSCanvas遊戲
- 前端實現彈幕效果的方法總結(包含css3和canvas的實現方式)前端CSSS3Canvas