weex-gcanvas
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -5,156 +5,158 @@ var GBridge = require('./gutil').GBridge; | ||
// GCanvasImage | ||
function GCanvasImage() { | ||
/** | ||
* The width of the image after it is loaded. | ||
* | ||
* @type {number} | ||
*/ | ||
this.width = 0; | ||
/** | ||
* The height of the image after it is loaded. | ||
* | ||
* @type {number} | ||
*/ | ||
this.height = 0; | ||
/** | ||
* A unique id assigned to each image upon creation. | ||
* | ||
* @type {number} | ||
*/ | ||
// this.id = (++GCanvasImage.idCounter); | ||
// this._id = this.id; // public facing "id" but _id used to internally | ||
// track image | ||
this._src = ""; // image source path | ||
this._complete = true; // "is loading" identifier for complete property | ||
} | ||
function GCanvasImage() { | ||
/** | ||
* Iterator for generating id values for GCanvasImage instances | ||
* internally. | ||
* The width of the image after it is loaded. | ||
* | ||
* @private | ||
* @type {number} | ||
*/ | ||
GCanvasImage.idCounter = 0; | ||
GCanvasImage.imageMap = new GHashMap(); | ||
this.width = 0; | ||
/** | ||
* Callback for when an image has successfully loaded a file path assigned | ||
* to {@link GCanvasImage#src}. | ||
* The height of the image after it is loaded. | ||
* | ||
* @type {function} | ||
* @name GCanvasImage#onload | ||
* @type {number} | ||
*/ | ||
this.height = 0; | ||
/** | ||
* Callback for when an image has failed to load a file path assigned to | ||
* {@link GCanvasImage#src}. | ||
* A unique id assigned to each image upon creation. | ||
* | ||
* @type {function} | ||
* @name GCanvasImage#onerror | ||
* @type {number} | ||
*/ | ||
GCanvasImage.prototype.removeEventListener = function(type, callback, force) { | ||
}; | ||
// this.id = (++GCanvasImage.idCounter); | ||
GCanvasImage.prototype.addEventListener = function(type, listener, force) { | ||
if (type === "load" && typeof listener === 'function') { | ||
this.onload = listener; | ||
} | ||
// this._id = this.id; // public facing "id" but _id used to internally | ||
// track image | ||
this._src = ""; // image source path | ||
this._complete = true; // "is loading" identifier for complete property | ||
} | ||
if (type === "error" && typeof listener === 'function') { | ||
this.onerror = listener; | ||
} | ||
}; | ||
/** | ||
* Iterator for generating id values for GCanvasImage instances | ||
* internally. | ||
* | ||
* @private | ||
*/ | ||
GCanvasImage.idCounter = 0; | ||
GCanvasImage.imageMap = new GHashMap(); | ||
/** | ||
* The source property, identifying a URL path to the image's file location. | ||
* Upon setting this value to a file path value, the file is loaded into the | ||
* GCanvas plugin. For GCanvas images can be unloaded by setting the | ||
* src to null or "". | ||
* | ||
* @type {string} | ||
* @name GCanvasImage#src | ||
* @example var myImage = GCanvas.createImage(); myImage.onload = | ||
* function(){ // ... myContext.drawImage(myImage, 0,0,100,100, | ||
* 0,0,100,100); GCanvas.render(); } myImage.onerror = | ||
* function(){ console.log("Could not load image!"); } myImage.src = | ||
* "images/spritesheet.jpg"; | ||
*/ | ||
/** | ||
* Callback for when an image has successfully loaded a file path assigned | ||
* to {@link GCanvasImage#src}. | ||
* | ||
* @type {function} | ||
* @name GCanvasImage#onload | ||
*/ | ||
Object.defineProperty(GCanvasImage.prototype, "src", { | ||
get: function() { | ||
return this._src; | ||
}, | ||
set: function(src) { | ||
/** | ||
* Callback for when an image has failed to load a file path assigned to | ||
* {@link GCanvasImage#src}. | ||
* | ||
* @type {function} | ||
* @name GCanvasImage#onerror | ||
*/ | ||
GCanvasImage.prototype.removeEventListener = function(type, callback, force) {}; | ||
if (!src || src === this._src) { | ||
return; | ||
} | ||
GCanvasImage.prototype.addEventListener = function(type, listener, force) { | ||
if (type === "load" && typeof listener === 'function') { | ||
this.onload = listener; | ||
} | ||
this._src = src; | ||
if (type === "error" && typeof listener === 'function') { | ||
this.onerror = listener; | ||
} | ||
}; | ||
// Loading | ||
this.complete = false; | ||
/** | ||
* The source property, identifying a URL path to the image's file location. | ||
* Upon setting this value to a file path value, the file is loaded into the | ||
* GCanvas plugin. For GCanvas images can be unloaded by setting the | ||
* src to null or "". | ||
* | ||
* @type {string} | ||
* @name GCanvasImage#src | ||
* @example var myImage = GCanvas.createImage(); myImage.onload = | ||
* function(){ // ... myContext.drawImage(myImage, 0,0,100,100, | ||
* 0,0,100,100); GCanvas.render(); } myImage.onerror = | ||
* function(){ console.log("Could not load image!"); } myImage.src = | ||
* "images/spritesheet.jpg"; | ||
*/ | ||
// callback wrappers | ||
var me = this; | ||
Object.defineProperty(GCanvasImage.prototype, "src", { | ||
get: function() { | ||
return this._src; | ||
}, | ||
set: function(src) { | ||
var data = GCanvasImage.imageMap.get(src); | ||
if( data ) | ||
{ | ||
me.id = data.id; | ||
me._id = data.id; | ||
me.complete = true; | ||
me.width = data.width; | ||
me.height = data.height; | ||
me.onload && me.onload(); | ||
return; | ||
} | ||
if (!src || src === this._src) { | ||
return; | ||
} | ||
this.id = (++GCanvasImage.idCounter); | ||
this._id = this.id; // public facing "id" but _id used to internally | ||
this._src = src; | ||
GBridge.preLoadImage([src, this.id], function (data) { | ||
if (typeof data === 'string') { | ||
try { | ||
data = JSON.parse(data); | ||
} catch (err) {} | ||
} | ||
// Loading | ||
this.complete = false; | ||
if( data.error ){ | ||
me.onerror && me.onerror(); | ||
}else{ | ||
me.complete = true; | ||
me.width = typeof data.width === 'number' ? data.width : 0; | ||
me.height = typeof data.height === 'number' ? data.height : 0; | ||
// callback wrappers | ||
var me = this; | ||
me.onload && me.onload(); | ||
GCanvasImage.imageMap.put(src, data); | ||
} | ||
}); | ||
var data = GCanvasImage.imageMap.get(src); | ||
if (data) { | ||
me.id = data.id; | ||
me._id = data.id; | ||
me.complete = true; | ||
me.width = data.width; | ||
me.height = data.height; | ||
me.onload && me.onload(); | ||
return; | ||
} | ||
}); | ||
/** | ||
* False when the image is in the process of loading an image after the src | ||
* property has been set. True when loading is complete or if src is never | ||
* set. If an error occurred when attempting to load the image, once the | ||
* process of loading is complete, despite the error, this value will still | ||
* become true. | ||
* | ||
* @type {boolean} | ||
* @name GCanvasImage#complete | ||
*/ | ||
Object.defineProperty(GCanvasImage.prototype, "complete", { | ||
get: function() { | ||
return this._complete; | ||
}, | ||
set: function(value) { | ||
this._complete = value; | ||
} | ||
}); | ||
this.id = (++GCanvasImage.idCounter); | ||
this._id = this.id; // public facing "id" but _id used to internally | ||
GBridge.preLoadImage([src, this.id], function(data) { | ||
if (typeof data === 'string') { | ||
try { | ||
data = JSON.parse(data); | ||
} catch (err) {} | ||
} | ||
module.exports = typeof Image === 'function' ? Image : GCanvasImage; | ||
if (data.error) { | ||
me.onerror && me.onerror(); | ||
} else { | ||
me.complete = true; | ||
me.width = typeof data.width === 'number' ? data.width : 0; | ||
me.height = typeof data.height === 'number' ? data.height : 0; | ||
me.onload && me.onload(); | ||
GCanvasImage.imageMap.put(src, data); | ||
} | ||
}); | ||
} | ||
}); | ||
/** | ||
* False when the image is in the process of loading an image after the src | ||
* property has been set. True when loading is complete or if src is never | ||
* set. If an error occurred when attempting to load the image, once the | ||
* process of loading is complete, despite the error, this value will still | ||
* become true. | ||
* | ||
* @type {boolean} | ||
* @name GCanvasImage#complete | ||
*/ | ||
Object.defineProperty(GCanvasImage.prototype, "complete", { | ||
get: function() { | ||
return this._complete; | ||
}, | ||
set: function(value) { | ||
this._complete = value; | ||
} | ||
}); | ||
GCanvasImage.prototype.tagName = 'img'; | ||
GCanvasImage.prototype.getAttribute = function(name) { | ||
return this[name]; | ||
}; | ||
module.exports = typeof Image === 'function' ? Image : GCanvasImage; |
{ | ||
"name": "weex-gcanvas", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"main":"gcanvas.js", | ||
@@ -5,0 +5,0 @@ "platform": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2679
94331