-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(freehand): initialize freehand #2
- Loading branch information
1 parent
61dbd52
commit eb6c07a
Showing
12 changed files
with
281 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
57 changes: 57 additions & 0 deletions
57
packages/drawnix/src/plugins/freehand/freehand.component.ts
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,57 @@ | ||
import { | ||
PlaitBoard, | ||
PlaitPluginElementContext, | ||
OnContextChanged, | ||
RectangleClient, | ||
isSelectionMoving, | ||
} from '@plait/core'; | ||
import { ActiveGenerator, CommonElementFlavour } from '@plait/common'; | ||
import { Freehand } from './type'; | ||
import { FreehandGenerator } from './freehand.generator'; | ||
|
||
export class FreehandComponent | ||
extends CommonElementFlavour<Freehand, PlaitBoard> | ||
implements OnContextChanged<Freehand, PlaitBoard> | ||
{ | ||
constructor() { | ||
super(); | ||
} | ||
|
||
activeGenerator!: ActiveGenerator<Freehand>; | ||
|
||
generator!: FreehandGenerator; | ||
|
||
initializeGenerator() { | ||
this.activeGenerator = new ActiveGenerator<Freehand>(this.board, { | ||
getRectangle: (element: Freehand) => { | ||
return RectangleClient.getRectangleByPoints(element.points); | ||
}, | ||
getStrokeWidth: () => 0, | ||
getStrokeOpacity: () => 0, | ||
hasResizeHandle: () => { | ||
return !isSelectionMoving(this.board); | ||
}, | ||
}); | ||
this.generator = new FreehandGenerator(this.board); | ||
} | ||
|
||
initialize(): void { | ||
super.initialize(); | ||
this.initializeGenerator(); | ||
this.generator.processDrawing(this.element, this.getElementG()); | ||
} | ||
|
||
onContextChanged( | ||
value: PlaitPluginElementContext<Freehand, PlaitBoard>, | ||
previous: PlaitPluginElementContext<Freehand, PlaitBoard> | ||
) { | ||
if (value.element !== previous.element) { | ||
} else { | ||
const hasSameSelected = value.selected === previous.selected; | ||
} | ||
} | ||
|
||
destroy(): void { | ||
super.destroy(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/drawnix/src/plugins/freehand/freehand.generator.ts
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,22 @@ | ||
import { Generator } from '@plait/common'; | ||
import { PlaitBoard, setStrokeLinecap } from '@plait/core'; | ||
import { Options } from 'roughjs/bin/core'; | ||
import { DefaultFreehand, Freehand } from './type'; | ||
|
||
export interface FreehandData {} | ||
|
||
export class FreehandGenerator extends Generator<Freehand, FreehandData> { | ||
protected draw( | ||
element: Freehand, | ||
data?: FreehandData | undefined | ||
): SVGGElement | undefined { | ||
let option: Options = { ...DefaultFreehand }; | ||
const g = PlaitBoard.getRoughSVG(this.board).curve(element.points, option); | ||
setStrokeLinecap(g, 'round'); | ||
return g; | ||
} | ||
|
||
canDraw(element: Freehand, data: FreehandData): boolean { | ||
return true; | ||
} | ||
} |
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,30 @@ | ||
import { StrokeStyle } from '@plait/common'; | ||
import { DEFAULT_COLOR, PlaitElement, Point } from '@plait/core'; | ||
|
||
export const DefaultFreehand = { | ||
strokeColor: DEFAULT_COLOR, | ||
strokeWidth: 4, | ||
}; | ||
|
||
export enum FreehandShape { | ||
nibPen = 'nibPen', | ||
feltTipPen = 'feltTipPen', | ||
artisticBrush = 'artisticBrush', | ||
markerHighlight = 'markerHighlight', | ||
} | ||
|
||
export interface Freehand extends PlaitElement { | ||
type: 'freehand'; | ||
points: Point[]; | ||
shape: FreehandShape; | ||
strokeColor?: string; | ||
strokeWidth?: number; | ||
strokeStyle?: StrokeStyle; | ||
fill?: string; | ||
} | ||
|
||
export const Freehand = { | ||
isFreehand: (value: any): value is Freehand => { | ||
return value.type === 'freehand'; | ||
}, | ||
}; |
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,21 @@ | ||
import { idCreator, Point } from '@plait/core'; | ||
import { DefaultFreehand, Freehand, FreehandShape } from './type'; | ||
|
||
export function getFreehandPointers() { | ||
return [FreehandShape.feltTipPen]; | ||
} | ||
|
||
export const createFreehandElement = ( | ||
shape: FreehandShape, | ||
points: Point[] | ||
): Freehand => { | ||
const element: Freehand = { | ||
id: idCreator(), | ||
type: 'freehand', | ||
shape, | ||
points, | ||
strokeWidth: DefaultFreehand.strokeWidth, | ||
strokeColor: DefaultFreehand.strokeColor, | ||
}; | ||
return element; | ||
}; |
85 changes: 85 additions & 0 deletions
85
packages/drawnix/src/plugins/freehand/with-freehand-create.ts
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,85 @@ | ||
import { | ||
PlaitBoard, | ||
Point, | ||
Transforms, | ||
createG, | ||
throttleRAF, | ||
toHostPoint, | ||
toViewBoxPoint, | ||
} from '@plait/core'; | ||
import { isDrawingMode } from '@plait/common'; | ||
import { createFreehandElement, getFreehandPointers } from './utils'; | ||
import { Freehand, FreehandShape } from './type'; | ||
import { FreehandGenerator } from './freehand.generator'; | ||
|
||
export const withFreehandCreate = (board: PlaitBoard) => { | ||
const { pointerDown, pointerMove, pointerUp, globalPointerUp } = board; | ||
|
||
let isDrawing: boolean = false; | ||
|
||
let points: Point[] = []; | ||
|
||
const generator = new FreehandGenerator(board); | ||
|
||
let temporaryElement: Freehand | null = null; | ||
|
||
const complete = (cancel?: boolean) => { | ||
if (isDrawing) { | ||
const pointer = PlaitBoard.getPointer(board) as FreehandShape; | ||
temporaryElement = createFreehandElement(pointer, points); | ||
} | ||
if (temporaryElement && !cancel) { | ||
Transforms.insertNode(board, temporaryElement, [board.children.length]); | ||
} | ||
generator?.destroy(); | ||
temporaryElement = null; | ||
isDrawing = false; | ||
points = []; | ||
}; | ||
|
||
board.pointerDown = (event: PointerEvent) => { | ||
const freehandPointers = getFreehandPointers(); | ||
const isFreehandPointer = PlaitBoard.isInPointer(board, freehandPointers); | ||
if (isFreehandPointer && isDrawingMode(board)) { | ||
isDrawing = true; | ||
const point = toViewBoxPoint(board, toHostPoint(board, event.x, event.y)); | ||
points.push(point); | ||
} | ||
pointerDown(event); | ||
}; | ||
|
||
board.pointerMove = (event: PointerEvent) => { | ||
if (isDrawing) { | ||
throttleRAF(board, 'with-freehand-creation', () => { | ||
generator?.destroy(); | ||
const newPoint = toViewBoxPoint( | ||
board, | ||
toHostPoint(board, event.x, event.y) | ||
); | ||
points.push(newPoint); | ||
const pointer = PlaitBoard.getPointer(board) as FreehandShape; | ||
|
||
temporaryElement = createFreehandElement(pointer, points); | ||
generator.processDrawing( | ||
temporaryElement, | ||
PlaitBoard.getElementActiveHost(board) | ||
); | ||
}); | ||
return; | ||
} | ||
|
||
pointerMove(event); | ||
}; | ||
|
||
board.pointerUp = (event: PointerEvent) => { | ||
complete(); | ||
pointerUp(event); | ||
}; | ||
|
||
board.globalPointerUp = (event: PointerEvent) => { | ||
complete(true); | ||
globalPointerUp(event); | ||
}; | ||
|
||
return board; | ||
}; |
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,29 @@ | ||
import { | ||
PlaitBoard, | ||
PlaitElement, | ||
PlaitPluginElementContext, | ||
RectangleClient, | ||
} from '@plait/core'; | ||
import { Freehand } from './type'; | ||
import { FreehandComponent } from './freehand.component'; | ||
import { withFreehandCreate } from './with-freehand-create'; | ||
|
||
export const withFreehand = (board: PlaitBoard) => { | ||
const { getRectangle, drawElement } = board; | ||
|
||
board.drawElement = (context: PlaitPluginElementContext) => { | ||
if (Freehand.isFreehand(context.element)) { | ||
return FreehandComponent; | ||
} | ||
return drawElement(context); | ||
}; | ||
|
||
board.getRectangle = (element: PlaitElement) => { | ||
if (Freehand.isFreehand(element)) { | ||
return RectangleClient.getRectangleByPoints(element.points); | ||
} | ||
return getRectangle(element); | ||
}; | ||
|
||
return withFreehandCreate(board); | ||
}; |
Oops, something went wrong.