canvas實現的彈力小球效果程式碼例項
分享一段程式碼例項,它實現了彈力小球效果。
點選滑鼠可以建立一個小球,然後具有彈力效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> body { padding: 0; margin: 0; } </style> </head> <body> <div> <canvas id='canvas' style="background-color:#333"></canvas> </div> <script> var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'); var x1, y1, x2, y2, paused = false, suzu = [], collarg = 0.8; //彈力系數; canvas.width = window.innerWidth; canvas.height = window.innerHeight; canvas.onmousedown = function(e) { x1 = e.clientX; y1 = e.clientY; paused = true; } function draw() { if (paused) { context.beginPath(); context.moveTo(x1, y1); context.lineTo(x2, y2); context.strokeStyle = "#fff" context.stroke(); } } canvas.onmousemove = function(e) { x2 = e.clientX; y2 = e.clientY; if (paused) { draw(); } } canvas.onmouseup = function() { paused = false; var ball = new createCircle(x2, y2, (x1 - x2) / 6, (y1 - y2) / 6); suzu.push(ball); if (suzu.length > 100) { suzu.shift(); } console.log(ball) } loop(); function loop() { context.clearRect(0, 0, canvas.width, canvas.height) draw(); suzu.forEach(function(circle) { circle.draw(context); circle.update(); }) requestAnimationFrame(loop); } function createCircle(x, y, speedx, speedy) { this.radius = Math.random() * 10 + 10; this.x = x; this.y = y; this.color = (Math.random() * 360).toFixed(0); this.vX = speedx; this.vY = speedy; this.draw = function(c) { c.beginPath(); c.arc(this.x, this.y, this.radius - 1, 0, Math.PI * 2); c.fillStyle = 'hsla(' + this.color + ',100%,60%,1)'; c.fill(); c.strokeStyle = 'hsla(0,100%,100%,0.6)'; c.stroke(); c.beginPath(); c.arc(this.x, this.y, 5, 0, Math.PI * 2); c.fillStyle = 'hsla(0,100%,100%,0.6)'; c.fill(); } this.update = function() { this.vY += 0.5; this.y += this.vY; if (this.vX > 0) { this.vX -= 0.05; if (this.vX < 0) { this.vX = 0; } } else { this.vX += 0.05; if (this.vX > 0) { this.vX = 0; } } this.x += this.vX; if (this.y > (canvas.height - this.radius) || this.y < this.radius) { this.y = this.y < this.radius ? this.radius : canvas.height - this.radius; this.vY = -this.vY * collarg; } if (this.x > (canvas.width - this.radius) || this.x < this.radius) { this.x = this.x < this.radius ? this.radius : canvas.width - this.radius; this.vX = -this.vX * collarg; } } } canvasMj() function canvasMj() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; } window.addEventListener('resize', function() { canvasMj(); }) </script> </body> </html>
相關文章
- canvas載入效果程式碼例項Canvas
- canvas原型鐘錶效果程式碼例項Canvas原型
- canvas氣泡上浮效果程式碼例項Canvas
- canvas繪製箭頭效果程式碼例項Canvas
- canvas小球碰壁效果Canvas
- 奇妙的canvas:彈跳小球Canvas
- html實現簡單ListViews效果的例項程式碼HTMLView
- canvas小球擺動效果Canvas
- canvas刮刮樂程式碼例項Canvas
- canvas繪製扇形程式碼例項Canvas
- canvas繪製網格程式碼例項Canvas
- jQuery tab選項卡效果程式碼例項jQuery
- CSS橢圓效果程式碼例項CSS
- Canvas實現放大鏡效果完整案例分析(附程式碼)Canvas
- canvas繪製機器貓程式碼例項Canvas
- canvas繪製拋物線程式碼例項Canvas線程
- canvas translate()、scale()和rotate()方法程式碼例項Canvas
- flex彈性佈局程式碼例項Flex
- 美化滾動條效果程式碼例項
- css3水滴效果程式碼例項CSSS3
- css背景虛化效果程式碼例項CSS
- canvas繪製圓形鐘錶程式碼例項Canvas
- canvas實現波浪效果Canvas
- jQuery點選滑出層效果程式碼例項jQuery
- css3晃動效果程式碼例項CSSS3
- CSS3心形效果程式碼例項CSSS3
- div前後翻轉效果程式碼例項
- 當前文字框高亮效果程式碼例項
- CSS3旋轉效果程式碼例項CSSS3
- 淡入淡出效果簡單程式碼例項
- canvas實現 漂亮的下雨效果Canvas
- 180行JavaScript程式碼實現的小球隨機移動程式碼JavaScript隨機
- 前端實現彈幕效果的方法總結(包含css3和canvas的實現方式)前端CSSS3Canvas
- html5 canvas 實現光線沿不規則路徑運動例項程式碼HTMLCanvas
- jQuery實現的表格展開伸縮效果例項jQuery
- 小球彈彈樂
- CSS3滑過光束效果程式碼例項CSSS3
- CSS3小黃人效果程式碼例項CSSS3
- css3折角效果程式碼例項CSSS3