跳到主要内容

绑定 config prop

如果绑定了 prop,Svelte-Konva 可以在 dragendtransformend 事件结束后,使部分 props 与 Konva 的内部 state(位置、旋转、缩放等)保持同步。

禁用自动同步

多数情况下,svelte-konva 监听 dragendtransformend 事件的默认行为正是你需要的行为。但是,在某些情况下(主要出于性能考虑),此行为可能没有帮助。这时,可以向组件传入 staticConfig prop 以禁用此行为。此时,svelte-konva 不会监听这些事件,也不会更新绑定的 props:

<script>
import { Stage, Layer, Rect } from 'svelte-konva';

// x and y values will not be synced with actual position after dragend even if bound
const config = { x: 100, y: 100, width: 400, height: 200, fill: 'blue', draggable: true };
</script>

<Stage width={window.innerWidth} height={window.innerHeight}>
<Layer>
<Rect {...config} staticConfig />
</Layer>
</Stage>

请注意,svelte-konva 只在组件初始化期间计算一次 staticConfig prop。组件初始化后,再更改 staticConfig prop 不会产生任何效果。

拖动不同的圆环并观察 Svelte 触发的响应式变化。请注意,只有绑定的圆环(黄色)会在 dragend 时自动更改坐标。