Skip to content

Commit

Permalink
docs(demo): add special rotation examples
Browse files Browse the repository at this point in the history
  • Loading branch information
timmywil committed Jan 16, 2020
1 parent e0e8190 commit c014fd1
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
4 changes: 3 additions & 1 deletion demo/Demo.tsx
Expand Up @@ -2,11 +2,12 @@ import React from 'react'

interface Props {
title: string
subtitle?: string
code: React.ReactNode
children: React.ReactNode
}

export default function Demo({ title, code, children }: Props) {
export default function Demo({ title, subtitle, code, children }: Props) {
return (
<div className="demo">
<a
Expand All @@ -16,6 +17,7 @@ export default function Demo({ title, code, children }: Props) {
<h2>{title}</h2>
</a>
{code}
{subtitle && <p>{subtitle}</p>}
<div className="live-example">{children}</div>
</div>
)
Expand Down
4 changes: 4 additions & 0 deletions demo/demo.css
Expand Up @@ -38,6 +38,10 @@ body {
margin-bottom: 30px;
border-radius: 5px;
}
.demo p {
margin: 0 0 1em;
font-size: 18px;
}
pre {
border-radius: 5px;
margin: 0 0 20px !important;
Expand Down
2 changes: 1 addition & 1 deletion demo/examples/DisabledYAxis.tsx
Expand Up @@ -5,7 +5,7 @@ import Demo from '../Demo'

const code = <Code>{`Panzoom(elem, { disableYAxis: true })`}</Code>

export default function ContainInside() {
export default function DisabledYAxis() {
const elem = useRef<HTMLDivElement>(null)
useEffect(() => {
Panzoom(elem.current, { disableYAxis: true })
Expand Down
57 changes: 57 additions & 0 deletions demo/examples/Rotate.tsx
@@ -0,0 +1,57 @@
import React, { useEffect, useRef } from 'react'
import Panzoom from '../../src/panzoom'
import Code from '../Code'
import Demo from '../Demo'

const code = (
<Code>{`const panzoom = Panzoom(elem, {
setTransform: (_, { scale, x, y }) => {
panzoom.setStyle('transform', \`rotate($\{x / 20}deg) scale($\{scale}) translate($\{x}px, $\{y}px)\`)
}
})`}</Code>
)

const code2 = <Code>{`Panzoom(elem)`}</Code>

export default function Rotate() {
const elem = useRef<HTMLDivElement>(null)
const elem2 = useRef<HTMLDivElement>(null)
useEffect(() => {
const panzoom = Panzoom(elem.current, {
setTransform: (_: HTMLElement, { scale, x, y }: { scale: number; x: number; y: number }) => {
panzoom.setStyle(
'transform',
`rotate(${x / 20}deg) scale(${scale}) translate(${x}px, ${y}px)`
)
}
})
Panzoom(elem2.current)
}, [])
return (
<>
<Demo
title="Adding on matrix functions to the transform"
subtitle="Warning: the setTransform option should be used with caution. Most cases can be handled by adding a parent element and applying any special transforms to the child (see the next example)."
code={code}>
<div className="panzoom-parent">
<div className="panzoom" ref={elem} style={{ width: '400px', margin: '0 auto' }}>
<img style={{ width: '100%', height: '100%' }} src="awesome_tiger.svg" />
</div>
</div>
</Demo>
<Demo
title="Adding a transform to a child"
subtitle="A rotation of 180deg is applied to the image tag. It's almost always better to add any special transforms to a child of the panzoom element instead of the panzoom element."
code={code2}>
<div className="panzoom-parent">
<div className="panzoom" ref={elem2} style={{ width: '400px', margin: '0 auto' }}>
<img
style={{ width: '100%', height: '100%', transform: 'rotate(180deg)' }}
src="awesome_tiger.svg"
/>
</div>
</div>
</Demo>
</>
)
}
2 changes: 1 addition & 1 deletion demo/examples/Standard.tsx
Expand Up @@ -17,7 +17,7 @@ rangeInput.addEventListener('input', (event) => {
</Code>
)

export default function Buttons() {
export default function Standard() {
const elem = useRef<HTMLDivElement>(null)
const range = useRef<HTMLInputElement>(null)
const panzoomRef = useRef<PanzoomObject>(null)
Expand Down
2 changes: 2 additions & 0 deletions demo/index.tsx
Expand Up @@ -10,6 +10,7 @@ import React from 'react'
import ReactDOM from 'react-dom'
import SVG from './examples/SVG'
import Standard from './examples/Standard'
import Rotate from './examples/Rotate'

function Demos() {
return (
Expand All @@ -32,6 +33,7 @@ function Demos() {
<ContainInside />
<ContainOutside />
<DisabledYAxis />
<Rotate />
</div>
)
}
Expand Down

0 comments on commit c014fd1

Please sign in to comment.