diff --git a/examples/Node.js/RemoteRaster.js b/examples/Node.js/RasterRemote.js similarity index 92% rename from examples/Node.js/RemoteRaster.js rename to examples/Node.js/RasterRemote.js index 8e109c7c..4318415b 100644 --- a/examples/Node.js/RemoteRaster.js +++ b/examples/Node.js/RasterRemote.js @@ -27,3 +27,7 @@ raster.onLoad = function() { console.log('saved png'); }); }; + +raster.onError = function(message) { + console.error(message); +}; diff --git a/examples/Node.js/SvgImport.js b/examples/Node.js/SvgImport.js index fe7f7fbc..730327c3 100644 --- a/examples/Node.js/SvgImport.js +++ b/examples/Node.js/SvgImport.js @@ -6,7 +6,7 @@ var path = require('path'); var fs = require('fs'); paper.setup(new paper.Size(300, 600)); -paper.project.importSVG('file://' + path.resolve('./in.svg'), { +paper.project.importSVG('in.svg', { onLoad: function(item) { paper.view.exportFrames({ amount: 1, diff --git a/src/core/PaperScope.js b/src/core/PaperScope.js index 343cf0e0..260ebc8c 100644 --- a/src/core/PaperScope.js +++ b/src/core/PaperScope.js @@ -292,6 +292,12 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{ delete PaperScope._scopes[this._id]; }, + resolvePath: function(url) { + // On Node.js, resolve relative URLs to local files: + return this.agent.node && !/^(?:[a-z]+:)?\/\//i.test(url) + ? 'file://' + require('path').resolve(url) : url; + }, + statics: new function() { // Produces helpers to e.g. check for both 'canvas' and // 'data-paper-canvas' attributes: diff --git a/src/item/Raster.js b/src/item/Raster.js index 725c2fc0..aebd4679 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -82,12 +82,14 @@ var Raster = Item.extend(/** @lends Raster# */{ // Otherwise we need to check the type of object: if (!this._initialize(object, position !== undefined && Point.read(arguments, 1))) { - if (typeof object === 'string') { - // Both data-urls and normal urls are supported here! - this.setSource(object); - } else { + // object can be an image, canvas, URL or DOM-ID: + var image = typeof object === 'string' + ? document.getElementById(object) : object; + if (image) { // #setImage() handles both canvas and image types. - this.setImage(object); + this.setImage(image); + } else { + this.setSource(object); } } if (!this._size) { @@ -367,14 +369,11 @@ var Raster = Item.extend(/** @lends Raster# */{ }, setSource: function(src) { - var crossOrigin = this._crossOrigin, - // src can be an URL or a DOM ID to load the image from: - image = document.getElementById(src) || new window.Image(); + image = new window.Image(); + image.src = paper.resolvePath(src); + var crossOrigin = this._crossOrigin; if (crossOrigin) image.crossOrigin = crossOrigin; - // A new image created above? Set the source now. - if (!image.src) - image.src = src; this.setImage(image); }, @@ -397,13 +396,15 @@ var Raster = Item.extend(/** @lends Raster# */{ * console.log(view.element.toDataURL('image/png').substring(0, 32)); */ getCrossOrigin: function() { - return this._image && this._image.crossOrigin || this._crossOrigin || ''; + var image = this._image; + return image && image.crossOrigin || this._crossOrigin || ''; }, setCrossOrigin: function(crossOrigin) { this._crossOrigin = crossOrigin; - if (this._image) - this._image.crossOrigin = crossOrigin; + var image = this._image; + if (image) + image.crossOrigin = crossOrigin; }, // DOCS: document Raster#getElement diff --git a/src/net/Http.js b/src/net/Http.js index e5a304af..26ec7827 100644 --- a/src/net/Http.js +++ b/src/net/Http.js @@ -14,7 +14,8 @@ var Http = { request: function(options) { // Code borrowed from Coffee Script and extended: var xhr = new window.XMLHttpRequest(); - xhr.open((options.method || 'get').toUpperCase(), options.url, + xhr.open((options.method || 'get').toUpperCase(), + paper.resolvePath(options.url), Base.pick(options.async, true)); if (options.mimeType) xhr.overrideMimeType(options.mimeType);