<script>
import * as PIXI from 'pixi.js'
import { Text, Sprite } from 'svelte-pixi'
import { spring } from 'svelte/motion'
let dragging = false
let offset = { x: 0, y: 0 }
let startingPosition = { x: 360, y: 200 }
let position = spring(startingPosition, {
stiffness: 0.2,
damping: 0.4,
})
function handleDragStart(event) {
dragging = true
const { x, y } = event.detail.data.global
offset = {
x: x - $position.x,
y: y - $position.y,
}
}
function handleDrag(event) {
if (dragging) {
const { x, y } = event.detail.data.global
position.update(
($position) => ({
x: x - offset.x,
y: y - offset.y,
}),
{ hard: true },
)
}
}
function handleDragEnd() {
dragging = false
position.set(startingPosition, { soft: 1 })
}
</script>
<Sprite
texture={PIXI.Texture.from('/assets/mushroom.png')}
anchor={0.5}
x={$position.x}
y={$position.y}
rotation={($position.x - startingPosition.x) * 0.02}
eventMode="static"
cursor="pointer"
zIndex={10}
on:pointerdown={handleDragStart}
on:pointerup={handleDragEnd}
on:pointerupoutside={handleDragEnd}
on:globalpointermove={handleDrag}
/>
<Text
x={360}
y={300}
anchor={0.5}
text="Click and drag the mushroom"
style={{ fill: 'white', fontSize: 20 }}
/>