溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

canvas怎么實(shí)現(xiàn)愛心和彩虹雨的效果

發(fā)布時(shí)間:2020-10-23 15:26:47 來源:億速云 閱讀:131 作者:小新 欄目:web開發(fā)

小編給大家分享一下canvas怎么實(shí)現(xiàn)愛心和彩虹雨的效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

代碼如下:

<!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.addColorStop(0, '#fff');
 gradient.addColorStop(0.5, this.color);
 gradient.addColorStop(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>

以上是canvas怎么實(shí)現(xiàn)愛心和彩虹雨的效果的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI