Permalink
Browse files

x2 and x3 sized badges support

Badge URL now supports "size" argument, with values of 1, 2 or 3. By
default 1 is considered, and values higher than 3 are considered as 3.

Example: http://npm.packagequality.com/badge/stdio.png?size=3
  • Loading branch information...
sgmonda committed Sep 17, 2016
1 parent 3412eec commit ab051781fc5c0317d506b13c27ce610507ae90bd
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
@@ -62,7 +62,8 @@ function serveBadge (request, response) {
if (!result) {
return response.status(403).send({error: 'package ' + packageName + ' not found'});
}
badges.compileBadge(packageName, (result.quality * 100).toFixed(2), function (error, png) {
var size = parseInt(request.query.size) || 1;
badges.compileBadge(packageName, (result.quality * 100).toFixed(2), size, function (error, png) {
if (error) {
return response.status(503).send({error: 'database not available'});
}
View
@@ -14,14 +14,23 @@ var MAX_PACKAGE_NAME = 19;
var RATING_PARAM = '$rating$';
var COLOR_PARAM = '$color$';
var SHIELDS_IO_URL = 'https://img.shields.io/badge/quality-' + RATING_PARAM + '-' + COLOR_PARAM + '.svg?';
var MAX_SIZE = 3;
// globals
var log = new Log(config.logLevel);
var badge = fs.readFileSync(__dirname + '/../app/img/badge2.png');
var stars = {};
for (var i = 0.5; i <= 5; i += 0.5) {
stars[i] = fs.readFileSync(__dirname + '/../app/img/stars' + i + '.png');
var BADGES_BY_SIZE = [];
var STARS_BY_SIZE = [];
[1, 2, 3].forEach(function (size) {
BADGES_BY_SIZE.push(readImage('badge2', size));
var stars = {};
for (var value = 0.5; value <= 5; value += 0.5) stars[value] = readImage('stars' + value, size);
STARS_BY_SIZE.push(stars);
});
function readImage (name, size) {
return fs.readFileSync(__dirname + '/../app/img/' + name + '_x' + size + '.png');
}
var ranges = [
[0, 1],
[12, 25],
@@ -62,29 +71,32 @@ function getStarsCount(score) {
return '5';
}
function getStarsImage(score) {
return stars[getStarsCount(score)];
function getStarsImage(score, size) {
return STARS_BY_SIZE[size - 1][getStarsCount(score)];
}
exports.compileBadge = function (name, quality, callback) {
exports.compileBadge = function (name, quality, size, callback) {
size = size || 1;
if (size > MAX_SIZE) size = MAX_SIZE;
if (size < 1) size = 1;
if (name.length > MAX_PACKAGE_NAME) {
name = name.slice(0, MAX_PACKAGE_NAME - 3) + '...';
}
var canvas = new Canvas(256, 64);
var canvas = new Canvas(256 * size, 64 * size);
var context = canvas.getContext('2d');
var image = new Canvas.Image();
image.src = badge;
context.drawImage(image, 0, 0, 256, 64);
image.src = BADGES_BY_SIZE[size - 1];
context.drawImage(image, 0, 0, 256 * size, 64 * size);
context.font = '15px monospace';
context.font = (15 * size) + 'px monospace';
context.textAlign = 'right';
context.fillText(name, 244, 23);
context.fillText(name, 244 * size, 23 * size);
var starsImage = new Canvas.Image();
starsImage.src = getStarsImage(quality);
context.drawImage(starsImage, 155, 27, 92, 17);
starsImage.src = getStarsImage(quality, size);
context.drawImage(starsImage, 155 * size, 27 * size, 92 * size, 17 * size);
//canvas.toDataURL('image/png', callback);
canvas.toBuffer(callback);
@@ -168,4 +180,3 @@ if (__filename == process.argv[1])
{
exports.test(testing.show);
}

0 comments on commit ab05178

Please sign in to comment.