Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/icon.js

Issue 29334223: Issue 3532 - Generate animation images at runtime (Closed)
Patch Set: Rebased Created Jan. 23, 2016, 3:09 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/notificationHelper.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/icon.js
diff --git a/lib/icon.js b/lib/icon.js
index 2011d1e23701aada50df48f3f2722514d3b3b24e..1c79fa96e3556d4f2c1c37ebc65594cb09d9f4f2 100644
--- a/lib/icon.js
+++ b/lib/icon.js
@@ -17,73 +17,146 @@
/** @module icon */
-const numberOfFrames = 10;
+"use strict";
+const frameOpacities = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,
+ 1, 1, 1, 1, 1,
Sebastian Noack 2016/01/23 20:38:30 Since, we have 10 frames per second now, you also
kzar 2016/01/23 21:16:56 Done.
+ 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0];
+const numberOfFrames = frameOpacities.length;
+
+let stopAnimation = null;
+let animationPlaying = false;
let whitelistedState = new ext.PageMap();
-let notificationType = null;
-let animationInterval = null;
-let animationStep = 0;
-function getIconPath(whitelisted)
+let loadImageAsCanvas =
+/**
+ * Loads an image and draws it onto a canvas.
+ *
+ * @param {string} path Path of the image to load
Sebastian Noack 2016/01/23 20:38:29 Nit: "url" would be more accurrate than "path" as
kzar 2016/01/23 21:16:56 Done.
+ * @return {Promise}
+ */
+exports.loadImageAsCanvas = function (path, size)
Sebastian Noack 2016/01/23 20:38:29 Nit: No space before arguments list.
Sebastian Noack 2016/01/23 20:38:29 The size argument is unused.
kzar 2016/01/23 21:16:55 Done.
kzar 2016/01/23 21:16:56 Done.
{
- let filename = "icons/abp-$size";
+ return new Promise((resolve, reject) => {
+ let image = new Image();
+ image.src = path;
+ image.addEventListener("load", () =>
+ {
+ let canvas = document.createElement("canvas");
+ let context = canvas.getContext("2d");
+ canvas.height = image.height;
+ canvas.width = image.width;
+ context.drawImage(image, 0, 0);
+ resolve(canvas);
+ });
+ image.addEventListener("error", () => {
+ reject("Failed to load image " + path);
+ });
+ });
+};
- // If the current page is whitelisted, pick an icon that indicates that
- // Adblock Plus is disabled, however not when the notification icon has
- // full opacity, or on Safari where icons are genrally grayscale-only.
- if (whitelisted && animationStep < numberOfFrames && require("info").platform != "safari")
- filename += "-whitelisted";
+function getImageData(baseIcon, overlayIcon, opacity, animationStep, frameCache)
+{
+ let cacheIndex = baseIcon + overlayIcon + opacity;
- // If the icon is currently animating to indicate a pending notification,
- // pick the icon for the corresponing notification type and animation frame.
- if (notificationType && animationStep > 0)
- {
- filename += "-notification-" + notificationType;
+ if (frameCache && frameCache[cacheIndex])
+ return frameCache[cacheIndex];
- if (animationStep < numberOfFrames)
- filename += "-" + animationStep;
- }
+ let imageData = Promise.all(
+ [loadImageAsCanvas(baseIcon),
+ overlayIcon && loadImageAsCanvas(overlayIcon) || Promise.resolve(null)]
+ ).then(icons =>
+ {
+ let baseIconCanvas = icons[0];
+ let overlayIconCanvas = icons[1];
+ let context = baseIconCanvas.getContext("2d");
- return filename + ".png";
+ if (overlayIconCanvas && opacity)
+ {
+ context.globalAlpha = opacity;
+ context.drawImage(overlayIconCanvas, 0, 0);
+ }
+
+ return context.getImageData(0, 0, baseIconCanvas.width,
+ baseIconCanvas.height);
+ });
+ if (frameCache)
Sebastian Noack 2016/01/23 20:38:29 Instead of caching we could probably simplify the
kzar 2016/01/23 21:16:57 I did consider pre-rendering them all, or caching
Sebastian Noack 2016/01/23 21:26:32 The idea is NOT to prerender them on extension int
kzar 2016/01/24 15:43:00 Ah I see, I think you're right. Done.
+ frameCache[cacheIndex] = imageData;
+ return imageData;
}
-function setIcon(page)
+function setIcon(page, animationType, animationStep, frameCache)
{
- page.browserAction.setIcon(getIconPath(whitelistedState.get(page)));
+ let safari = require("info").platform == "safari";
+ let opacity = animationType ? frameOpacities[animationStep] : 0;
+ let greyed = whitelistedState.get(page) && !safari;
+ let blending = (animationType && opacity > 0 && opacity < 1);
+
+ let filename = "icons/abp-$size";
+ let baseIcon = filename + (greyed ? "-whitelisted" : "") + ".png";
+ let overlayIcon = animationType && (filename + "-notification-" +
+ animationType + ".png");
+
+
+ // If the icon doesn't need any modifications, or the platform doesn't support
+ // data URLs, we can just use the image's filename with the $size placeholder.
+ if (!blending || safari)
+ if (overlayIcon && opacity > 0.5)
+ return page.browserAction.setIcon(overlayIcon);
+ else
+ return page.browserAction.setIcon(baseIcon);
+
+ // Otherwise we must process the images using a canvas and return a data URL
+ // of the result for each size that's required. (19px and 38px are required by
+ // Chrome/Opera.)
+ let imageData = [19, 38].map(size =>
+ {
+ return getImageData(baseIcon.replace("$size", size.toString()),
+ overlayIcon.replace("$size", size.toString()),
+ opacity, animationStep, frameCache);
+ });
+ Promise.all(imageData).then(imageData =>
+ {
+ chrome.browserAction.setIcon({tabId: page._id,
+ imageData: {19: imageData[0],
Sebastian Noack 2016/01/23 20:38:29 How about generating the object as it is used in t
kzar 2016/01/23 21:16:55 That would be nice but I'm not sure how to do it c
Sebastian Noack 2016/01/23 21:26:32 See my other comment, and the code snippet I provi
+ 38: imageData[1]}});
+ });
}
-function runAnimation()
+function runAnimation(animationType)
{
- return setInterval(function()
+ let frameCache = {};
+ let frameInterval;
+
+ function playAnimation()
{
- ext.pages.query({active: true}, function(pages)
+ animationPlaying = true;
+ let animationStep = 0;
+ frameInterval = setInterval(() =>
{
- let fadeInInterval = setInterval(function()
+ ext.pages.query({active: true}, pages =>
Sebastian Noack 2016/01/23 20:38:29 Querying every 100ms for active tabs will impair t
kzar 2016/01/23 21:16:56 Ah, whoops. I changed this around because I notice
Sebastian Noack 2016/01/23 21:26:33 Well, exceptions shouldn't occur. The easiest way,
kzar 2016/01/23 22:01:06 On second thoughts I think it is worth adding the
{
- animationStep++;
- pages.forEach(setIcon);
-
- if (animationStep < numberOfFrames)
- return;
-
- setTimeout(function()
+ pages.forEach(page => {
+ setIcon(page, animationType, animationStep++, frameCache);
+ });
+ if (animationStep >= numberOfFrames)
{
- let fadeOutInterval = setInterval(function()
- {
- animationStep--;
- pages.forEach(setIcon);
-
- if (animationStep > 0)
- return;
-
- clearInterval(fadeOutInterval);
- }, 100);
- },1000);
+ animationStep = 0;
+ clearInterval(frameInterval);
+ animationPlaying = false;
+ }
+ });
+ }, 100);
+ }
- clearInterval(fadeInInterval);
- }, 100);
- });
- }, 10000);
+ playAnimation();
+ let animationInterval = setInterval(playAnimation, 10000);
+ return () =>
+ {
+ clearInterval(frameInterval);
+ clearInterval(animationInterval);
+ animationPlaying = false;
+ };
}
/**
@@ -95,8 +168,9 @@ function runAnimation()
*/
exports.updateIcon = function(page, whitelisted)
{
- page.browserAction.setIcon(getIconPath(whitelisted));
whitelistedState.set(page, whitelisted);
+ if (!animationPlaying)
+ setIcon(page);
};
/**
@@ -106,10 +180,8 @@ exports.updateIcon = function(page, whitelisted)
*/
exports.startIconAnimation = function(type)
{
- notificationType = type;
-
- if (animationInterval == null)
- animationInterval = runAnimation();
+ stopAnimation && stopAnimation();
+ stopAnimation = runAnimation(type);
};
/**
@@ -117,11 +189,5 @@ exports.startIconAnimation = function(type)
*/
exports.stopIconAnimation = function()
{
- if (animationInterval != null)
- {
- clearInterval(animationInterval);
- animationInterval = null;
- }
-
- notificationType = null;
+ stopAnimation && stopAnimation();
};
« no previous file with comments | « no previous file | lib/notificationHelper.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld