Skip to content

Commit

Permalink
SvgImageLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Nov 28, 2023
1 parent 82893e7 commit a422c7c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
35 changes: 35 additions & 0 deletions haxe/ui/backend/html5/loaders/image/SvgImageLoader.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package haxe.ui.backend.html5.loaders.image;

import haxe.ui.assets.ImageInfo;
import haxe.ui.loaders.image.ImageLoaderBase;
import haxe.ui.util.Variant;
import js.Browser;
import js.html.Blob;
import js.html.Image;
import js.html.URL;

using StringTools;

class SvgImageLoader extends ImageLoaderBase {
public override function load(resource:Variant, callback:ImageInfo->Void) {
var image = new Image();
var svgData:String = resource.toString();
if (svgData.startsWith("svg://")) {
svgData = svgData.substr("svg://".length);
}
var svg = new Blob([svgData], {type: "image/svg+xml;charset=utf-8"});
var url = URL.createObjectURL(svg);
image.onload = function() {
callback({
data:image,
width: image.width,
height: image.height
});
}
image.onerror = function(e) {
trace(e);
callback(null);
}
image.src = url;
}
}
6 changes: 6 additions & 0 deletions haxe/ui/backend/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
</native>
</themes>

<loaders>
<image-loaders>
<image-loader prefix="svg" pattern="^&lt;svg.*&gt;.*&lt;\/svg&gt;$" class="haxe.ui.backend.html5.loaders.image.SvgImageLoader" singleInstance="true" />
</image-loaders>
</loaders>

<properties>
<property name="haxe.ui.containers.menus.menubar.style" value="rounded" />
<property name="haxe.ui.containers.menus.menu.style" value="rounded" />
Expand Down

0 comments on commit a422c7c

Please sign in to comment.