canvas实现爱心和彩虹雨效果
网络编程 2021-07-04 19:20www.168986.cn编程入门
本文主要介绍了canvas实现爱心和彩虹雨效果的实例,具有很好的参考价值。狼蚁网站SEO优化跟着长沙网络推广一起来看下吧
效果图
代码如下
<!doctype html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <canvas id="canvas"></canvas> <script> var canvas = document.getElementById('canvas'), ctx = canvas.getContext('2d'), canvasW = canvas.width = window.innerWidth, canvasH = canvas.height = window.innerHeight, canvasWHalf = canvasW / 2, canvasHHalf = canvasH / 2, xoff = canvasWHalf - 306, yoff = 50, bg = '00061a', id = 0, raindrops = [], minSize = 1, maxSize = 4, minSpeed = 5, maxSpeed = 20, minHue = 0, maxHue = 360, maxAmount = 50; function random(min, max) { if (arguments.length < 2) { max = min; min = 0; } return Math.floor(Math.random() (max - min) + min); } function hexToRGB(hex, opacity) { var rgb = ''; hex.match(/.{2}/g).forEach(function(n) { rgb += (parseInt(n, 16)) + ','; }); return 'rgba(' + rgb + opacity + ')'; } function draw() { // Heart ctx.fillStyle = hexToRGB(bg, '0.1'); ctx.beginPath(); // Left half ctx.moveTo(0, 0); ctx.lineTo(canvasWHalf, 0); ctx.lineTo(304 + xoff, 97 + yoff); ctx.bezierCurveTo(282 + xoff, -5 + yoff, 80 + xoff, -6 + yoff, 76 + xoff, 165 + yoff); ctx.bezierCurveTo(74 + xoff, 251 + yoff, 184 + xoff, 300 + yoff, 304 + xoff, 447 + yoff); ctx.lineTo(canvasWHalf, canvasH); ctx.lineTo(0, canvasH); // Right half ctx.moveTo(canvasW, 0); ctx.lineTo(canvasWHalf, 0); ctx.lineTo(304 + xoff, 97 + yoff); ctx.bezierCurveTo(326 + xoff, 5 + yoff, 528 + xoff, 6 + yoff, 532 + xoff, 165 + yoff); ctx.bezierCurveTo(534 + xoff, 251 + yoff, 424 + xoff, 300 + yoff, 304 + xoff, 447 + yoff); ctx.lineTo(canvasWHalf, canvasH); ctx.lineTo(canvasW, canvasH); ctx.closePath(); ctx.fill(); // Raindrops for (var i = 1; i < id; i++) { raindrops[i].fall(); }; } var Raindrop = function() { id++; this.y = random(-canvasH); this.x = random(canvasW); this.size = random(minSize, maxSize); this.speed = random(minSpeed, maxSpeed); this.color = 'hsl(' + random(minHue, maxHue) + ',100%,55%)'; this.origColor = this.color; this.id = id; raindrops[id] = this; }; Raindrop.prototype.fall = function() { this.y += this.speed; if (this.y >= canvasH) { this.y = random(-canvasH); this.x = random(canvasW); } ctx.save(); ctx.beginPath(); var gradient = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.size); gradient.addColorS(0, '#fff'); gradient.addColorS(0.5, this.color); gradient.addColorS(1, hexToRGB(bg, 0)); ctx.rect(this.x, this.y, this.size, maxSpeed); ctx.fillStyle = gradient; ctx.fill(); ctx.closePath(); ctx.restore(); }; (function init() { ctx.fillStyle = '#' + bg; ctx.fillRect(0, 0, canvasW, canvasH); for (var i = 0; i < maxAmount; i++) { new Raindrop(); } }()); function animate() { draw(); window.requestAnimationFrame(animate); } window.requestAnimationFrame(animate); function mouseTrail(x, y) { ctx.save(); ctx.globalCompositeOperation = 'overlay'; ctx.fillStyle = 'rgba(255,255,255,0.1)'; ctx.arc(x, y, 50, 0, Math.PI 2); ctx.fill(); ctx.restore(); } window.addEventListener('mousemove', function(cursor) { mouseTrail(cursor.x, cursor.y); }); </script> </body> </html>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望多多支持狼蚁SEO!
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程