我需要呈现N项,它们之间是x像素宽,N-1空间y像素宽。
我需要计算多少项目将在屏幕上冷杉。我要做的是把一系列的振幅作为列。这是我的测试产品,没有空格:

我首先计算出合适的项数,然后重新对原始数组进行重采样,以得到屏幕上合适的值的确切数量。这与每列5个像素相同,您可以看到原始数组已被重放:

我现在使用的公式是no_items = Math.floor(canvasWidth/(barWidth+spaceWidth))
/**
* Recalculates waveform data for given width of canvas
* @param {number} canvasWidth Image width in pixels
* @param {number} barWidth Width of waveform bars
* @param {number} spaceWidth Width of spaces between bars
* @returns {Uint8Array} recalculated bytes
*/
recalculateForWidth(canvasWidth, barWidth, spaceWidth) {
const no_items = Math.floor(canvasWidth/(barWidth+spaceWidth))
const result = new Uint8Array(no_items);
// Resampling code follows
...
return result;
}但这显然假设在数据末尾有一个额外的空间--它只是简单地计算项目宽度为条形width+空间宽度。
我想在最后呈现没有空间的条形图,那么我如何计算出有多少条会与空格相适应,而在最后却没有空间呢?
发布于 2018-03-25 00:01:47
您的总宽度将是:
barWidth(NumberBars) + spaceWidth(NumberBars - 1) = TotalWidth它扩展到:
barWidth(NumberBars) + (spaceWidth)(NumberBars) - spaceWidth = TotalWidth将spaceWidth添加到双方:
barWidth(NumberBars) + spaceWidth(NumberBars) = TotalWidth + spaceWidth左侧因素:
NumberBars(barWidth + spaceWidth) = TotalWidth + spaceWidth并划分:
NumberBars = (TotalWidth + spaceWidth) / (barWidth + spaceWidth)发布于 2018-03-24 23:55:09
为了简化,让我们举一个小例子。假设你有103个像素,你想要5像素宽的条形图和2像素之间的空格。就像你说的,比空格多一个酒吧。您希望最后一条位于屏幕的末尾,所以请将宽度减少这个值,即98 by。
然后,剩下的宽度将除以成对的条形空间,每一条空间的总长度为barWidth + spaceWidth。也就是说,98/7 = 14对。总共有15间酒吧。
所以,你的公式应该是
(canvasWidth - barWidth) / (barWidth + spaceWidth) + 1最后+1代表最后一条。
希望能帮你解决这个问题。
https://stackoverflow.com/questions/49471082
复制相似问题