UPDATE:我已经算上了REPL with the code and data here分.

我正试图使用Svelte和Flubber来重建demo example个显示变形Geojson形状的图形.我不喜欢使用D3,我找到了一个example,它向我展示了如何使用tweened进行插补.

我的topo.json,包含数据,被上传here.

这就是我到目前为止所拥有的:

<script>
   import { interpolate } from 'flubber';
  import { feature } from 'topojson';
  import { tweened } from 'svelte/motion';
  import * as eases from 'svelte/easing';
  import wards from '$lib/Wards/shapes/map.topo.json';

  const shape = tweened(undefined, {
    interpolate,
    easing: eases.cubicInOut,
    duration: 400,
  });

  let places = feature(wards, wards.objects.states).features.map(function (d) {
    return d.geometry.coordinates[0];
  });

  // Convert the polygon coordinates to a string format
  places = places.map(function (d) {
    return d.map(function (d) {
      return d.join(',');
    });
  });
  let selected = 0;
  $: $shape = places[selected];
</script>

{#each Object.keys(places) as key}
  <button
     class:selected="{selected === key}"
     on:click="{() => (selected = key)}"
   >
  {key}
  </button>
{/each}

<main>
   <svg viewBox="0 0 960 500">
      <path d="{$shape}"></path>
   </svg>
</main>

当我运行此命令时,我在控制台中收到以下错误:

Error: <path> attribute d: Expected moveto path command ('M' or 'm'), "77.4618079999067…".

Uncaught TypeError: All shapes must be supplied as arrays of [x, y] points or an SVG path string (https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d).
Example valid ways of supplying a shape would be:
[[0, 0], [10, 0], [10, 10]]
"M0,0 L10,0 L10,10Z"

我做错了什么?我怀疑这与插补函数有关,但我不确定是什么.我是否需要将点数组转换为传统的SVG路径("M0,0 L10,0 L10,10Z" )?

推荐答案

Here's a REPL with the fixed code: https://svelte.dev/repl/6f98e177b5bf416e8a9c269a37b08d7d?version=4.0.0
For posterity, here's the fixed contents:

<script>
  import flubber from 'flubber';
  import { feature } from 'topojson';
  import { tweened } from 'svelte/motion';
  import * as eases from 'svelte/easing';
  import wards from './wards.topo.json';

  const shape = tweened(undefined, {
    interpolate: flubber.interpolate,
    easing: eases.cubicInOut,
    duration: 400,
  });

  let places = feature(wards, wards.objects.collection).features.map(function (
    d
  ) {
    return d.geometry.coordinates[0];
  });

  // for each place, // Convert the polygon coordinates to a string format
  places = places.map(function (d) {
    const points = d[0].map((p, i) => {
      const x = p[0].toFixed(4), y = p[1].toFixed(4)
      if (i == 0) return `M${x},${y}`
      return `L${x},${y}`
    });
    return points.join(" ") + "Z";
  });

  let selected = 0;
  $: $shape = s;
</script>

{#each Object.keys(places) as key}
  <button
    class:selected="{selected === key}"
    on:click="{() => (selected = key)}"
  >
    {key}
  </button>
{/each}

<main>
  <svg viewBox="77 12 2 2">
    <path d={$shape}></path>
  </svg>
</main>

<style>
  svg {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
  }

  path {
    fill: #0074d9;
  }

  button {
    margin: 0 0.5em 0 0;
  }

  .selected {
    background-color: #333;
    color: white;
  }
</style>

Javascript相关问答推荐

通过实现regex逻辑自定义数据表搜索

materialized - activeIndex返回-1

如何在Connect 4游戏中 for each 玩家使用位板寻找7形状?

扫描qr code后出错whatter—web.js

在这种情况下,如何 for each 元素添加id?

将字符串UTC Date转换为ngx—counting leftTime配置值的数字

MathJax可以导入本地HTML文档使用的JS文件吗?

引用在HTMLAttributes<;HTMLDivElement>;中不可用

显示图—如何在图例项上添加删除线效果?

我的角模板订阅后不刷新'

还原器未正确更新状态

如何在文本字段中输入变量?

Angular 形式,从DOM中删除不会删除指定索引处的内容,但会删除最后一项

JWT Cookie安全性

在HTML5画布上下文中使用putImageData时,重载解析失败

未找到用于 Select 器的元素:in( puppeteer 师错误)

限制数组中每个元素的长度,

输入数据覆盖JSON文件

需要从对象生成列表

如何在单击Search按钮时更新Reaction组件?