Skip to content

Commit 1c64dad

Browse files
author
Lanny McNie
committed
Added CORS support to images generated by EaselJS.
Signed-off-by: Lanny McNie <lanny@gskinner.com>
1 parent 8cb41b4 commit 1c64dad

File tree

10 files changed

+41
-6
lines changed

10 files changed

+41
-6
lines changed

VERSIONS.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Version NEXT [NOT RELEASED]
1212
- fixed touch/multitouch in IE11
1313
- fixed issue with -1 mouse pointers (thanks cubica)
1414
- fixed issue with AlphaMapFilter (thanks Dave)
15+
- added cross-origin (CORS) support for generated images.
16+
- included a sample htaccess file in /extras that can be used to serve CORS-safe images.
1517

1618

1719
Version 0.7.0 [September 25, 2013]

docs/easeljs_docs-NEXT.zip

-35.9 KB
Binary file not shown.

examples/assets/preloadjs-NEXT.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extras/README.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This directory includes additional code and snippets that might be useful.
2+
3+
** sample.htaccess **
4+
A linux/unix .htaccess file, which provides a cross-origin header to files served from this directory. This is useful
5+
for setting up images served on a domain other than where the application is served from. Images must include a
6+
crossOrigin="Anonymous" attribute. Rename this file to ".htaccess", or add its contents to an existing .htacess file.

extras/sample.htaccess

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Header set Access-Control-Allow-Origin "*"

lib/easeljs-NEXT.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/easeljs/display/Bitmap.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ var p = Bitmap.prototype = new createjs.DisplayObject();
109109
this.DisplayObject_initialize();
110110
if (typeof imageOrUri == "string") {
111111
this.image = document.createElement("img");
112+
if (!this._isLocal(imageOrUri)) { this.image.crossOrigin = "Anonymous"; }
112113
this.image.src = imageOrUri;
113114
} else {
114115
this.image = imageOrUri;
@@ -210,6 +211,18 @@ var p = Bitmap.prototype = new createjs.DisplayObject();
210211
var hasContent = (this.image && (this.image.complete || this.image.getContext || this.image.readyState >= 2));
211212
return hasContent ? this._rectangle.initialize(0, 0, o.width, o.height) : null;
212213
};
214+
215+
/**
216+
* @method _isLocal
217+
* @param {String} path
218+
* @return {Boolean}
219+
* @protected
220+
*/
221+
p._isLocal = function(path) {
222+
var target = document.createElement("a");
223+
target.href = path;
224+
return target.hostname == "" && target.protocol == "file:";
225+
};
213226

214227
/**
215228
* Returns a clone of the Bitmap instance.

src/easeljs/display/Sprite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var p = Sprite.prototype = new createjs.DisplayObject();
6666
// events:
6767

6868
/**
69-
* Dispatched when an animation reaches its ends.
69+
* Dispatched when an animation reaches its end.
7070
* @event animationend
7171
* @param {Object} target The object that dispatched the event.
7272
* @param {String} type The event type.

src/easeljs/display/SpriteSheet.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ var p = SpriteSheet.prototype = new createjs.EventDispatcher();
263263
if (typeof img == "string") {
264264
var src = img;
265265
img = document.createElement("img");
266+
if (!this._isLocal(src) && img.crossOrigin === "") { img.crossOrigin = "Anonymous"; }
266267
img.src = src;
267268
}
268269
a.push(img);
@@ -469,5 +470,17 @@ var p = SpriteSheet.prototype = new createjs.EventDispatcher();
469470
this._numFrames = ttlFrames;
470471
};
471472

473+
/**
474+
* @method _isLocal
475+
* @param {String} path
476+
* @return {Boolean}
477+
* @protected
478+
*/
479+
p._isLocal = function(path) {
480+
var target = document.createElement("a");
481+
target.href = path;
482+
return target.hostname == "" && target.protocol == "file:";
483+
};
484+
472485
createjs.SpriteSheet = SpriteSheet;
473486
}());

src/easeljs/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ this.createjs = this.createjs || {};
2727
* @type String
2828
* @static
2929
**/
30-
s.buildDate = /*date*/"Thu, 05 Dec 2013 22:43:21 GMT"; // injected by build process
30+
s.buildDate = /*date*/"Mon, 09 Dec 2013 21:40:56 GMT"; // injected by build process
3131

3232
})();

0 commit comments

Comments
 (0)