-2

这是我的问题

1)我有动态y数组数据,使用该数组如何连续绘制波浪。

如果 Y 数组数据完成,则使用相同的 y 数组数据继续。

2)该数组值为143的声音自动播放。如果我停止,则不会停止。

这是我的代码:

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <canvas id="canvas" width="160" height="160" style="background-color: black;"></canvas>
        <script type='text/javascript'>


            var canvas = document.getElementById("canvas");
            var ctx = canvas.getContext("2d");
            ctx.fillStyle ="#dbbd7a";
            ctx.fill();

            var fps = 1000;
            var n = 0;

            var myAudio = new Audio("http://www-mmsp.ece.mcgill.ca/documents/AudioFormats/WAVE/Samples/AFsp/M1F1-uint8WE-AFsp.wav"); // buffers automatically when created
            myAudio.addEventListener('ended', function() {
                this.currentTime = 0;
                this.play();
            }, false);

            animate();
            function animate() {
                setTimeout(function () {
                    requestAnimationFrame(animate);
                    ctx.lineWidth="2";
                    ctx.strokeStyle='green';
                    ctx.clearRect(0, 0, canvas.width, canvas.height);

                // y axixs Data

                var Ydata = [
                            148,149,149,150,150,150,143,82,82,82,82,82,82,82,
                            148,149,149,150,150,150,143,82,82,82,82,82,82,82,
                            148,149,149,150,150,150,143,82,82,82,82,82,82,82,
                            148,149,149,150,150,150,143,82,82,82,82,82,82,82,
                            148,149,149,150,150,150,143,82,82,82,82,82,82,82,
                            148,149,149,150,150,150,143,82,82,82,82,82,82,82,
                            148,149,149,150,150,150,143,82,82,82,82,82,82,82,
                            148,149,149,150,150,150,143,82,82,82,82,82,82,82,
                            148,149,149,150,150,150,143,82,82,82,82,82,82,82,
                            148,149,149,150,150,150,143,82,82,82,82,82,82,82,
                          ];

                    // Drawing code goes here
                    n += 1.5;
                    if (n > 200) {
                        n = 0;
                    }
                    ctx.beginPath();
                    for (var x = 0; x < n; x++) {
                        if(Ydata[x] == '143')
                           myAudio.play();

                        ctx.lineTo(x, Ydata[x]);
                    }
                ctx.stroke();

            }, 1000/fps);
        }

        function stopAlarm()
        {
            myAudio.pause();
            myAudio.currentTime = 0;

        }

        </script>
        <button type="button" name="Stop" id="Stop" onclick="stopAlarm();">Stop Alarm</button>
    </body>
</html>
4

1 回答 1

0

我不太清楚你要做什么,但是快速检查你的代码,我会发现一个错误,如果 n 可以是 199,并且 Ydata 中有 140 个元素,在某些时候你可能会遇到异常,

如果您尝试在某种类型的循环中使用“动画”,您应该尝试通过 setInterval 更改 setTimeOut:

http://www.w3schools.com/jsref/met_win_setinterval.asp

于 2013-10-10T07:49:48.230 回答