canvas小球擺動效果
下面是一個非常簡單的運動效果,類似於鐘錶一樣,小球可以左右擺動。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> html,body,canvas{ width:100vw; height:100vh; background:#000000; overflow:hidden; } </style> <script> setTimeout(() => { let canvas = document.querySelector('canvas'); let context = canvas.getContext('2d'); canvas.width = canvas.offsetWidth; canvas.height = canvas.offsetHeight; let pointer = { x: canvas.width / 2, y: canvas.height / 4 }; let angleInit = Math.PI / 6; let angle; let render = function () { context.clearRect(0, 0, canvas.width, canvas.height); // 小球 context.save(); context.fillStyle = "green"; context.translate(pointer.x, pointer.y); context.rotate(angle); context.beginPath(); context.arc(0, 200, 30, 0, 2 * Math.PI, false); context.fill(); context.strokeStyle = "gray"; context.lineWidth = 2; context.beginPath(); context.moveTo(0, 0); context.lineTo(0, 200); context.stroke(); context.restore(); }; let reverse = false; let run; (run = (time) => { if (!time) { angle = angleInit; } angle = angleInit * Math.cos(6 * time / 1000) render(); requestAnimationFrame(run) })(); }, 500); </script> </head> <body> <canvas></canvas> </body> </html>
相關文章
- canvas小球碰壁效果Canvas
- canvas小球碰壁隨機運動Canvas隨機
- canvas小球拋物線運動Canvas
- canvas小球亂撞Canvas
- css3小球上下移動效果CSSS3
- 奇妙的canvas:彈跳小球Canvas
- js小球重力和碰撞效果JS
- canvas動態時鐘效果Canvas
- canvas繪製小球漸隱漸現Canvas
- canvas水平勻速運動效果Canvas
- CSS3小球靜態環繞效果CSSS3
- canvas模擬eharts首頁動畫效果Canvas動畫
- canvas簽名效果Canvas
- canvas矩形拖拽效果Canvas
- canvas繪製圓盤走動鐘錶效果Canvas
- canvas實現波浪效果Canvas
- canvas蔚藍星空效果Canvas
- canvas繪製流星效果Canvas
- 小球運動-常規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
- canvas小畫板——(3)筆鋒效果Canvas
- 由canvas實現btn效果有感Canvas
- canvas繪製網格射線效果Canvas
- canvas載入效果程式碼例項Canvas
- canvas繪製卡通人臉形象效果Canvas
- canvas繪製多個圓圈效果Canvas
- canvas原型鐘錶效果程式碼例項Canvas原型