Skip to content

Commit

Permalink
feat: 新增controls设置和render方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Jul 21, 2023
1 parent c1975d8 commit 814b00c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
26 changes: 21 additions & 5 deletions packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { BaseService } from '@pictode/utils';
import { fabric } from 'fabric';

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

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

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

private options?: AppOptions;
private option?: AppOption;
private installedPlugins: Map<string, Plugin> = new Map();
private canvasEl: HTMLCanvasElement;

constructor(options?: AppOptions) {
constructor(option?: AppOption) {
super();
this.options = options;
this.option = option;
this.canvasEl = document.createElement('canvas');
this.canvas = new fabric.Canvas(null, {
backgroundColor: options?.backgroundColor,
backgroundColor: option?.backgroundColor,
});
}

Expand All @@ -31,6 +31,14 @@ export class App extends BaseService<EventArgs> {
this.setModel('select');
}

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

public setModel(model: Model): App {
const rect = new Rect({
width: 200,
Expand Down Expand Up @@ -68,6 +76,14 @@ export class App extends BaseService<EventArgs> {
return this;
}

public render(asyncRedraw?: boolean): void {
if (asyncRedraw) {
this.canvas.requestRenderAll();
} else {
this.canvas.renderAll();
}
}

public use(plugin: Plugin, ...options: any[]): App {
if (!this.installedPlugins.has(plugin.name)) {
this.installedPlugins.set(plugin.name, plugin);
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface ControlVisible {
mtr?: boolean;
}

export interface Controls {
export interface ControlsOption {
enable?: boolean;
padding?: number;
borderColor?: string;
Expand All @@ -38,7 +38,7 @@ export interface Controls {
controlVisible?: ControlVisible;
}

export interface AppOptions {
export interface AppOption {
backgroundColor?: string;
controls?: Controls;
controls?: ControlsOption;
}

0 comments on commit 814b00c

Please sign in to comment.