我需要渲染像素宽的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;
}
但这显然假设数据末尾有一个额外的空间 - 它只是计算项目宽度为条宽 + 空间宽度。
我想在最后渲染没有空格的条,那么我如何计算有多少条适合空格但最后没有空格?