xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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(` `);


posted @ 2020-03-08 02:56  xgqfrms  阅读(275)  评论(5编辑  收藏  举报