Skip to content

Commit

Permalink
Added support for getting at the normalized bar data. Useful for thos…
Browse files Browse the repository at this point in the history
…e of us who don't want to use images, but do want the data to render to canvas, etc.
  • Loading branch information
drone1 committed Aug 7, 2018
1 parent 5802afd commit 0b7d776
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
})
}
18 changes: 18 additions & 0 deletions test/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)

0 comments on commit 0b7d776

Please sign in to comment.