3

抱歉,这会有点长...

一点上下文

作为一个更大项目的一部分,我正在尝试制作一个项目时间表以包含在用于远程控制音乐制作程序的网页中(对于那些感兴趣的人来说,称为 Reaper)。我试图让它显示当前播放位置、项目标记和项目区域。这些都是直接从程序的 API 提供的,获取信息没有问题。对于初学者,我只是想显示项目标记,但是我已经拉了一个多星期的头发,现在试图让它工作。

这是软件内部的一个快速屏幕截图,以说明我要模拟的内容:Reaper Ruler screencap

通常我会为这样的事情使用进度条,或者使用来自网络的数千个示例之一,但是我无法知道项目的长度,因为软件不限制它。结果,我重新使用了每条 10 像素的固定比例。有点随意,我选择了它,因为它最适合 120 bpm 的 5 分钟歌曲。暂时不用太担心外观,我只是想让它工作哈哈。

我遇到的问题(代码包含在底部)是因为我对标记使用绝对定位以便将它们全部从屏幕左侧对齐,它们是从文档流中拉出的,所以我不能将它们包装在父 div 中。最后,我打算使用滚动条将父 div 设置为 80% 宽度以查看其余标记,所以显然我做错了。但是,我似乎找不到任何与我想要实现的目标相似的代码片段。

所以这是实际的问题:

我应该使用哪种显示/位置/浮动 CSS 来代替position: absoluteand float: left?如果我需要 JS 来做,那我该怎么做呢?

感谢您能给我带来的任何帮助,无论是实际代码还是朝着正确方向轻推!


这是我的(相关)代码:

索引.html

<html>
    <body>
        <div id="timeline">
            <div id="labels"></div>
            <div id="markers"></div>
        </div>
    </body>
</html>

脚本.js:

// hardcoded for debugging purposes
// See section below about the API for info about how I get this data
var markers = [
    {label: "Start", pos: "20.00000000000000"},
    {label: "First", pos: "50.00000000000000"},
    {label: "Second", pos: "200.00000000000000"},
    {label: "Last", pos: "576.845412000000000"}
];

function draw_markers(marker_list) {
    var label_html = "";
    var marker_html = "";

    $.each(marker_list, function(index, obj) {
        var label = obj.label;
        var offset = parseFloat(obj.pos) * 7; // obj.pos is mesured in bars, not px

        label_html = label_html +
                    "<span class='label' style='margin-left:"+offset+"px'>" +
                    label + "</span>";

        marker_html = marker_html +
                    "<span class='marker' style='margin-left:"+offset+"px'>|</span>";
    });

    document.getElementById("labels").innerHTML = label_html;
    document.getElementById("markers").innerHTML = marker_html;
}

draw_markers(markers);

样式.css:

    html, body {
    background: #eeeeee;
}

#timeline {
    height: 4em;
    width: 100%;
    background-color: #9E9E9E;
}

#labels {
    border-bottom: 1px solid black;
    height: 50%;
    width: 100%;
}

#markers {
    height: 50%;
    width: 100%;
}

.label {
    position: absolute;
    float: left;
}
.marker {
    position: absolute;
    float: left;
}

关于API

我们得到了一堆函数,它们定期轮询服务器并解析(明文)响应。典型的响应如下所示:

MARKERLIST_BEGINMARKER_LIST
MARKER \t label \t ID \t position
...
MARKER_LIST_END
TRANSPORT \t playstate \t position_seconds \t isRepeatOn \t position_string \t position_string_beats
...

使用 JS,我拆分每一行并使用 switch 语句来确定如何处理每一行。然后,我构建了一个全局数组,其中包含项目中的所有标记以及我需要的信息。

4

2 回答 2

1

您可以使用divs asdisplay: inline-block并将它们的宽度设置为重叠绝对时间线位置的增量的百分比:

function draw_markers(marker_list) {
    var label_html = "";
    var marker_html = "";
    var total = null;
    var prev = 0;

    $.each(marker_list, function(index, obj) {
        var delta = parseFloat(obj.pos) - prev;
        obj.delta = delta;
        prev += parseFloat(obj.pos);
        total += delta;

    })

    $.each(marker_list, function(index, obj) {
        var label = obj.label;

        var offset = parseFloat(obj.delta) / (total / 100); // obj.pos is mesured in bars, not px

        label_html = label_html +
                    "<div class='label' style='width:"+offset+"%'>" +
                    label + "</div>";

        marker_html = marker_html +
                    "<div class='marker' style='width:"+offset+"%'>|</div>";
    });

    document.getElementById("labels").innerHTML = label_html;
    document.getElementById("markers").innerHTML = marker_html;
}

draw_markers(markers);

CSS:

html, body {
    background: #eeeeee;
}

#timeline {
    height: 4em;
    width: 100%;
    background-color: #9E9E9E;
}

#labels {
    border-bottom: 1px solid black;
    height: 50%;
    width: 100%;
}

#markers {
    height: 50%;
    width: 100%;
}

.label {
    position: relative;
    display: inline-block;
}
.marker {
    position: relative;
    display: inline-block;
}
于 2018-06-05T20:21:08.503 回答
1

或者,您可以使用<canvas>Javascript 绘制时间线(这是我用于Song Switcher的网络界面的)。您还可以使用GetProjectLengthAPI 函数获取项目长度(例如,通过调用脚本将长度放入临时 extstate,然后从 Web 界面读取)。

function Timeline(canvas) {
  this.canvas  = canvas;
  this.ctx     = canvas.getContext('2d');
  this.length  = 0;
  this.markers = [];
}

Timeline.prototype.resize = function() {
  this.canvas.width  = this.canvas.clientWidth;
  this.canvas.height = this.canvas.clientHeight;
  
  this.scale = this.length / this.canvas.width;
}

Timeline.prototype.update = function() {
  this.resize();
  
  this.ctx.fillStyle = '#414141';
  this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
  
  this.ctx.textBaseline = 'hanging';
  
  for(var marker of this.markers)
    this.drawMarker(marker);
}

Timeline.prototype.drawMarker = function(marker) {
  const MARKER_WIDTH = 2;
  const FONT_SIZE    = 14;
  const PADDING      = MARKER_WIDTH * 2;
 
  var xpos = this.timeToPx(marker.pos);

  this.ctx.strokeStyle = this.ctx.fillStyle = 'red';
  this.ctx.lineWidth   = MARKER_WIDTH;

  this.ctx.beginPath();
  this.ctx.moveTo(xpos, 0);
  this.ctx.lineTo(xpos, this.canvas.height);
  this.ctx.stroke();

  if(marker.name.length > 0) {
    this.ctx.font = `bold ${FONT_SIZE}px sans-serif`;

    var boxWidth = this.ctx.measureText(marker.name).width + PADDING;
    this.ctx.fillRect(xpos, 0, boxWidth, FONT_SIZE + PADDING);

    this.ctx.fillStyle = 'white';
    this.ctx.fillText(marker.name, xpos + MARKER_WIDTH, PADDING);
  }
}

Timeline.prototype.timeToPx = function(time) {
  return time / this.scale;
}

var timeline = new Timeline(document.getElementById('timeline'));
timeline.length  = 30; // TODO: Fetch using GetProjectLength
timeline.markers = [
  {pos:  3, name: "Hello"},
  {pos: 15, name: "World!"},
  {pos: 29, name: ""},
];
timeline.update();

window.addEventListener('resize', timeline.update.bind(timeline));
#timeline {
  height: 50px;
  line-height: 50px;
  image-rendering: pixelated;
  width: 100%;
}
<canvas id="timeline"></canvas>

于 2018-06-17T05:50:43.773 回答