For whatever reason, I can't seem to get Firefox to render SVGs through CreateJS. Probably a bug, but I'm willing to fallback to raster graphics. The problem is I can't position a raster graphic vs an SVG the same way using the same code. What I mean is, the code to center an SVG in my canvas is not the same as the code to center a raster graphic. I'm not sure if this is intentional or not.
For the following code snippets, width
and height
are the width and height of the canvas element.
This works for raster graphics, but not SVGs:
let bounds = bitmap.getBounds();
bitmap.regX = bounds.width / 2;
bitmap.regY = bounds.height / 2;
bitmap.x = width / 2;
bitmap.y = height / 2;
stage.update();
This works for SVGs, but not raster graphics (would need to be modified if the SVG isn't square):
bitmap.regX = Math.min(width,height) / 2;
bitmap.regY = Math.min(width,height) / 2;
bitmap.x = width / 2;
bitmap.y = height / 2;
stage.update();
Here's an example on CodePen: https://codepen.io/CorySanin/pen/joOxMZ
Is there a simpler way to center these elements?