mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2024-12-29 09:22:22 -05:00
Only mark an image as loaded if it actually has src set and is complete.
This commit is contained in:
parent
8391543115
commit
0b991cefdd
3 changed files with 49 additions and 50 deletions
|
@ -245,7 +245,6 @@ var Raster = Item.extend(/** @lends Raster# */{
|
|||
// Trigger the load event on the image once it's loaded
|
||||
DomEvent.add(image, {
|
||||
load: function(event) {
|
||||
that._loaded = true;
|
||||
that._setImage(image);
|
||||
emit(event);
|
||||
},
|
||||
|
@ -272,7 +271,7 @@ var Raster = Item.extend(/** @lends Raster# */{
|
|||
// A Image object
|
||||
this._image = image;
|
||||
this._canvas = null;
|
||||
this._loaded = image && image.complete;
|
||||
this._loaded = !!(image && image.src && image.complete);
|
||||
}
|
||||
// Both canvas and image have width / height attributes. Due to IE,
|
||||
// naturalWidth / Height needs to be checked for a swell, because it
|
||||
|
|
|
@ -37,6 +37,47 @@ console.error = function() {
|
|||
errorHandler.apply(this, arguments);
|
||||
};
|
||||
|
||||
// Override equals to convert functions to message and execute them as tests()
|
||||
function equals(actual, expected, message, options) {
|
||||
// Allow the use of functions for actual, which will get called and their
|
||||
// source content extracted for readable reports.
|
||||
if (typeof actual === 'function') {
|
||||
if (!message)
|
||||
message = getFunctionMessage(actual);
|
||||
actual = actual();
|
||||
}
|
||||
// Get the comparator based on the expected value's type only and ignore the
|
||||
// actual value's type.
|
||||
var type = typeof expected,
|
||||
cls;
|
||||
type = expected === null && 'Null'
|
||||
|| type === 'number' && 'Number'
|
||||
|| type === 'boolean' && 'Boolean'
|
||||
|| type === 'undefined' && 'Undefined'
|
||||
|| Array.isArray(expected) && 'Array'
|
||||
|| expected instanceof Element && 'Element' // handle DOM Elements
|
||||
|| (cls = expected && expected._class) // check _class 2nd last
|
||||
|| type === 'object' && 'Object'; // Object as catch-all
|
||||
var comparator = type && comparators[type];
|
||||
if (!message)
|
||||
message = type ? type.toLowerCase() : 'value';
|
||||
if (comparator) {
|
||||
comparator(actual, expected, message, options);
|
||||
} else if (expected && expected.equals) {
|
||||
// Fall back to equals
|
||||
QUnit.push(expected.equals(actual), actual, expected, message);
|
||||
} else {
|
||||
// Finally perform a strict compare
|
||||
QUnit.push(actual === expected, actual, expected, message);
|
||||
}
|
||||
if (options && options.cloned && cls) {
|
||||
var identical = identicalAfterCloning[cls];
|
||||
QUnit.push(identical ? actual === expected : actual !== expected,
|
||||
actual, identical ? expected : 'not ' + expected,
|
||||
message + ': identical after cloning');
|
||||
}
|
||||
}
|
||||
|
||||
// Register a jsDump parser for Base.
|
||||
QUnit.jsDump.setParser('Base', function (obj, stack) {
|
||||
// Just compare the string representation of classes inheriting from Base,
|
||||
|
@ -338,47 +379,6 @@ function getFunctionMessage(func) {
|
|||
return message;
|
||||
}
|
||||
|
||||
// Override equals to convert functions to message and execute them as tests()
|
||||
function equals(actual, expected, message, options) {
|
||||
// Allow the use of functions for actual, which will get called and their
|
||||
// source content extracted for readable reports.
|
||||
if (typeof actual === 'function') {
|
||||
if (!message)
|
||||
message = getFunctionMessage(actual);
|
||||
actual = actual();
|
||||
}
|
||||
// Get the comparator based on the expected value's type only and ignore the
|
||||
// actual value's type.
|
||||
var type = typeof expected,
|
||||
cls;
|
||||
type = expected === null && 'Null'
|
||||
|| type === 'number' && 'Number'
|
||||
|| type === 'boolean' && 'Boolean'
|
||||
|| type === 'undefined' && 'Undefined'
|
||||
|| Array.isArray(expected) && 'Array'
|
||||
|| expected instanceof Element && 'Element' // handle DOM Elements
|
||||
|| (cls = expected && expected._class) // check _class 2nd last
|
||||
|| type === 'object' && 'Object'; // Object as catch-all
|
||||
var comparator = type && comparators[type];
|
||||
if (!message)
|
||||
message = type ? type.toLowerCase() : 'value';
|
||||
if (comparator) {
|
||||
comparator(actual, expected, message, options);
|
||||
} else if (expected && expected.equals) {
|
||||
// Fall back to equals
|
||||
QUnit.push(expected.equals(actual), actual, expected, message);
|
||||
} else {
|
||||
// Finally perform a strict compare
|
||||
QUnit.push(actual === expected, actual, expected, message);
|
||||
}
|
||||
if (options && options.cloned && cls) {
|
||||
var identical = identicalAfterCloning[cls];
|
||||
QUnit.push(identical ? actual === expected : actual !== expected,
|
||||
actual, identical ? expected : 'not ' + expected,
|
||||
message + ': identical after cloning');
|
||||
}
|
||||
}
|
||||
|
||||
function test(testName, expected) {
|
||||
return QUnit.test(testName, function() {
|
||||
var project = new Project();
|
||||
|
|
|
@ -14,19 +14,19 @@ module('Raster');
|
|||
|
||||
test('Create a raster without a source and check its size', function() {
|
||||
var raster = new Raster();
|
||||
equals(raster.size.toString(), new Size(0, 0).toString(), true);
|
||||
equals(raster.size, new Size(0, 0), true);
|
||||
});
|
||||
|
||||
test('Create a raster without a source and set its size', function() {
|
||||
var raster = new Raster();
|
||||
raster.size = [640, 480];
|
||||
equals(raster.size.toString(), new Size(640, 480).toString(), true);
|
||||
equals(raster.size, new Size(640, 480), true);
|
||||
});
|
||||
|
||||
asyncTest('Create a raster from a url', function(callback) {
|
||||
var raster = new Raster('assets/paper-js.gif');
|
||||
raster.onLoad = function() {
|
||||
equals(raster.size.toString(), new Size(146, 146).toString(), true);
|
||||
equals(raster.size, new Size(146, 146), true);
|
||||
callback();
|
||||
};
|
||||
});
|
||||
|
@ -34,7 +34,7 @@ asyncTest('Create a raster from a url', function(callback) {
|
|||
asyncTest('Create a raster from a data url', function(callback) {
|
||||
var raster = new Raster('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABlJREFUeNpi+s/AwPCfgYmR4f9/hv8AAQYAHiAFAS8Lwy8AAAAASUVORK5CYII=');
|
||||
raster.onLoad = function() {
|
||||
equals(raster.size.toString(), new Size(2, 2).toString(), true);
|
||||
equals(raster.size, new Size(2, 2), true);
|
||||
callback();
|
||||
};
|
||||
});
|
||||
|
@ -46,7 +46,7 @@ asyncTest('Create a raster from a dom image', function(callback) {
|
|||
DomEvent.add(img, {
|
||||
load: function() {
|
||||
var raster = new Raster(img);
|
||||
equals(raster.size.toString(), new Size(146, 146).toString(), true);
|
||||
equals(raster.size, new Size(146, 146), true);
|
||||
document.body.removeChild(img);
|
||||
callback();
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ asyncTest('Create a raster from a dom image', function(callback) {
|
|||
test('Create a raster from a canvas', function(callback) {
|
||||
var canvas = CanvasProvider.getCanvas(30, 20);
|
||||
var raster = new Raster(canvas);
|
||||
equals(raster.size.toString(), new Size(30, 20).toString(), true);
|
||||
equals(raster.size, new Size(30, 20), true);
|
||||
CanvasProvider.release(canvas);
|
||||
});
|
||||
|
||||
|
@ -68,7 +68,7 @@ asyncTest('Create a raster from a dom id', function(callback) {
|
|||
DomEvent.add(img, {
|
||||
load: function() {
|
||||
var raster = new Raster('testimage');
|
||||
equals(raster.size.toString(), new Size(146, 146).toString(), true);
|
||||
equals(raster.size, new Size(146, 146), true);
|
||||
document.body.removeChild(img);
|
||||
callback();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue