Skip to main content
deleted 49 characters in body
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

I was unaware that support for SVG fragment identifiers is not so hot, so I found somewhat of a workaround in the form of Base64 encoding (thanks to these doppnet and CSS Tricks posts).

So, after optimizing my SVG in Inkscape I converted it to base64 using Mobilefish's converter (if you use this, make sure to set max characters per line to 0 so you don't have line breaks), and then added them as strings in a JSON array:

var activityURI = [
    {"activity":"All","uri":"oZWlnaH..."},
    {"activity":"Hiking","uri":"4bWxucz..."},
    ...
];

And then accessed them like this:

//for minor extra efficiency, set prefix of URI common to every item in array:
    var uriRoot = "data:image/svg+xml;base64,PD94bWwgdmVy...";

//set full URI using desired index:
    var uriPath = uriRoot + activityURI[//desired index# goes here\\].uri;
    var defPoiStyle = {
        externalGraphic: uriPath
    }

Drawbacks

  1. Long, ugly strings in my code (thank goodness for word wrap).
  2. Bigger JS file.
  3. Tedious to maintain SVG since Base64 converter must be used every time.

Benefits:

  1. It works in Chrome.
  2. No more individual HTTP requests for individual SVG files. EDIT: not true at all! I incorrectly assumed that this method would not trigger an additional HTTP request since it is included within my JS. Back to the drawing board...
  3. Base64 may be comparable in performance to unencoded SVG.

Here is a working result. Any thoughts on how to do this better? All ears.

I was unaware that support for SVG fragment identifiers is not so hot, so I found somewhat of a workaround in the form of Base64 encoding (thanks to these doppnet and CSS Tricks posts).

So, after optimizing my SVG in Inkscape I converted it to base64 using Mobilefish's converter (if you use this, make sure to set max characters per line to 0 so you don't have line breaks), and then added them as strings in a JSON array:

var activityURI = [
    {"activity":"All","uri":"oZWlnaH..."},
    {"activity":"Hiking","uri":"4bWxucz..."},
    ...
];

And then accessed them like this:

//for minor extra efficiency, set prefix of URI common to every item in array:
    var uriRoot = "data:image/svg+xml;base64,PD94bWwgdmVy...";

//set full URI using desired index:
    var uriPath = uriRoot + activityURI[//desired index# goes here\\].uri;
    var defPoiStyle = {
        externalGraphic: uriPath
    }

Drawbacks

  1. Long, ugly strings in my code (thank goodness for word wrap).
  2. Bigger JS file.
  3. Tedious to maintain SVG since Base64 converter must be used every time.

Benefits:

  1. It works in Chrome.
  2. No more individual HTTP requests for individual SVG files. EDIT: not true at all! I incorrectly assumed that this method would not trigger an additional HTTP request since it is included within my JS. Back to the drawing board...
  3. Base64 may be comparable in performance to unencoded SVG.

Here is a working result. Any thoughts on how to do this better? All ears.

I was unaware that support for SVG fragment identifiers is not so hot, so I found somewhat of a workaround in the form of Base64 encoding (thanks to these doppnet and CSS Tricks posts).

So, after optimizing my SVG in Inkscape I converted it to base64 using Mobilefish's converter (if you use this, make sure to set max characters per line to 0 so you don't have line breaks), and then added them as strings in a JSON array:

var activityURI = [
    {"activity":"All","uri":"oZWlnaH..."},
    {"activity":"Hiking","uri":"4bWxucz..."},
    ...
];

And then accessed them like this:

//for minor extra efficiency, set prefix of URI common to every item in array:
    var uriRoot = "data:image/svg+xml;base64,PD94bWwgdmVy...";

//set full URI using desired index:
    var uriPath = uriRoot + activityURI[//desired index# goes here\\].uri;
    var defPoiStyle = {
        externalGraphic: uriPath
    }

Drawbacks

  1. Long, ugly strings in my code (thank goodness for word wrap).
  2. Bigger JS file.
  3. Tedious to maintain SVG since Base64 converter must be used every time.

Benefits:

  1. It works in Chrome.
  2. No more individual HTTP requests for individual SVG files. EDIT: not true at all! I incorrectly assumed that this method would not trigger an additional HTTP request since it is included within my JS. Back to the drawing board...
  3. Base64 may be comparable in performance to unencoded SVG.

Here is a working result.

Corrected the part about the Base64 method not adding additional HTTP requests, which it does.
Source Link
abettermap
  • 431
  • 6
  • 11

I was unaware that support for SVG fragment identifiers is not so hot, so I found somewhat of a workaround in the form of Base64 encoding (thanks to these doppnet and CSS Tricks posts).

So, after optimizing my SVG in Inkscape I converted it to base64 using Mobilefish's converter (if you use this, make sure to set max characters per line to 0 so you don't have line breaks), and then added them as strings in a JSON array:

var activityURI = [
    {"activity":"All","uri":"oZWlnaH..."},
    {"activity":"Hiking","uri":"4bWxucz..."},
    ...
];

And then accessed them like this:

//for minor extra efficiency, set prefix of URI common to every item in array:
    var uriRoot = "data:image/svg+xml;base64,PD94bWwgdmVy...";

//set full URI using desired index:
    var uriPath = uriRoot + activityURI[//desired index# goes here\\].uri;
    var defPoiStyle = {
        externalGraphic: uriPath
    }

Drawbacks

  1. Long, ugly strings in my code (thank goodness for word wrap).
  2. Bigger JS file.
  3. Tedious to maintain SVG since Base64 converter must be used every time.

Benefits:

  1. It works in Chrome.
  2. No more individual HTTP requests for individual SVG files. EDIT: not true at all! I incorrectly assumed that this method would not trigger an additional HTTP request since it is included within my JS. Back to the drawing board...
  3. Base64 may be comparable in performance to unencoded SVG.

Here is a working result. Any thoughts on how to do this better? All ears.

I was unaware that support for SVG fragment identifiers is not so hot, so I found somewhat of a workaround in the form of Base64 encoding (thanks to these doppnet and CSS Tricks posts).

So, after optimizing my SVG in Inkscape I converted it to base64 using Mobilefish's converter (if you use this, make sure to set max characters per line to 0 so you don't have line breaks), and then added them as strings in a JSON array:

var activityURI = [
    {"activity":"All","uri":"oZWlnaH..."},
    {"activity":"Hiking","uri":"4bWxucz..."},
    ...
];

And then accessed them like this:

//for minor extra efficiency, set prefix of URI common to every item in array:
    var uriRoot = "data:image/svg+xml;base64,PD94bWwgdmVy...";

//set full URI using desired index:
    var uriPath = uriRoot + activityURI[//desired index# goes here\\].uri;
    var defPoiStyle = {
        externalGraphic: uriPath
    }

Drawbacks

  1. Long, ugly strings in my code (thank goodness for word wrap).
  2. Bigger JS file.
  3. Tedious to maintain SVG since Base64 converter must be used every time.

Benefits:

  1. It works in Chrome.
  2. No more individual HTTP requests for individual SVG files.
  3. Base64 may be comparable in performance to unencoded SVG.

Here is a working result. Any thoughts on how to do this better? All ears.

I was unaware that support for SVG fragment identifiers is not so hot, so I found somewhat of a workaround in the form of Base64 encoding (thanks to these doppnet and CSS Tricks posts).

So, after optimizing my SVG in Inkscape I converted it to base64 using Mobilefish's converter (if you use this, make sure to set max characters per line to 0 so you don't have line breaks), and then added them as strings in a JSON array:

var activityURI = [
    {"activity":"All","uri":"oZWlnaH..."},
    {"activity":"Hiking","uri":"4bWxucz..."},
    ...
];

And then accessed them like this:

//for minor extra efficiency, set prefix of URI common to every item in array:
    var uriRoot = "data:image/svg+xml;base64,PD94bWwgdmVy...";

//set full URI using desired index:
    var uriPath = uriRoot + activityURI[//desired index# goes here\\].uri;
    var defPoiStyle = {
        externalGraphic: uriPath
    }

Drawbacks

  1. Long, ugly strings in my code (thank goodness for word wrap).
  2. Bigger JS file.
  3. Tedious to maintain SVG since Base64 converter must be used every time.

Benefits:

  1. It works in Chrome.
  2. No more individual HTTP requests for individual SVG files. EDIT: not true at all! I incorrectly assumed that this method would not trigger an additional HTTP request since it is included within my JS. Back to the drawing board...
  3. Base64 may be comparable in performance to unencoded SVG.

Here is a working result. Any thoughts on how to do this better? All ears.

Source Link
abettermap
  • 431
  • 6
  • 11

I was unaware that support for SVG fragment identifiers is not so hot, so I found somewhat of a workaround in the form of Base64 encoding (thanks to these doppnet and CSS Tricks posts).

So, after optimizing my SVG in Inkscape I converted it to base64 using Mobilefish's converter (if you use this, make sure to set max characters per line to 0 so you don't have line breaks), and then added them as strings in a JSON array:

var activityURI = [
    {"activity":"All","uri":"oZWlnaH..."},
    {"activity":"Hiking","uri":"4bWxucz..."},
    ...
];

And then accessed them like this:

//for minor extra efficiency, set prefix of URI common to every item in array:
    var uriRoot = "data:image/svg+xml;base64,PD94bWwgdmVy...";

//set full URI using desired index:
    var uriPath = uriRoot + activityURI[//desired index# goes here\\].uri;
    var defPoiStyle = {
        externalGraphic: uriPath
    }

Drawbacks

  1. Long, ugly strings in my code (thank goodness for word wrap).
  2. Bigger JS file.
  3. Tedious to maintain SVG since Base64 converter must be used every time.

Benefits:

  1. It works in Chrome.
  2. No more individual HTTP requests for individual SVG files.
  3. Base64 may be comparable in performance to unencoded SVG.

Here is a working result. Any thoughts on how to do this better? All ears.