summaryrefslogtreecommitdiff
path: root/public/js/mp/resource.js
diff options
context:
space:
mode:
authorFreeyorp <TheFreeYorp@NOSPAM.G.m.a.i.l.replace>2013-06-26 02:58:35 +1200
committerFreeyorp <TheFreeYorp@NOSPAM.G.m.a.i.l.replace>2013-06-26 02:58:35 +1200
commitda89573679309ad3c8ed11b93a806ea6384ba6fb (patch)
tree8b68ef02da4b8b1742e9c7ff49e1698f96e9edde /public/js/mp/resource.js
parent2cf8ba330dc60fa4c269c4fdb16716a1d9ad1e90 (diff)
downloadmanaportal-da89573679309ad3c8ed11b93a806ea6384ba6fb.tar.gz
manaportal-da89573679309ad3c8ed11b93a806ea6384ba6fb.tar.bz2
manaportal-da89573679309ad3c8ed11b93a806ea6384ba6fb.tar.xz
manaportal-da89573679309ad3c8ed11b93a806ea6384ba6fb.zip
Implement dyeImage
Split loadImage into a common resource.js Add a compatability check for both canvas-node and browser functionality Note that the generated dyed image is off-by-one to the tested TMWW image. The algorithm needs verification and possibly correction. Either way, it's close enough by the eye.
Diffstat (limited to 'public/js/mp/resource.js')
-rw-r--r--public/js/mp/resource.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/public/js/mp/resource.js b/public/js/mp/resource.js
new file mode 100644
index 0000000..f348a89
--- /dev/null
+++ b/public/js/mp/resource.js
@@ -0,0 +1,36 @@
+"use strict";
+var mp = function(mp) {
+ mp.resource = {
+ loadImage: loadImage
+ };
+
+ /*
+ * Quick compatability workaround
+ * node testing environment needs new Canvas() and won't tolerate document.createElement("canvas")
+ * A createCanvas method is therefore provided in its sandbox which can be quickly checked to determine the method needed
+ */
+ var createCanvas = "createCanvas" in document ? document.createCanvas : function() { return document.createElement("canvas"); };
+
+ var canvas = createCanvas();
+ var context = canvas.getContext("2d");
+
+ /*
+ * Load in an image given a URL.
+ * The provided callback will fire when loading is complete.
+ * The parameters will be false and the the imageData if successful, and false and the error otherwise.
+ */
+ function loadImage(url, callback) {
+ var image = new Image();
+ image.onload = function() {
+ canvas.width = image.width;
+ canvas.height = image.height;
+ context.drawImage(image, 0, 0);
+ callback(false, context.getImageData(0, 0, image.width, image.height));
+ };
+ image.onerror = function(err) {
+ callback(true, err);
+ };
+ image.src = url;
+ }
+ return mp;
+}(mp || {});