Skip to content

Commit

Permalink
Merge pull request makeable#11 from dodo/master
Browse files Browse the repository at this point in the history
Expose Canvas Options & Add Notificon.reset method
  • Loading branch information
makeable committed Feb 17, 2012
2 parents d0f1912 + 2f27d9b commit 7e79436
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions notificon.js
Expand Up @@ -53,8 +53,12 @@ or implied, of Matt Williams.
options = {};
}
var defaults = {
font: "10px monospace",
color: "#000000",
stroke: "rgba(255,255,255,0.85)",
align: 'right',
valign: 'bottom',
width: 4,
favicon: getExistingFavicon()
};
for (var key in defaults) {
Expand Down Expand Up @@ -99,18 +103,26 @@ or implied, of Matt Williams.
document.getElementsByTagName('head')[0].appendChild(link);
};

var drawLabel = function drawLabel(canvas, label, color, stroke) {
var getCoords = function getCoords(options) {
return {
x: options.align.toLowerCase() === 'left' ? 0 : 16,
y: options.valign.toLowerCase() === 'top' ? 0 : 18
};
};

var drawLabel = function drawLabel(canvas, label, options) {
var context = canvas.getContext("2d");
context.font = "10px monospace";
context.fillStyle = color;
context.textAlign = 'right';
context.textBaseline = "top";
context.strokeStyle = stroke;
context.lineWidth = 4;
context.strokeText(label,16,6);
context.fillText(label,16,6);
var coords = getCoords(options);
context.font = options.font;
context.fillStyle = options.color;
context.textAlign = options.align;
context.textBaseline = options.valign;
context.strokeStyle = options.stroke;
context.lineWidth = options.width;
context.strokeText(label, coords.x, coords.y);
context.fillText(label, coords.x, coords.y);
};

var imgToCanvas = function imgToCanvas(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
Expand All @@ -126,7 +138,8 @@ or implied, of Matt Williams.
}

var options = mergeDefaultOptions(myOptions);


label = "" + label;
if (!label.length) {
return changeFavicon(options.favicon);
}
Expand All @@ -136,7 +149,7 @@ or implied, of Matt Williams.
img.onload = function() {
var canvas = imgToCanvas(img);
if (label) {
drawLabel(canvas, label, options.color, options.stroke);
drawLabel(canvas, label, options);
}
try {
return changeFavicon(canvas.toDataURL("image/png"));
Expand All @@ -155,11 +168,15 @@ or implied, of Matt Williams.
};
return true;
};

var Notificon = function(label, options) {
createNotificon(label, options);
};

Notificon.reset = function reset() {
removeNotificon();
};

if (typeof exports !== 'undefined') {
module.exports = Notificon;
} else {
Expand Down

0 comments on commit 7e79436

Please sign in to comment.