I keep getting an error when trying to load an SVG in Matter-JS. I am basically copying and pasting code from the example to try and get this to work. I have installed poly-decomp and am using webpack and npm. The error is:
"Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'numberOfItems')"
Here is the snippet of code where I am trying to load an SVG:
// module aliases
const Engine = Matter.Engine,
Common = Matter.Common,
Composite = Matter.Composite,
Vertices = Matter.Vertices,
Svg = Matter.Svg,
Bodies = Matter.Bodies;
// provide concave decomposition support library
Common.setDecomp(require('poly-decomp'));
const engine = Engine.create(),
world = engine.world;
if (typeof fetch !== 'undefined') {
var select = function(root, selector) {
return Array.prototype.slice.call(root.querySelectorAll(selector));
};
var loadSvg = function(url) {
return fetch(url)
.then(function(response) { return response.text(); })
.then(function(raw) { return (new window.DOMParser()).parseFromString(raw, 'image/svg+xml'); });
};
([
'./puzzle.svg'
]).forEach(function(path, i) {
loadSvg(path).then(function(root) {
// var color = Common.choose(['#f19648', '#f5d259', '#f55a3c', '#063e7b', '#ececd1']);
var vertexSets = select(root, 'path')
.map(function(path) { return Vertices.scale(Svg.pathToVertices(path, 30), 0.4, 0.4); });
Composite.add(world, Bodies.fromVertices(0,i * -100, vertexSets, {
isStatic: true
}, true));
});
});
loadSvg('./svg/svg.svg').then(function(root) {
// var color = Common.choose(['#f19648', '#f5d259', '#f55a3c', '#063e7b', '#ececd1']);
var vertexSets = select(root, 'path')
.map(function(path) { return Svg.pathToVertices(path, 30); });
Composite.add(world, Bodies.fromVertices(400, 80, vertexSets, {
isStatic: true
}, true));
});
} else {
Common.warn('Fetch is not available. Could not load SVG.');
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/itxMNS5
Comments
Post a Comment