Skip to content

Commit

Permalink
Merge pull request airbnb#231 from hshoff/harry-dragii
Browse files Browse the repository at this point in the history
 [drag][demo] add resetOnStart prop, add drawboard demo
  • Loading branch information
hshoff authored Jan 17, 2018
2 parents fc34704 + c20c13b commit 3f660b6
Show file tree
Hide file tree
Showing 8 changed files with 1,080 additions and 24 deletions.
40 changes: 39 additions & 1 deletion packages/vx-demo/components/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Tilt from 'react-tilt';
import Link from 'next/link';
import { ParentSize } from '@vx/responsive';
import { extent, max } from 'd3-array';
import drawData from './util/drawData';

import Page from '../components/page';
import Footer from '../components/footer';
Expand Down Expand Up @@ -35,6 +36,7 @@ import Treemap from '../components/tiles/treemap';
import Radar from '../components/tiles/radar';
import Responsive from '../components/tiles/responsive';
import DragI from '../components/tiles/drag-i';
import DragII from '../components/tiles/drag-ii';

const items = [
'#242424',
Expand Down Expand Up @@ -911,7 +913,43 @@ export default class Gallery extends React.Component {
</div>
</Link>
</Tilt>
<div className="gallery-item placeholder" />
<Tilt className="tilt" options={{ max: 8, scale: 1 }}>
<Link prefetch href="/drag-ii">
<div
className="gallery-item"
style={{
background: '#04002b',
borderRadius: '14px',
}}
>
<div className="image">
<ParentSize>
{({ width, height }) => (
<DragII
width={543}
height={390}
events={false}
data={drawData}
/>
)}
</ParentSize>
</div>
<div
className="details"
style={{
color: '#ff614e',
zIndex: 1,
}}
>
<div className="title">Drag</div>
<div className="description">
<pre>{`<Drag.Drag />`}</pre>
</div>
</div>
</div>
</Link>
</Tilt>
{false && <div className="gallery-item placeholder" />}
</div>

<div>
Expand Down
9 changes: 7 additions & 2 deletions packages/vx-demo/components/tiles/drag-i.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React from 'react';
import { localPoint } from '@vx/event';
import { scaleOrdinal } from '@vx/scale';
import { withParentSize } from '@vx/responsive';
import { LinearGradient } from '@vx/gradient';
import { Drag, raise } from '@vx/drag';

Expand Down Expand Up @@ -52,6 +50,10 @@ export default class DragI extends React.Component {
});
}

componentDidMount() {
this.forceUpdate();
}

render() {
const { width, height } = this.props;
return (
Expand Down Expand Up @@ -108,6 +110,9 @@ export default class DragI extends React.Component {
onMouseMove={dragMove}
onMouseUp={dragEnd}
onMouseDown={dragStart}
onTouchStart={dragStart}
onTouchMove={dragMove}
onTouchEnd={dragEnd}
/>
);
}}
Expand Down
150 changes: 150 additions & 0 deletions packages/vx-demo/components/tiles/drag-ii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import React from 'react';
import { LinePath } from '@vx/shape';
import { localPoint } from '@vx/event';
import { Drag } from '@vx/drag';
import { curveBasis } from '@vx/curve';
import { LinearGradient } from '@vx/gradient';

export default class DragII extends React.Component {
constructor(props) {
super(props);
this.state = {
data: props.data || [],
};
}
componentDidMount() {
this.forceUpdate();
}
render() {
const { width, height } = this.props;
return (
<div className="DragII">
<svg width={width} height={height} ref={s => (this.svg = s)}>
<LinearGradient id="stroke" from="#ff614e" to="#ffdc64" />
<rect
fill="#04002b"
width={width}
height={height}
rx={14}
/>
{this.state.data.map((d, i) => {
return (
<LinePath
key={`line-${i}`}
stroke="url(#stroke)"
strokeWidth={3}
data={d}
curve={curveBasis}
x={d => d.x}
y={d => d.y}
xScale={d => d}
yScale={d => d}
/>
);
})}
<Drag
svg={this.svg}
width={width}
height={height}
resetOnStart={true}
onDragStart={({ x, y }) => {
// add the new line with the starting point
this.setState((state, props) => {
const newLine = [{ x, y }];
return {
data: state.data.concat([newLine]),
};
});
}}
onDragMove={({ x, y, dx, dy }) => {
// add the new point to the current line
this.setState((state, props) => {
const nextData = [...state.data];
const point = [{ x: x + dx, y: y + dy }];
const i = nextData.length - 1;
nextData[i] = nextData[i].concat(point);
return { data: nextData };
});
}}
>
{({
x,
y,
dx,
dy,
isDragging,
dragStart,
dragEnd,
dragMove,
}) => {
return (
<g>
{/* decorate the currently drawing line */}
{isDragging && (
<g>
<rect
fill="white"
width={8}
height={8}
x={x + dx - 4}
y={y + dy - 4}
style={{ pointerEvents: 'none' }}
/>
<circle
cx={x}
cy={y}
r={4}
fill="transparent"
stroke="white"
style={{ pointerEvents: 'none' }}
/>
</g>
)}
{/* create the drawing area */}
<rect
fill="transparent"
width={width}
height={height}
onMouseDown={dragStart}
onMouseUp={dragEnd}
onMouseMove={dragMove}
/>
</g>
);
}}
</Drag>
</svg>
<div className="deets">
<div>
Based on Mike Bostock's{' '}
<a href="https://bl.ocks.org/mbostock/f705fc55e6f26df29354">
Line Drawing
</a>
</div>
</div>

<style jsx>{`
.DragII {
display: flex;
flex-direction: column;
user-select: none;
}
svg {
margin: 1rem 0;
cursor: crosshair;
}
.deets {
display: flex;
flex-direction: row;
font-size: 12px;
}
.deets > div {
margin: 0.25rem;
}
`}</style>
</div>
);
}
}
Loading

0 comments on commit 3f660b6

Please sign in to comment.