Skip to content

Commit

Permalink
feat: 新增controls控制
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Jul 21, 2023
1 parent 814b00c commit 6aa1187
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
29 changes: 17 additions & 12 deletions packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import { BaseService } from '@pictode/utils';
import { fabric } from 'fabric';

import { Rect } from './customs/rect';
import { AppOption, ControlsOption, EventArgs, Plugin } from './types';

type Model = 'select' | 'drawing' | 'rect' | 'circle';
import { AppOption, ControlsOption, EventArgs, Model, Plugin } from './types';
import { DEFAULT_APP_OPTION } from './utils';

export class App extends BaseService<EventArgs> {
public canvas: fabric.Canvas;

private option?: AppOption;
private option: AppOption & { controls: ControlsOption };
private installedPlugins: Map<string, Plugin> = new Map();
private canvasEl: HTMLCanvasElement;

constructor(option?: AppOption) {
super();
this.option = option;
this.option = Object.assign({}, DEFAULT_APP_OPTION, option ?? DEFAULT_APP_OPTION);
this.canvasEl = document.createElement('canvas');
this.canvas = new fabric.Canvas(null, {
backgroundColor: option?.backgroundColor,
});
this.setControls(this.option.controls);
}

public mount(element: HTMLElement) {
Expand All @@ -31,10 +31,19 @@ export class App extends BaseService<EventArgs> {
this.setModel('select');
}

public setControls(controls?: ControlsOption | boolean): App {
if (!controls) {
Reflect.set(fabric.Object.prototype, 'hasControls', false);
public setControls(controls: ControlsOption | boolean): App {
this.option.controls = controls;
if (typeof controls === 'boolean') {
this.option.controls.hasControls = controls;
}

Object.entries(this.option.controls).forEach(([key, value]) => {
if (key === 'controlVisible') {
fabric.Object.prototype.setControlsVisibility(value);
} else {
Reflect.set(fabric.Object.prototype, key, value);
}
});
this.render(true);
return this;
}
Expand All @@ -48,10 +57,6 @@ export class App extends BaseService<EventArgs> {
fill: 'transparent',
stroke: 'blue',
strokeWidth: 5,
borderColor: 'rgb(93, 94, 214)',
cornerColor: 'rgb(93, 94, 214)',
cornerSize: 8,
cornerStyle: 'circle',
rx: 5,
ry: 5,
});
Expand Down
7 changes: 0 additions & 7 deletions packages/core/src/customs/object.ts

This file was deleted.

16 changes: 8 additions & 8 deletions packages/core/src/customs/rect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { fabric } from 'fabric';

const isTransparent = (color: string): boolean => {
const alpha = new fabric.Color(color).getAlpha();
return alpha === 0;
return new fabric.Color(color).getAlpha() === 0;
};

export class Rect extends fabric.Rect {
Expand All @@ -17,16 +16,17 @@ export class Rect extends fabric.Rect {
if (isFillTransparent && !activeObjects?.includes(this)) {
const tolerance = 3;
const boundingRect = this.getBoundingRect();
const strokeWidth = this.strokeWidth || 1; // 边框宽度,默认为 1
const distanceToTop = Math.abs(point.y - boundingRect.top);
const distanceToBottom = Math.abs(point.y - boundingRect.top - boundingRect.height);
const distanceToLeft = Math.abs(point.x - boundingRect.left);
const distanceToRight = Math.abs(point.x - boundingRect.left - boundingRect.width);
return (
distanceToTop <= tolerance ||
distanceToBottom <= tolerance ||
distanceToLeft <= tolerance ||
distanceToRight <= tolerance
);
const onBorder =
distanceToTop <= strokeWidth + tolerance ||
distanceToBottom <= strokeWidth + tolerance ||
distanceToLeft <= strokeWidth + tolerance ||
distanceToRight <= strokeWidth + tolerance;
return onBorder;
}
return super.containsPoint(point, lines, absolute, calculate);
}
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface Plugin {

export interface EventArgs {}

export type Model = 'select' | 'drawing' | 'rect' | 'circle';

export interface ControlVisible {
bl?: boolean;
br?: boolean;
Expand All @@ -24,11 +26,11 @@ export interface ControlVisible {
}

export interface ControlsOption {
enable?: boolean;
hasControls?: boolean;
padding?: number;
borderColor?: string;
cornerColor?: string;
connerStrokeColor?: string;
cornerStrokeColor?: string;
cornerStyle?: 'rect' | 'circle';
cornerSize?: number;
rotatingPointOffset?: number;
Expand All @@ -40,5 +42,5 @@ export interface ControlsOption {

export interface AppOption {
backgroundColor?: string;
controls?: ControlsOption;
controls?: ControlsOption | boolean;
}
16 changes: 16 additions & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AppOption, ControlsOption } from '../types';

export const DEFAULT_APP_OPTION: AppOption & { controls: ControlsOption } = {
backgroundColor: '#ffffff',
controls: {
hasControls: true,
padding: 3,
borderColor: 'rgb(157, 157, 231)',
cornerColor: 'rgb(157, 157, 231)',
cornerStrokeColor: 'rgb(157, 157, 231)',
cornerStyle: 'circle',
cornerSize: 6,
rotatingPointOffset: 10,
transparentCorners: true,
},
};

0 comments on commit 6aa1187

Please sign in to comment.