From 1c3b817b89a37103bcb0551b975c88e862a4c059 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 22 Mar 2013 16:22:18 -0700 Subject: [PATCH] Autosize both sprites (fixes #1116) --- js/id/svg/surface.js | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/js/id/svg/surface.js b/js/id/svg/surface.js index 15ef0cca3..c37f5d89b 100644 --- a/js/id/svg/surface.js +++ b/js/id/svg/surface.js @@ -5,6 +5,17 @@ iD.svg.Surface = function() { }); } + function autosize(image) { + var img = document.createElement('img'); + img.src = image.attr('xlink:href'); + img.onload = function() { + image.attr({ + width: img.width, + height: img.height + }); + }; + } + function sprites(stylesheetName, selectorRegexp) { var sprites = []; @@ -90,12 +101,9 @@ iD.svg.Surface = function() { .attr('height', function(d) { return d; }); defs.append('image') - .attr({ - id: 'sprite', - width: 460, - height: 320, - 'xlink:href': 'img/sprite.png' - }); + .attr('id', 'sprite') + .attr('xlink:href', 'img/sprite.png') + .call(autosize); defs.selectAll() .data(sprites("app.css", /^\.(icon-operation-[a-z0-9-]+)$/)) @@ -104,20 +112,10 @@ iD.svg.Surface = function() { .attr('transform', function(d) { return "translate(" + d.x + "," + d.y + ")"; }) .attr('xlink:href', '#sprite'); - var image = defs.append('image') - .attr({ - id: 'maki-sprite', - 'xlink:href': 'img/feature-icons.png' - }); - - var img = document.createElement('img'); - img.src = 'img/feature-icons.png'; - img.onload = function() { - image.attr({ - width: img.width, - height: img.height - }); - }; + defs.append('image') + .attr('id', 'maki-sprite') + .attr('xlink:href', 'img/feature-icons.png') + .call(autosize); defs.selectAll() .data(sprites("feature-icons.css", /^\.(feature-[a-z0-9-]+-(12|18))$/))