Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjennings committed Nov 22, 2023
1 parent 861c636 commit 8e50a64
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ export default defineConfig({
{
label: 'Guides',
items: [
{
label: 'Animation',
collapsed: true,
autogenerate: {
directory: 'guides/animation',
},
},
{
label: 'Migrations',
collapsed: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
eventMode="static"
cursor="pointer"
hitArea={new PIXI.Circle(0, 0, circleSize)}
zIndex={10}
on:pointerdown={handleDragStart}
on:globalpointermove={handleDrag}
on:pointerup={handleDragEnd}
Expand Down
115 changes: 115 additions & 0 deletions docs/src/content/docs/guides/animation/svelte-motion.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
title: svelte/motion
---

You can animate using `svelte/motion` just like you would in other Svelte application.

## Spring

```svelte live
<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: 0, y: 0 }
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
y={100}
anchor={0.5}
text="Try clicking and dragging the mushroom"
style={{ fill: 'white', fontSize: 20 }}
/>
```

## Tween

```svelte live
<script>
import { onMount} from 'svelte'
import { Graphics, onTick } from 'svelte-pixi'
import { tweened } from 'svelte/motion'
import { cubicOut } from 'svelte/easing'
const barWidth = 200
const progress = tweened(barWidth / 5, {
duration: 400,
easing: cubicOut
})
let step = 0
onTick(() => {
step += 1
if (step > 100) {
step = 0
if ($progress < barWidth) {
$progress += barWidth / 5
} else {
$progress = barWidth / 5
}
}
})
</script>
<Graphics
pivot={{ x: barWidth / 2 }}
draw={graphics => {
graphics.clear()
graphics.beginFill(0xff0000)
graphics.drawRect(0, 0, $progress, 50)
graphics.endFill()
}}
/>
```

0 comments on commit 8e50a64

Please sign in to comment.