From 056dcceaccc9e28a369261cccfa629cff0249092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 2 Nov 2013 21:17:11 +0100 Subject: [PATCH] Implement #importSVG() from external URL. --- src/core/PaperScript.js | 27 ++++++++------------------- src/net/Http.js | 34 ++++++++++++++++++++++++++++++++++ src/paper.js | 5 +++++ src/svg/SVGImport.js | 22 +++++++++++++++++++++- 4 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 src/net/Http.js diff --git a/src/core/PaperScript.js b/src/core/PaperScript.js index 787e020e..3a78f16a 100644 --- a/src/core/PaperScript.js +++ b/src/core/PaperScript.js @@ -289,20 +289,6 @@ paper.PaperScope.prototype.PaperScript = (function(root) { } /*#*/ if (options.environment == 'browser') { - // Code borrowed from Coffee Script: - function request(url, scope) { - var xhr = new (window.ActiveXObject || XMLHttpRequest)( - 'Microsoft.XMLHTTP'); - xhr.open('GET', url, true); - if (xhr.overrideMimeType) - xhr.overrideMimeType('text/plain'); - xhr.onreadystatechange = function() { - if (xhr.readyState === 4) { - return evaluate(xhr.responseText, scope); - } - }; - return xhr.send(null); - } function load() { var scripts = document.getElementsByTagName('script'); @@ -324,11 +310,14 @@ paper.PaperScope.prototype.PaperScript = (function(root) { // it, to support multiple scripts per canvas. Otherwise // create a new one. scope = PaperScope.get(canvas) - || new PaperScope(script).setup(canvas); - if (script.src) { - // If we're loading from a source, request that first and then - // run later. - request(script.src, scope); + || new PaperScope(script).setup(canvas), + src = script.src; + if (src) { + // If we're loading from a source, request that first and + // then run later. + Http.request('get', src, function(code) { + evaluate(code, scope); + }); } else { // We can simply get the code form the script tag. evaluate(script.innerHTML, scope); diff --git a/src/net/Http.js b/src/net/Http.js new file mode 100644 index 00000000..d4d05832 --- /dev/null +++ b/src/net/Http.js @@ -0,0 +1,34 @@ +/* + * Paper.js - The Swiss Army Knife of Vector Graphics Scripting. + * http://paperjs.org/ + * + * Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey + * http://lehni.org/ & http://jonathanpuckey.com/ + * + * Distributed under the MIT license. See LICENSE file for details. + * + * All rights reserved. + */ + +var Http = { + request: function(method, url, callback) { + // Code borrowed from Coffee Script and extended: + var xhr = new (window.ActiveXObject || XMLHttpRequest)( + 'Microsoft.XMLHTTP'); + xhr.open(method.toUpperCase(), url, true); + if ('overrideMimeType' in xhr) + xhr.overrideMimeType('text/plain'); + xhr.onreadystatechange = function() { + if (xhr.readyState === 4) { + var status = xhr.status; + if (status === 0 || status === 200) { + callback.call(xhr, xhr.responseText); + } else { + throw new Error('Could not load ' + url + ' (Error ' + + status + ')'); + } + } + }; + return xhr.send(null); + } +}; diff --git a/src/paper.js b/src/paper.js index 9899f659..f05e104b 100644 --- a/src/paper.js +++ b/src/paper.js @@ -118,6 +118,11 @@ var paper = new function(undefined) { /*#*/ include('tool/ToolEvent.js'); /*#*/ include('tool/Tool.js'); + +// Http is used both for PaperScript and SVGImport +/*#*/ if (options.paperscript || options.svg) { +/*#*/ include('net/Http.js'); +/*#*/ } // options.paperscript || options.svg /*#*/ } // options.environment == 'browser' /*#*/ include('canvas/CanvasProvider.js'); diff --git a/src/svg/SVGImport.js b/src/svg/SVGImport.js index f0787d21..cd2b6f68 100644 --- a/src/svg/SVGImport.js +++ b/src/svg/SVGImport.js @@ -493,8 +493,26 @@ new function() { function importSVG(node, isRoot, options) { if (!options) options = {}; - if (typeof node === 'string') + if (typeof node === 'string') { + // Check if the string does not represent SVG data, in which case + // it must be a url of a SVG to be loaded. + if (isRoot && !/^.*