Skip to content

Commit

Permalink
feat: init rough
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-zone committed Mar 13, 2024
1 parent c5ec7e3 commit d291ef4
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 13,968 deletions.
13,648 changes: 0 additions & 13,648 deletions package-lock.json

This file was deleted.

4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ export const IMAGE_CLIPPATH_QUALITY = false;
export const MAX_HISTORY_LENGTH = 100;

export const PANEL_WIDTH = 360;
export const SETTER_WIDTH = 280;
export const SETTER_WIDTH = 280;

export const CAPTURE_SUBTARGET_WHEN_DBLCLICK = false;
1 change: 1 addition & 0 deletions src/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface IGloablStateContext {
isReady?: boolean;
setReady?: (o: boolean) => void;
editor?: Editor;
roughSvg?: any;
}

export const GloablStateContext = createContext<IGloablStateContext>(null);
Empty file.
12 changes: 7 additions & 5 deletions src/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import FabricHistory from './extensions/history';
import AutoSave from './extensions/autosave';
import { createGroup } from './objects/group';
import createCustomClass from './custom-objects';
import { HOVER_OBJECT_CORNER, HOVER_OBJECT_CONTROL } from '@/config';
import { HOVER_OBJECT_CORNER, HOVER_OBJECT_CONTROL, CAPTURE_SUBTARGET_WHEN_DBLCLICK } from '@/config';

export default class Editor {
public canvas: fabric.Canvas;
Expand Down Expand Up @@ -222,10 +222,12 @@ export default class Editor {
if (subTarget.type === 'f-text') {
this._editTextInGroup(target, subTarget);
} else {
subTarget.set('hasControls', false);
this.canvas.discardActiveObject();
this.canvas.setActiveObject(subTarget);
this.canvas.requestRenderAll();
if (CAPTURE_SUBTARGET_WHEN_DBLCLICK) {
subTarget.set('hasControls', false);
this.canvas.discardActiveObject();
this.canvas.setActiveObject(subTarget);
this.canvas.requestRenderAll();
}
}
}
});
Expand Down
33 changes: 32 additions & 1 deletion src/fabritor/UI/panel/ShapePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Flex } from 'antd';
import Title from '@/fabritor/components/Title';
import LineTypeList from './line-type-list';
import ShapeTypeList from './shape-type-list';
import RoughTypeList from './rough-type-list';
import { drawArrowLine, drawLine, drawTriArrowLine } from '@/editor/objects/line';
import createRect from '@/editor/objects/rect';
import createShape from '@/editor/objects/shape';
Expand All @@ -10,7 +11,7 @@ import { GloablStateContext } from '@/context';
import { createPathFromSvg } from '@/editor/objects/path';

export default function ShapePanel () {
const { editor } = useContext(GloablStateContext);
const { editor, roughSvg } = useContext(GloablStateContext);

const addLine = (item) => {
const { type, options = {} } = item;
Expand Down Expand Up @@ -48,6 +49,22 @@ export default function ShapePanel () {
}
}

const addRough = (item) => {
const { key, elem, options } = item;
const canvas = editor.canvas;

const r = roughSvg.rectangle(0, 0, 400, 400, {
fill: '#F6C445',
stroke: '#EC6A52',
hachureGap: 16,
fillWeight: 6,
strokeWidth: 6
});
console.log(r)
const svgString = `<svg fill="none" xmlns="http://www.w3.org/2000/svg">${r.innerHTML}</svg>`
createPathFromSvg({ svgString, canvas, sub_type: key });
}

return (
<div className="fabritor-panel-wrapper">
<Title>线条</Title>
Expand Down Expand Up @@ -78,6 +95,20 @@ export default function ShapePanel () {
))
}
</Flex>
<Title>手绘风格形状</Title>
<Flex gap={10} wrap="wrap" justify="space-around">
{
RoughTypeList.map(item => (
<div
key={item.key}
onClick={() => { addRough(item) }}
className="fabritor-panel-shape-item"
>
{item.elem}
</div>
))
}
</Flex>
</div>
)
}
9 changes: 9 additions & 0 deletions src/fabritor/UI/panel/ShapePanel/rough-type-list.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default [
{
key: 'rough-rect',
elem: 'rect',
options: {

}
}
]
14 changes: 13 additions & 1 deletion src/fabritor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { GloablStateContext } from '@/context';
import ContextMenu from './components/ContextMenu';
import { SKETCH_ID } from '@/utils/constants';
import ObjectRotateAngleTip from './components/ObjectRotateAngleTip';
import rough from 'roughjs';

import '../font.css';

Expand All @@ -31,7 +32,9 @@ const contentStyle: React.CSSProperties = {
export default function Fabritor () {
const canvasEl = useRef<HTMLCanvasElement>(null);
const workspaceEl = useRef<HTMLDivElement>(null);
const roughSvgEl = useRef(null);
const [editor, setEditor] = useState<Editor | null>(null);
const [roughSvg, setRoughSvg] = useState<any>();
const [activeObject, setActiveObject] = useState<fabric.Object | null | undefined>(null);
const [isReady, setReady] = useState(false);
const contextMenuRef = useRef<any>(null);
Expand Down Expand Up @@ -110,9 +113,15 @@ export default function Fabritor () {
setActiveObject(_editor.sketch);
}

const initRoughSvg = () => {
// @ts-ignore rough svg
setRoughSvg(rough.svg(roughSvgEl.current));
}

useEffect(() => {
if (editor) {
initEvent();
initRoughSvg();
}
}, [editor]);

Expand All @@ -133,7 +142,8 @@ export default function Fabritor () {
setActiveObject,
isReady,
setReady,
editor
editor,
roughSvg
}}
>
<Layout style={{ height: '100%' }} className="fabritor-layout">
Expand All @@ -151,6 +161,8 @@ export default function Fabritor () {
</Content>
<Setter />
</Layout>

<svg id="fabritor-rough-svg" ref={roughSvgEl} />
</Layout>
</GloablStateContext.Provider>
)
Expand Down
6 changes: 6 additions & 0 deletions src/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ html, body {
&:hover {
background-color: rgba(64,87,109,.07);
}
}

#fabritor-rough-svg {
position: fixed;
left: 99999999;
top: 99999999;
}
Loading

0 comments on commit d291ef4

Please sign in to comment.