使用css实现类似语音输入动态水波纹效果

  • 实现的详细代码如下,复制可直接运行查看效果:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Microphone Recording Animation</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
        }

        .microphone-btn {
            border: none;
            background-color: transparent;
            cursor: pointer;
            position: relative;
            outline: none;
        }

        .microphone-icon {
            width: 50px;
            height: 50px;
            background-color: #333;
            border-radius: 50%;
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
            transition: background-color 0.3s;
        }

        .microphone-icon::before {
            content: '';
            width: 20px;
            height: 30px;
            background-color: #fff;
            border-radius: 10px 10px 0 0;
            position: absolute;
            top: 5px;
        }

        .microphone-icon::after {
            content: '';
            width: 10px;
            height: 10px;
            background-color: #fff;
            border-radius: 50%;
            position: absolute;
            bottom: 5px;
        }

        .wave {
            position: absolute;
            border: 4px solid #f00; /* 设置线的粗细 */
            border-radius: 50%;
            opacity: 0;
            animation: wave-animation 1.5s infinite;
        }

        .microphone-icon.recording .wave:nth-child(1) {
            animation-delay: 0s;
        }

        .microphone-icon.recording .wave:nth-child(2) {
            animation-delay: 0.5s;
        }

        .microphone-icon.recording .wave:nth-child(3) {
            animation-delay: 1s;
        }

        @keyframes wave-animation {
            0% {
                width: 50px;
                height: 50px;
                opacity: 1;
                border-color: rgba(255, 0, 0, 1);
            }
            100% {
                width: 150px;
                height: 150px;
                opacity: 0;
                border-color: rgba(255, 0, 0, 0);
            }
        }
    </style>
</head>
<body>
    <button id="startRecording" class="microphone-btn">
        <div class="microphone-icon recording">
            <div class="wave"></div>
            <div class="wave"></div>
            <div class="wave"></div>
        </div>
    </button>
    <script>
        document.getElementById('startRecording').addEventListener('click', function() {
            const microphoneIcon = document.querySelector('.microphone-icon');
            microphoneIcon.classList.toggle('recording');
        });
    </script>
</body>
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现录音水波纹动态效果的思路如下: 1. 使用HTML5的`<canvas>`标签创建画布,用于绘制水波纹效果。 2. 使用CSS3的`animation`属性,定义动画效果。 3. 使用JavaScript控制录音功能,并将录音数据传递给canvas绘制出水波纹效果。 下面是实现的详细步骤: 1. HTML代码 ``` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>录音水波纹动态效果</title> <style> #canvas { width: 300px; height: 300px; margin: 50px auto; display: block; background-color: #ccc; border-radius: 50%; overflow: hidden; } #canvas:after { content: ""; display: block; position: absolute; width: 100%; height: 100%; top: 0px; left: 0px; background-color: #fff; opacity: 0.5; z-index: 1; } #canvas:before { content: ""; display: block; position: absolute; width: 100%; height: 100%; top: 0px; left: 0px; background-image: radial-gradient(ellipse at center, #fff, #222); background-size: 100% 100%; z-index: 2; } </style> </head> <body> <canvas id="canvas"></canvas> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </body> </html> ``` 2. CSS代码 在CSS样式中,我们为`<canvas>`标签设置了宽度、高度和边框半径等属性,用于显示圆形的水波纹效果。同时,我们使用了`overflow: hidden`属性,以隐藏画布中超出边界的部分。 ``` #canvas { width: 300px; height: 300px; margin: 50px auto; display: block; background-color: #ccc; border-radius: 50%; overflow: hidden; } ``` 接着,我们使用`:before`和`:after`伪元素,分别绘制一个半透明的白色背景和一个黑色的圆形渐变背景。这些背景将作为水波纹的底部和顶部。 ``` #canvas:after { content: ""; display: block; position: absolute; width: 100%; height: 100%; top: 0px; left: 0px; background-color: #fff; opacity: 0.5; z-index: 1; } #canvas:before { content: ""; display: block; position: absolute; width: 100%; height: 100%; top: 0px; left: 0px; background-image: radial-gradient(ellipse at center, #fff, #222); background-size: 100% 100%; z-index: 2; } ``` 3. JavaScript代码 在JavaScript中,我们使用`<canvas>`标签的`getContext()`方法创建一个2D画布上下文,并定义一个`draw()`函数用于绘制水波纹效果。 ``` var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var audioContext = new AudioContext(); var analyser = audioContext.createAnalyser(); navigator.mediaDevices.getUserMedia({ audio: true }).then(function(stream) { var source = audioContext.createMediaStreamSource(stream); source.connect(analyser); analyser.connect(audioContext.destination); }); function draw() { var canvasWidth = canvas.width; var canvasHeight = canvas.height; var centerX = canvasWidth / 2; var centerY = canvasHeight / 2; var radius = canvasWidth / 3; var bars = 200; var barWidth = 2; var angle = Math.PI * 2 / bars; var dataArray = new Uint8Array(analyser.frequencyBinCount); analyser.getByteFrequencyData(dataArray); ctx.clearRect(0, 0, canvasWidth, canvasHeight); ctx.beginPath(); for (var i = 0; i < bars; i++) { var barHeight = dataArray[i] / 2; var x = centerX + Math.cos(angle * i) * (radius + barHeight); var y = centerY + Math.sin(angle * i) * (radius + barHeight); ctx.fillStyle = "rgba(0, 0, 0, 0.3)"; ctx.fillRect(x - barWidth / 2, y - barWidth / 2, barWidth, barHeight); ctx.save(); ctx.globalCompositeOperation = "lighter"; ctx.fillStyle = "rgba(255, 255, 255, 0.5)"; ctx.beginPath(); ctx.arc(x, y, 10, 0, Math.PI * 2, true); ctx.closePath(); ctx.fill(); ctx.restore(); } requestAnimationFrame(draw); } draw(); ``` 在`draw()`函数中,我们首先获取画布的宽度和高度,以及画布中心点的坐标和半径等信息。接着,我们使用`AudioContext`和`createAnalyser()`方法创建音频分析器,并在`getUserMedia()`方法中打开录音设备,并将录音数据传递给分析器。 在绘制水波纹时,我们使用`getByteFrequencyData()`方法获取分析器中的频率数据,并遍历数据数组,依次绘制出每个音频条的位置和高度。同时,我们使用`globalCompositeOperation`属性设置混合模式,实现水波纹效果。 最后,我们通过`requestAnimationFrame()`方法实现动态效果,并在函数结束时再次调用`draw()`函数,以保持动态效果的持续性。 以上就是实现录音水波纹动态效果的完整代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值