svg rect to polygon points & points order bug
svg rect to polygon points & points order bug
https://codepen.io/xgqfrms/pen/vYOWjYr?editors=1000
points 数据类型转换 bug, string + string !== number + number
https://codepen.io/xgqfrms/pen/vYOWjYr?editors=1000
error
const x = rect.getAttribute('x');
const y = rect.getAttribute('y');
const width = rect.getAttribute('width');
const height = rect.getAttribute('height');
const points = [];
points.push(pointGenerator(x, y));
points.push(pointGenerator((x + width), y));
points.push(pointGenerator((x + width), (y + height)));
points.push(pointGenerator(x, (y + height)));
OK
const x = +rect.getAttribute('x');
const y = +rect.getAttribute('y');
const width = +rect.getAttribute('width');
const height = +rect.getAttribute('height');
const points = [];
points.push(pointGenerator(x, y));
points.push(pointGenerator((x + width), y));
points.push(pointGenerator((x + width), (y + height)));
points.push(pointGenerator(x, (y + height)));
object map to array, order change bug
bug
const pointGenerator = (x, y) => {
// return [
// x,
// y,
// ];
return {
x,
y,
};
}
// const points_str = points.flat(2).join(` `);
const points_str = points.map(({x, y}) => [x, y]).flat(2).join(` `);
ok
const pointGenerator = (x, y) => {
return [
x,
y,
];
// return {
// x,
// y,
// };
}
const points_str = points.flat(2).join(` `);
// const points_str = points.map(({x, y}) => [x, y]).flat(2).join(` `);
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/12440243.html
未经授权禁止转载,违者必究!