-
Notifications
You must be signed in to change notification settings - Fork 34
/
stabilizer-pulled-string.tsx
136 lines (132 loc) · 4.43 KB
/
stabilizer-pulled-string.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React, { useRef, PointerEventHandler, useState, useEffect } from 'react';
import {
stroke as brush,
BrushStroke,
defaultBrushConfig,
BrushStrokeResult,
} from '@disjukr/croquis-js/lib/brush/simple';
import {
getStroke as getPulledStringStroke,
PulledStringDrawingContext,
} from '@disjukr/croquis-js/lib/stabilizer/pulled-string';
import type { StrokeDrawingContext } from '@disjukr/croquis-js/lib/stroke-protocol';
import { getStylusState, createStylusState } from '@disjukr/croquis-js/lib/stylus';
import PulledStringGuide from '../../components/guide/stabilizer/PulledStringGuide';
import Draw from '../../components/example/Draw';
import GithubCorner from '../../components/GithubCorner';
import DataController from '../../components/DataController';
import useCanvasFadeout from '../../misc/useCanvasFadeout';
import useWindowSize from '../../misc/useWindowSize';
import useForceUpdate from '../../misc/useForceUpdate';
const pulledString = getPulledStringStroke(brush);
const Page = () => {
const canvasRef = useRef<HTMLCanvasElement>(null);
useCanvasFadeout(canvasRef);
const windowSize = useWindowSize();
const [drawingContext, setDrawingContext] = useState<
StrokeDrawingContext<any, any, BrushStrokeResult>
>();
interface Config {
brushSize: number;
stringLength: number;
}
const [config, setConfig] = useState<Config>(() => ({
brushSize: 40,
stringLength: 100,
}));
const downHandler: PointerEventHandler = e => {
const ctx = canvasRef.current!.getContext('2d')!;
const stylusState = getStylusState(e.nativeEvent);
const brushConfig = {
...defaultBrushConfig,
ctx,
size: config.brushSize,
};
const drawingContext = pulledString.down(
{
stringLength: config.stringLength,
targetConfig: brushConfig,
},
stylusState
);
setDrawingContext(drawingContext);
};
useEffect(() => {
if (!drawingContext) return;
const moveHandler = (e: PointerEvent) => {
const stylusState = getStylusState(e);
drawingContext.move(stylusState);
};
const upHandler = (e: PointerEvent) => {
const stylusState = getStylusState(e);
drawingContext.up(stylusState);
setDrawingContext(undefined);
};
window.addEventListener('pointermove', moveHandler);
window.addEventListener('pointerup', upHandler);
return () => {
window.removeEventListener('pointermove', moveHandler);
window.removeEventListener('pointerup', upHandler);
};
}, [drawingContext]);
return (
<>
<canvas
ref={canvasRef}
onPointerDown={downHandler}
width={windowSize.width}
height={windowSize.height}
style={{
position: 'absolute',
top: 0,
left: 0,
touchAction: 'none',
}}
/>
<StabilizerGuide drawingContext={drawingContext} />
<Draw drawing={!!drawingContext} />
<GithubCorner
bannerColor="#000"
octoColor="#fff"
href="https://github.com/disjukr/croquis.js/blob/master/packages/website/src/pages/example/stabilizer-pulled-string.tsx"
/>
<DataController
className="data-controller"
data={config}
setData={setConfig}
config={{
brushSize: { label: 'Brush Size', type: 'range', min: 0, max: 100, step: 1 },
stringLength: { label: 'String Length', type: 'range', min: 0, max: 200, step: 1 },
}}
/>
</>
);
};
export default Page;
interface StabilizerGuideProps {
drawingContext?: PulledStringDrawingContext<BrushStroke>;
}
const defaultStylusState = createStylusState();
const StabilizerGuide: React.FC<StabilizerGuideProps> = props => {
const [stylusState, setStylusState] = useState(defaultStylusState);
const forceUpdate = useForceUpdate();
useEffect(() => {
const pointermove = (e: PointerEvent) => setStylusState(getStylusState(e));
const id = setInterval(forceUpdate, 10);
window.addEventListener('pointermove', pointermove);
return () => {
clearInterval(id);
window.removeEventListener('pointermove', pointermove);
};
}, []);
if (!props.drawingContext) return null;
return (
<PulledStringGuide
brushSize={props.drawingContext.getConfig(brush).size}
stylusState={stylusState}
follower={props.drawingContext.getState().follower}
stringLength={props.drawingContext.getConfig().stringLength}
style={{ position: 'absolute', top: 0, left: 0 }}
/>
);
};