You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1000 B

/**
* Module dependencies.
*/
var Canvas = require('../lib/canvas')
, canvas = new Canvas(40, 15)
, ctx = canvas.getContext('2d')
, fs = require('fs');
Object.defineProperty(Array.prototype, 'max', {
get: function(){
var max = 0;
for (var i = 0, len = this.length; i < len; ++i) {
var n = this[i];
if (n > max) max = n;
}
return max;
}
});
function spark(ctx, data) {
var len = data.length
, pad = 1
, width = ctx.canvas.width
, height = ctx.canvas.height
, barWidth = width / len
, max = data.max;
ctx.fillStyle = 'rgba(0,0,255,0.5)';
ctx.strokeStyle = 'red';
ctx.lineWidth = 1;
data.forEach(function(n, i){
var x = i * barWidth + pad
, y = height * (n / max)
ctx.lineTo(x, height - y);
ctx.fillRect(x, height, barWidth - pad, -y);
});
ctx.stroke();
}
spark(ctx, [1,2,4,5,10,4,2,5,4,3,3,2]);
canvas.toBuffer(function(err, buf){
if (err) throw err;
fs.writeFile(__dirname + '/spark.png', buf);
});