Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
* { margin:0; padding:0; } canvas { display:block; }
JavaScript
var canvas, ctx, width, height, stems, bubbles; stems = []; bubbles = []; function Bubble(x, y, radius) { this.x = x; this.y = y; this.radius = radius; this.vy = -Math.random() * 5; this.opacity = 0.2 + Math.random() * 0.5; this.oldY = y; } Bubble.prototype.draw = function() { var strokeColor, fillColor; strokeColor = 'rgba(255, 255, 255,' + this.opacity + ')'; fillColor = 'rgba(255, 255, 255,' + (this.opacity / 2) + ')'; ctx.save(); ctx.lineWidth = 0.8; ctx.strokeStyle = strokeColor; ctx.fillStyle = fillColor; ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true); ctx.closePath(); ctx.fill(); ctx.stroke(); ctx.restore(); } function Stem(points, color) { this.points = points; this.color = color; } Stem.prototype.draw = function(ctx) { var len, ctrlPoint, point; len = this.points.length - 1; ctrlPoint = {x: 0, y: 0}; ctx.save(); ctx.strokeStyle = this.color; ctx.beginPath(); ctx.moveTo(this.points[this.points.length - 1].x, this.points[this.points.length - 1].y); for (var i = len; i >= 1; i--) { point = this.points[i]; ctrlPoint.x = (point.x + this.points[i - 1].x) / 2; ctrlPoint.y = (point.y + this.points[i - 1].y) / 2; ctx.quadraticCurveTo(point.x, point.y, ctrlPoint.x, ctrlPoint.y); ctx.lineWidth = i * 1.1; ctx.stroke(); ctx.fillStyle = 'red'; } ctx.restore(); } init(); function init(){ canvas = document.querySelector('canvas'); ctx = canvas.getContext('2d'); width = canvas.width = window.innerWidth; height = canvas.height = window.innerHeight; populateStems(height / 3, width, 25); generateBubbles(50); drawFrame(); }; function generateBubbles(bubblesLimit) { for (var i = 0; i <= bubblesLimit; i++) { bubbles.push(new Bubble(Math.random() * width, height + Math.random() * height / 2, 2 + Math.random() * 2)); } } function populateStems(offset, limit, step) { for (var x = 0; x <= limit; x += step) { generateStem(x, height / 2 - offset / 2 + Math.random() * offset, 50) } } function generateStem(x, pointsLen, step) { var positions, y, offset, colorsArr, color; colorsArr = ['#6e881b', '#5d7314', '#54690f', '#657f0f', '#6f8f06']; color = Math.floor(1 + Math.random() * colorsArr.length - 1); positions = []; if (height < 600) { offset = -40 + Math.random() * 80; for (y = height - pointsLen; y <= height + 100; y += step / 2) { positions.push({x: x + offset / (y / 2000), y: y, angle: Math.random() * 360, speed: 0.1 + Math.random() * 0.3}); } } else { offset = -100 + Math.random() * 200; for (y = height - pointsLen; y <= height + 100; y += step) { positions.push({x: x + offset / (y / 2000), y: y, angle: Math.random() * 360, speed: 0.1 + Math.random() * 0.3}); } } stems.push(new Stem(positions, colorsArr[color])); } function drawFrame() { window.requestAnimationFrame(drawFrame, canvas); ctx.fillStyle = '#17293a'; ctx.fillRect(0, 0, width, height); bubbles.forEach(moveBubble); stems.forEach(function(stem) { stem.points.forEach(movePoint); stem.draw(ctx); }); } function moveBubble(bubble) { if (bubble.y + bubble.radius <= 0) bubble.y = bubble.oldY; bubble.y += bubble.vy; bubble.draw(ctx); } function movePoint(point, index) { point.x += Math.sin(point.angle) / (index / 2.2); point.angle += point.speed; }
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title> Canvas深海动画-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号