Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve Piecon's performance #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Piecon.setProgress(12);
Piecon.setProgress(25);
...
Piecon.reset();
...
Piecon.destroy(); // If you care about performance and use `useCache` option, use it instead of `Piecon.reset();`
```

### Options
Expand All @@ -26,7 +28,8 @@ Piecon.setOptions({
color: '#ff0084', // Pie chart color
background: '#bbb', // Empty pie chart color
shadow: '#fff', // Outer ring color
fallback: false // Toggles displaying percentage in the title bar (possible values - true, false, 'force')
fallback: false, // Toggles displaying percentage in the title bar (possible values - true, false, 'force')
useCache: false // If you want to keep animation with long-loop, make it true can give you high-performance
});
```

Expand Down
22 changes: 22 additions & 0 deletions example/loop.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<head>
<title>Piecon / Pie charts in your favicon!</title>
<link rel="icon" href="favicon.ico" />
<script src="../piecon.js"></script>
<script>
(function(){
var count = 0;
Piecon.setOptions({fallback: 'force', useCache: true});
setInterval(function(){
if (++count > 100) { count = 0; }
Piecon.setProgress(count);
}, 250);
})();
</script>
</head>
<body>
<div id="container">
<h1>Piecon</h1>
<p>A <a href="https://github.com/lipka">@lipka</a> production</p>
</div>
</body>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "piecon",
"version": "0.5.0",
"version": "0.5.1",
"description": "Dynamic pie charts in your favicon",
"author": "Lukas Lipka <lukaslipka@gmail.com> (http://lukaslipka.com)",
"main": "./piecon.js",
Expand Down
20 changes: 17 additions & 3 deletions piecon.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
var originalFavicon = null;
var originalTitle = null;
var canvas = null;
var dataUrlCache = {};
var options = {};
var defaults = {
color: '#ff0084',
Expand Down Expand Up @@ -74,7 +75,7 @@

var getCanvas = function () {
if (!canvas) {
canvas = document.createElement("canvas");
canvas = document.createElement('canvas');
if (isRetina) {
canvas.width = 32;
canvas.height = 32;
Expand All @@ -88,8 +89,15 @@
};

var drawFavicon = function(percentage) {

if (options.useCache) {
if (percentage in dataUrlCache) {
return setFaviconTag(dataUrlCache[percentage]);
}
}

var canvas = getCanvas();
var context = canvas.getContext("2d");
var context = canvas.getContext('2d');

percentage = percentage || 0;

Expand Down Expand Up @@ -120,7 +128,8 @@
context.fill();
}

setFaviconTag(canvas.toDataURL());
dataUrlCache[percentage] = canvas.toDataURL();
setFaviconTag(dataUrlCache[percentage]);
}
};

Expand Down Expand Up @@ -177,6 +186,11 @@
}
};

Piecon.destroy = function(){
Piecon.reset();
dataUrlCache = {};
};

Piecon.setOptions(defaults);

if(typeof define === 'function' && define.amd) {
Expand Down
2 changes: 1 addition & 1 deletion piecon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.