From ee62c611811d8041755da14520b03a45888fc567 Mon Sep 17 00:00:00 2001 From: Alexis Deveria Date: Fri, 16 Jul 2010 18:33:48 +0000 Subject: [PATCH] Added support for raster image loading through image libraries git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1636 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/extensions/ext-imagelib.js | 34 +++++++++++++++-- editor/extensions/imagelib/index.html | 38 +++++++++++++++---- editor/svg-editor.js | 54 +++++++++++++-------------- editor/svgcanvas.js | 3 +- 4 files changed, 91 insertions(+), 38 deletions(-) diff --git a/editor/extensions/ext-imagelib.js b/editor/extensions/ext-imagelib.js index d9f10a43..e2418687 100644 --- a/editor/extensions/ext-imagelib.js +++ b/editor/extensions/ext-imagelib.js @@ -20,11 +20,29 @@ svgEditor.addExtension("imagelib", function() { description: 'Free library of 2300+ illustrations' } ]; - + + var xlinkns = "http://www.w3.org/1999/xlink"; function closeBrowser() { $('#imgbrowse_holder').hide(); } + + function importImage(url) { + var newImage = svgCanvas.addSvgElementFromJson({ + "element": "image", + "attr": { + "x": 0, + "y": 0, + "width": 0, + "height": 0, + "id": svgCanvas.getNextId(), + "style": "pointer-events:inherit" + } + }); + svgCanvas.clearSelection(); + svgCanvas.addToSelection([newImage]); + svgCanvas.setImageURL(url); + } window.addEventListener("message", function(evt) { // Receive postMessage data @@ -42,15 +60,25 @@ svgEditor.addExtension("imagelib", function() { svgCanvas.importSvgString(response); break; case 'd': - if(response.indexOf('data:') === 0) { + if(response.indexOf('data:image/svg+xml') === 0) { var pre = 'data:image/svg+xml;base64,'; var src = response.substring(pre.length); svgCanvas.importSvgString(svgCanvas.Utils.decode64(src)); break; + } else if(response.indexOf('data:image/') === 0) { + + importImage(response); + break; } // Else fall through default: - $.alert('Unexpected data was returned', closeBrowser); + // TODO: See if there's a way to base64 encode the binary data stream +// var str = 'data:;base64,' + svgCanvas.Utils.encode64(response, true); + + // Assume it's raw image data +// importImage(str); + + $.alert('Unexpected data was returned: ' + response, closeBrowser); return; } diff --git a/editor/extensions/imagelib/index.html b/editor/extensions/imagelib/index.html index 495888d3..bc102c47 100644 --- a/editor/extensions/imagelib/index.html +++ b/editor/extensions/imagelib/index.html @@ -6,20 +6,44 @@

Select an image:

smiley.svg - +
+logo.png