forked from airbnb/visx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request airbnb#231 from hshoff/harry-dragii
[drag][demo] add resetOnStart prop, add drawboard demo
- Loading branch information
Showing
8 changed files
with
1,080 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} | ||
} |
Oops, something went wrong.