-
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
59619a6
commit 0f8c835
Showing
14 changed files
with
501 additions
and
41 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
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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
import { | ||
PlaitBoard, | ||
PlaitPluginElementContext, | ||
OnContextChanged, | ||
RectangleClient, | ||
isSelectionMoving, | ||
ACTIVE_STROKE_WIDTH, | ||
} 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: () => ACTIVE_STROKE_WIDTH, | ||
getStrokeOpacity: () => 1, | ||
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 || value.hasThemeChanged) { | ||
this.generator.processDrawing(this.element, this.getElementG()); | ||
this.activeGenerator.processDrawing( | ||
this.element, | ||
PlaitBoard.getElementActiveHost(this.board), | ||
{ | ||
selected: this.selected, | ||
} | ||
); | ||
} else { | ||
const needUpdate = value.selected !== previous.selected; | ||
if (needUpdate) { | ||
this.activeGenerator.processDrawing( | ||
this.element, | ||
PlaitBoard.getElementActiveHost(this.board), | ||
{ | ||
selected: this.selected, | ||
} | ||
); | ||
} | ||
} | ||
} | ||
|
||
destroy(): void { | ||
super.destroy(); | ||
this.activeGenerator?.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,25 @@ | ||
import { DEFAULT_COLOR, Point } from '@plait/core'; | ||
import { PlaitCustomGeometry } from '@plait/draw'; | ||
|
||
export const DefaultFreehand = { | ||
strokeColor: DEFAULT_COLOR, | ||
strokeWidth: 4, | ||
}; | ||
|
||
export enum FreehandShape { | ||
nibPen = 'nibPen', | ||
feltTipPen = 'feltTipPen', | ||
artisticBrush = 'artisticBrush', | ||
markerHighlight = 'markerHighlight', | ||
} | ||
|
||
export const FREEHAND_TYPE = 'freehand'; | ||
|
||
export interface Freehand | ||
extends PlaitCustomGeometry<typeof FREEHAND_TYPE, Point[], FreehandShape> {} | ||
|
||
export const Freehand = { | ||
isFreehand: (value: any): value is Freehand => { | ||
return value.type === FREEHAND_TYPE; | ||
}, | ||
}; |
Oops, something went wrong.