首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过SoundCloud的waveform.js绘制fillRect不起作用

通过SoundCloud的waveform.js绘制fillRect不起作用
EN

Stack Overflow用户
提问于 2014-11-16 08:08:23
回答 1查看 315关注 0票数 0

我正在尝试使用在http://www.waveformjs.org上找到的waveform.js来绘制SoundCloud轨迹的波形。画布元素已成功创建,但fillRect未显示。我将其与直接创建canvas元素和fillRect进行了对比,这两种方法都有效。

下面是HTML:

代码语言:javascript
复制
<!DOCTYPE html>
 <html>
   <head>
       <meta charset="UTF-8">
       <title>Sample document</title>
   </head>
   <body>
        <div id="example1"></div>
        <hr>
        <div id="example2"></div>
   </body>
       <script src="waveform.js"></script>
       <script src="script.js"></script>
 </html>

下面是script.js:

代码语言:javascript
复制
var canvas, container;

// Creating canvas element and rect in example1
canvas = document.createElement("canvas");
canvas.setAttribute("id", "canvas");
container = document.getElementById("example1");
container.appendChild(canvas);
canvas.width = container.clientWidth;
canvas.height = container.clientHeight;
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
ctx.fillRect(20,20,50,100);
// Both created successfully

// Creating canvas element and rect in example2, through waveform object
var waveform = new Waveform({
    container: document.getElementById("example2")
});
// Canvas element created successfully, no rectangle

下面是waveform.js:

代码语言:javascript
复制
(function() {

    var Waveform;

    window.Waveform = Waveform = (function() {

        Waveform.name = 'Waveform';

        function Waveform(options) {
            this.container = options.container;
            this.canvas = this.createCanvas(this.container, this.container.clientWidth, this.container.clientHeight);
            this.context = this.canvas.getContext("2d");
            this.width = parseInt(this.context.canvas.width, 10);
            this.height = parseInt(this.context.canvas.height, 10);
            this.makeRect();
        };

        Waveform.prototype.createCanvas = function(container, width, height) {
          var canvas;
          canvas = document.createElement("canvas");
          canvas.setAttribute("id", "wfCanvas");
          container.appendChild(canvas);
          canvas.width = width;
          canvas.height = height;
          return canvas;
        };

        Waveform.prototype.makeRect = function() {
          var wfCanvas = document.getElementById("wfCanvas");
          var ctx = wfCanvas.getContext("2d");
          ctx.fillRect(20,20,50,100);
        }

        return Waveform;

    })();

}).call(this);
EN

回答 1

Stack Overflow用户

发布于 2014-11-20 23:25:54

布鲁,你已经做得太远了。

只需确保要将波形应用到的容器具有定义为css或内联的像素尺寸。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26952281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档