From ce61857caa1014f963501f1beb823a393f267455 Mon Sep 17 00:00:00 2001 From: chenshenhai Date: Mon, 31 May 2021 00:50:10 +0800 Subject: [PATCH] refactor: rewrite core's renderer --- packages/core/src/index.ts | 2 +- packages/core/src/lib/painter.ts | 10 ----- packages/core/src/lib/renderer.ts | 70 +++++++++++++++++++++++++------ packages/util/src/index.ts | 4 ++ packages/util/src/lib/data.ts | 30 ++++++++++--- 5 files changed, 88 insertions(+), 28 deletions(-) delete mode 100644 packages/core/src/lib/painter.ts diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 69422cf..55560a5 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,6 +1,6 @@ import { TypeData, TypePoint, TypeHelperWrapperDotDirection, TypeConfig, TypeConfigStrict } from '@idraw/types'; import Board from '@idraw/board'; -import Renderer from './lib/renderer'; +import { Renderer } from './lib/renderer'; import { Element } from './lib/element'; import { Helper } from './lib/helper'; import { mergeConfig } from './lib/config'; diff --git a/packages/core/src/lib/painter.ts b/packages/core/src/lib/painter.ts deleted file mode 100644 index 7e831c0..0000000 --- a/packages/core/src/lib/painter.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { TypeData } from '@idraw/types'; - -export class Painter { - - // private _dataList: TypeData[] = []; - - draw(data: TypeData): void { - - } -} \ No newline at end of file diff --git a/packages/core/src/lib/renderer.ts b/packages/core/src/lib/renderer.ts index aef6748..cd412dd 100644 --- a/packages/core/src/lib/renderer.ts +++ b/packages/core/src/lib/renderer.ts @@ -1,21 +1,67 @@ -import { TypeContext, TypeData, TypeHelperConfig } from '@idraw/types'; -import { drawContext } from './draw'; -import Board from '@idraw/board'; - -export default class Renderer { +// import { TypeData, TypeHelperConfig, } from '@idraw/types'; +// import util from '@idraw/util'; +// import Board from '@idraw/board'; +// import { drawContext } from './draw'; - private _board: Board; - private _ctx: TypeContext; - private _data: TypeData = { elements: [] }; +// const { requestAnimationFrame } = window; +// const { deepClone } = util.data; + +// type QueueItem = { data: TypeData, helper: TypeHelperConfig }; +// enum DrawStatus { +// FREE = 'free', +// DRAWING = 'drawing', +// } + +// export class Renderer { + +// private _queue: QueueItem[] = []; +// private _board: Board; +// private _status: DrawStatus = DrawStatus.FREE; + +// constructor(board: Board) { +// this._board = board; +// } + +// render(data: TypeData, helper: TypeHelperConfig): void { +// const _data: QueueItem = deepClone({ data, helper }) as QueueItem; +// this._queue.push(_data); +// if (this._status === DrawStatus.FREE) { +// this._status = DrawStatus.DRAWING; +// this._drawFrame(); +// } +// } + +// private _drawFrame() { +// requestAnimationFrame(() => { +// const item: QueueItem | undefined = this._queue.shift(); +// if (item) { +// drawContext(this._board.getContext(), item.data, item.helper); +// this._board.draw(); +// this._drawFrame(); +// } else { +// this._status = DrawStatus.FREE +// } +// }) +// } +// } + + + + +import { TypeData, TypeHelperConfig, } from '@idraw/types'; +import Board from '@idraw/board'; +import { drawContext } from './draw'; + +export class Renderer { + + private _board: Board; constructor(board: Board) { this._board = board; - this._ctx = this._board.getContext(); } - render(data: TypeData, config: TypeHelperConfig): void { - this._data = data; - drawContext(this._ctx, this._data, config); + render(data: TypeData, helper: TypeHelperConfig): void { + drawContext(this._board.getContext(), data, helper); this._board.draw(); } } \ No newline at end of file diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index 45a037e..8388193 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -3,6 +3,7 @@ import { delay, compose, throttle } from './lib/time'; import { downloadImageFromCanvas } from './lib/file'; import { toColorHexStr, toColorHexNum, isColorStr } from './lib/color'; import { createUUID } from './lib/uuid'; +import { deepClone } from './lib/data'; import istype from './lib/istype'; export default { @@ -26,4 +27,7 @@ export default { createUUID }, istype, + data: { + deepClone, + } }; \ No newline at end of file diff --git a/packages/util/src/lib/data.ts b/packages/util/src/lib/data.ts index 78c8168..c51a9bb 100644 --- a/packages/util/src/lib/data.ts +++ b/packages/util/src/lib/data.ts @@ -1,7 +1,27 @@ -// import { } from './' -export function clone (data: any): any { - if (Object.prototype.toString.call(data)) - // TODO - return JSON.parse(JSON.stringify(data)); +export function deepClone(target: any): any { + function _clone(t: any) { + const type = is(t); + if (['Null', 'Number', 'String', 'Undefined'].indexOf(type) >= 0) { + return t; + } else if (type === 'Array') { + const arr: any[] = []; + t.forEach((item: any) => { + arr.push(_clone(item)); + }); + return arr; + } else if (type === 'Object') { + const obj: {[key: string]: any} = {}; + const keys = Object.keys(t); + keys.forEach((key) => { + obj[key] = _clone(t[key]); + }); + return obj; + } + } + return _clone(target); +} + +function is(data: any): string { + return Object.prototype.toString.call(data).replace(/[\]|\[]{1,1}/ig, '').split(' ')[1]; } \ No newline at end of file