javascript - Javascript::Processing.js动画未加载

标签 javascript html audio processing

我有一个供用户输入的表格:

  <form>
  <textarea onfocus="clearContents(this);" id="chat" cols="50" rows="5">  </textarea>
   <button type="button" onclick="animate_song();">talk</button>
 </form>

我的想法是让输入能够动态触发音频和动画文件:
<audio id="audio" src=" "></audio>

<canvas id="animaton" data-processing-sources=" "></canvas>

<script src="scripts/processing.min.js"></script>
<script src="scripts/soundEngine.js"></script>

我将音频和动画引擎都放在下面的同一函数中。
(正在触发音频播放。)
function animate_song(){

     var id = Song.prototype.lyricsIntersect(input);

     document.getElementById("animation").setAttribute(
         "data-processing-sources", 'sketches' + '/' + id + '.' + 'pde');

     var playback = 'http://127.0.0.1:8000/audio' + '/' + id + '.wav';

     var request = new XMLHttpRequest();
     request.open("GET", playback, true);
     request.responseType = "blob";   

     request.onload = function() {
       if (this.status == 200) {
          var audio = document.getElementById("audio");
          var src = URL.createObjectURL(this.response);
          audio.src = src;
          audio.load();
          audio.play();
       }
     }
     request.send();
     };

但是只有预加载到html文档中时,此函数才能加载动画,如下所示:
   <canvas data-processing-sources="sketches/song.pde"></canvas>

为什么会这样,我做错了什么?

最佳答案

处理lib会自动查找<canvas>,在这种情况下,它找不到源。因此,必须在设置Processing.loadSketchFromSources属性的函数末尾使用<canvas>方法。

   function animate_song(){

      var id = Song.prototype.lyricsIntersect(input);

      var sketch = 'http://127.0.0.1:8000/sketches' + '/' + id + '.pde';

      var request = new XMLHttpRequest();
      request.open("GET", sketch, true);
      request.responseType = "blob";   

      request.onload = function() {
          if (this.status == 200) {
              var animation = document.getElementById("animation");
              var src = URL.createObjectURL(this.response);
              animation.src = src;
              animation.setAttribute("data-processing-sources", 'sketches' + '/' + id + '.' + 'pde');
          }
       }
       request.send();

       Processing.loadSketchFromSources('animation', ['sketches' + '/' + id + '.' + 'pde']);
    } 

关于javascript - Javascript::Processing.js动画未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36228181/

相关文章:

javascript - 制作视频比较 slider 全宽/高度

Javascript持久变量,在删除节点后存储节点

javascript - JQuery 淡入背景的原始 Javascript

javascript - 当我在键盘上按下 'z' 时,我希望它更改具有 sound1.wav 的键 "a"现在按下时播放 sound2.wav

javascript - 使用 Angular2 将 JSON 发布到 Node 服务器返回 404 URL 未找到

javascript - 如何使用RegisterStartupScript调用javascript警报框后停止vb代码执行

javascript - 警报();不使用 jquery 和 bootstrap

html - 水平线未显示在站点中文本之间的正确位置

java - 播放mp3文件后没有声音

swift - 确定哪个应用程序正在使用音频设备