I have a routine that attaches an image to my SVG canvas and on load, it creates a rectangle with rounded corners so I can clip the image. This is run multiple times, fed by an array of image addresses.
Method 1 - clipWith()
The clipping does work (the final image looks fine) but it's throwing errors in the console on the on the clipWith() line:
this.root() is null
Errors occur with each run through the same array of images but on different images each time.
const newImage = newNested.image(imageData, (e) => {
const newWidth = e.target.naturalHeight;
const newHeight = e.target.naturalWidth;
const clipper = newNested.clip();
clipper.rect(newWidth, newHeight).attr({ rx: 5, ry: 5 }).fill('#000');
newImage.clipWith(clipper);
}
Method 2 - maskWith()
Same error as above whilst the output SVG is fine:
const newImage = newNested.image(imageData, (e) => {
const newWidth = e.target.naturalHeight;
const newHeight = e.target.naturalWidth;
const masker = newNested.rect(newWidth, newHeight).attr({ rx: 5, ry: 5 }).fill('#000');
newImage.maskWith(masker);
}
Method 3 - SVG.ClipPath()
I have also looked through the docs and the Github issues and found other ways to render a clipping mask:
const newImage = newNested.image(imageData, (e) => {
const newWidth = e.target.naturalHeight;
const newHeight = e.target.naturalWidth;
const clipPath = new SVG.ClipPath();
clipPath.rect(newWidth, newHeight).attr({ rx: imageNode.r, ry: imageNode.r }).fill('#000');
newImage.clipWith(clipPath);
}
Yet this throws the following error:
Uncaught TypeError: _svgdotjs_svg_js__WEBPACK_IMPORTED_MODULE_2__.SVG.ClipPath is not a constructor
And from looking at the docs, there is this one line that doesn't explain if this is related but trying it with SVGElement.ClipPath() got the same result:
Note: All properties that were former available on the global SVG object need to be imported now, see example below:
import { SVG, extend as SVGextend, Element as SVGElement } from'@svgdotjs/svg.js'