canvas繪製拋物線程式碼例項
分享一段程式碼例項,它實現了利用canvas繪製拋物線的功能。
點選頁面的一個點即可開始繪製過程。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> </head> <body> <canvas></canvas> <script> (function() { var W = innerWidth, H = innerHeight; var cvs = document.querySelector("canvas"), ctx = cvs.getContext("2d"), isCast = false; cvs.width = W; cvs.height = H; function Parabola(x0, y0, x1, y1, vx, a) { var x = x1 - x0, y = y1 - y0; this.x0 = x0; this.y0 = y0; this.x1 = x1; this.y1 = y1; this.a = a || 0.002; this.b = (y - this.a * x * x) / x; this._x = 0; this.vx = (((x1 - x0) >> 31) * 2 + 1) * vx; this.radius = 5; } Parabola.prototype.cast = function(ctx) { var x = (this._x += this.vx), y = this.a * x * x + this.b * x, isArrived = Math.abs(x + this.x0 - this.x1) < Math.abs(this.vx); ctx.beginPath(); isArrived ? ctx.arc(this.x1, this.y1, this.radius, 0, Math.PI * 2, true) : ctx.arc(x + this.x0, y + this.y0, this.radius, 0, Math.PI * 2, true); ctx.closePath(); ctx.stroke(); return !isArrived; }; document.addEventListener("click", function() { if (isCast) return; var x0 = Math.random() * W, y0 = Math.random() * H, x1 = Math.random() * W, y1 = Math.random() * H, radius = 5, parabola = new Parabola(x0, y0, x1, y1, 5); isCast = true; ctx.fillStyle = "red"; ctx.beginPath(); ctx.arc(x0, y0, radius, 0, Math.PI * 2, true); ctx.closePath(); ctx.fill(); ctx.fillStyle = "blue"; ctx.beginPath(); ctx.arc(x1, y1, radius, 0, Math.PI * 2, true); ctx.closePath(); ctx.fill(); function render() { if (parabola.cast(ctx)) { requestAnimationFrame(render); } else { isCast = false; } } render(); }, false); })(); </script> </body> </html>
相關文章
- canvas繪製扇形程式碼例項Canvas
- canvas繪製網格程式碼例項Canvas
- canvas繪製機器貓程式碼例項Canvas
- canvas繪製箭頭效果程式碼例項Canvas
- canvas繪製圓形鐘錶程式碼例項Canvas
- css繪製圓形程式碼例項CSS
- canvas小球拋物線運動Canvas
- SVG拖動繪製矩形程式碼例項SVG
- canvas繪製直線Canvas
- canvas 繪製線條Canvas
- canvas刮刮樂程式碼例項Canvas
- canvas載入效果程式碼例項Canvas
- canvas 繪製雙線技巧Canvas
- canvas原型鐘錶效果程式碼例項Canvas原型
- canvas氣泡上浮效果程式碼例項Canvas
- CSS3繪製太極圖程式碼例項詳解CSSS3
- canvas繪製sin正弦曲線Canvas
- canvas translate()、scale()和rotate()方法程式碼例項Canvas
- 小程式拋物線動畫動畫
- UML類圖繪製例項
- 例項QT程式 —— Qt自繪製小時鐘QT
- canvas繪製網格射線效果Canvas
- E-R圖繪製例項
- 資料流圖繪製例項
- Python例項:僅繪製圖例而不繪製實際的圖形Python
- canvas核心技術-如何繪製線段Canvas
- canvas繪製經典星空連線效果Canvas
- canvas 繪製扇形Canvas
- canvas 繪製文字Canvas
- canvas 繪製矩形Canvas
- table細線表格例項程式碼
- 小程式canvas居中剪裁繪製圖片Canvas
- canvas 繪製圓形Canvas
- canvas繪製流星效果Canvas
- canvas lineWidth 繪製原理Canvas
- canvas lineWidth繪製原理Canvas
- 原生Canvas繪製餅圖,我是不是被騙程式碼了Canvas
- canvas繪製帶有漸變效果的直線Canvas