From 0b7d7767f5c278550803901bc6fac83734fbd05f Mon Sep 17 00:00:00 2001 From: Jon Lippincott Date: Tue, 7 Aug 2018 00:33:54 -0400 Subject: [PATCH] Added support for getting at the normalized bar data. Useful for those of us who don't want to use images, but do want the data to render to canvas, etc. --- lib/render.js | 6 ++++++ test/render.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/render.js b/lib/render.js index 1db7cf7..8105064 100644 --- a/lib/render.js +++ b/lib/render.js @@ -120,9 +120,11 @@ module.exports = function render (url, options, cb) { } var maxValue = Math.max.apply(null, vals) + var normalizedVals = []; vals.forEach(function (val, index) { var scale = options.height / maxValue + normalizedVals.push(val / maxValue); val *= scale var w = options.barWidth if (options.barGap !== 0) { @@ -160,5 +162,9 @@ module.exports = function render (url, options, cb) { canvasContext.fillRect(0, options.baseline - (options.baselineWidth / 2), options.width, options.baselineWidth) } cb(null, canvas.toBuffer()) + + if (options.normalizedValuesCallback) { + options.normalizedValuesCallback(normalizedVals); + } }) } diff --git a/test/render.js b/test/render.js index 2e38c82..7a6d9cf 100644 --- a/test/render.js +++ b/test/render.js @@ -5,3 +5,21 @@ render('Canon.mp3', function (err, buffer) { if (err) return err fs.writeFileSync('out.png', buffer) }) + +var height = 256; +render('Canon.mp3', { + barWidth: 1, + baselineWidth: 0, + baseline: height / 2, + padding: 2, + width: height * 4, + height: height, + + normalizedValuesCallback: function(normalizedVals) { + console.log(normalizedVals) + } + }, + function(err, buffer) { + if (err) return err + } +) \ No newline at end of file