@idraw/renderer 0.3.0-alpha.6 → 0.3.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/constant/element.d.ts +2 -0
- package/dist/esm/constant/element.js +10 -0
- package/dist/esm/constant/static.d.ts +11 -0
- package/dist/esm/constant/static.js +13 -0
- package/dist/esm/index.d.ts +27 -0
- package/{esm → dist/esm}/index.js +7 -4
- package/dist/esm/lib/calculate.d.ts +5 -0
- package/dist/esm/lib/calculate.js +64 -0
- package/dist/esm/lib/diff.d.ts +6 -0
- package/dist/esm/lib/diff.js +80 -0
- package/dist/esm/lib/draw/base.d.ts +5 -0
- package/dist/esm/lib/draw/base.js +89 -0
- package/dist/esm/lib/draw/circle.d.ts +2 -0
- package/dist/esm/lib/draw/circle.js +28 -0
- package/dist/esm/lib/draw/html.d.ts +3 -0
- package/dist/esm/lib/draw/html.js +9 -0
- package/dist/esm/lib/draw/image.d.ts +3 -0
- package/dist/esm/lib/draw/image.js +9 -0
- package/dist/esm/lib/draw/index.d.ts +3 -0
- package/dist/esm/lib/draw/index.js +55 -0
- package/dist/esm/lib/draw/rect.d.ts +2 -0
- package/dist/esm/lib/draw/rect.js +4 -0
- package/dist/esm/lib/draw/svg.d.ts +3 -0
- package/dist/esm/lib/draw/svg.js +9 -0
- package/dist/esm/lib/draw/text.d.ts +3 -0
- package/dist/esm/lib/draw/text.js +116 -0
- package/dist/esm/lib/index.d.ts +6 -0
- package/dist/esm/lib/index.js +6 -0
- package/dist/esm/lib/loader-event.d.ts +33 -0
- package/dist/esm/lib/loader-event.js +50 -0
- package/dist/esm/lib/loader.d.ts +25 -0
- package/dist/esm/lib/loader.js +238 -0
- package/dist/esm/lib/parse.d.ts +2 -0
- package/dist/esm/lib/parse.js +32 -0
- package/dist/esm/lib/renderer-event.d.ts +32 -0
- package/dist/esm/lib/renderer-event.js +50 -0
- package/dist/esm/lib/temp.d.ts +22 -0
- package/dist/esm/lib/temp.js +29 -0
- package/dist/esm/lib/transform.d.ts +4 -0
- package/dist/esm/lib/transform.js +20 -0
- package/dist/esm/lib/value.d.ts +2 -0
- package/dist/esm/lib/value.js +7 -0
- package/dist/esm/util/filter.d.ts +1 -0
- package/dist/esm/util/filter.js +3 -0
- package/dist/index.global.js +1205 -1539
- package/dist/index.global.min.js +1 -1
- package/package.json +9 -14
- package/dist/index.cjs.js +0 -1588
- package/dist/index.d.ts +0 -81
- package/dist/index.esm.js +0 -1586
- package/esm/default.d.ts +0 -1
- package/esm/default.js +0 -2
- package/esm/esm.d.ts +0 -2
- package/esm/esm.js +0 -3
- /package/{esm → dist/esm}/names.d.ts +0 -0
- /package/{esm → dist/esm}/names.js +0 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare enum Mode {
|
|
2
|
+
NULL = "null",
|
|
3
|
+
SELECT_ELEMENT = "select-element",
|
|
4
|
+
SELECT_ELEMENT_LIST = "select-element-list",
|
|
5
|
+
SELECT_ELEMENT_WRAPPER_CONTROLLER = "select-element-wrapper-controller",
|
|
6
|
+
SELECT_AREA = "select-area"
|
|
7
|
+
}
|
|
8
|
+
export declare enum CursorStatus {
|
|
9
|
+
DRAGGING = "dragging",
|
|
10
|
+
NULL = "null"
|
|
11
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export var Mode;
|
|
2
|
+
(function (Mode) {
|
|
3
|
+
Mode["NULL"] = "null";
|
|
4
|
+
Mode["SELECT_ELEMENT"] = "select-element";
|
|
5
|
+
Mode["SELECT_ELEMENT_LIST"] = "select-element-list";
|
|
6
|
+
Mode["SELECT_ELEMENT_WRAPPER_CONTROLLER"] = "select-element-wrapper-controller";
|
|
7
|
+
Mode["SELECT_AREA"] = "select-area";
|
|
8
|
+
})(Mode || (Mode = {}));
|
|
9
|
+
export var CursorStatus;
|
|
10
|
+
(function (CursorStatus) {
|
|
11
|
+
CursorStatus["DRAGGING"] = "dragging";
|
|
12
|
+
CursorStatus["NULL"] = "null";
|
|
13
|
+
})(CursorStatus || (CursorStatus = {}));
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { TypeData, TypeContext } from '@idraw/types';
|
|
2
|
+
import { RendererEvent } from './lib/renderer-event';
|
|
3
|
+
import { _queue, _ctx, _status, _loader, _opts, _freeze, _drawFrame, _retainQueueOneItem } from './names';
|
|
4
|
+
type Options = {
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
contextWidth?: number;
|
|
8
|
+
contextHeight?: number;
|
|
9
|
+
devicePixelRatio: number;
|
|
10
|
+
};
|
|
11
|
+
export default class Renderer extends RendererEvent {
|
|
12
|
+
private [_queue];
|
|
13
|
+
private [_ctx];
|
|
14
|
+
private [_status];
|
|
15
|
+
private [_loader];
|
|
16
|
+
private [_opts]?;
|
|
17
|
+
constructor(opts?: Options);
|
|
18
|
+
render(target: HTMLCanvasElement | TypeContext, originData: TypeData, opts?: {
|
|
19
|
+
changeResourceUUIDs?: string[];
|
|
20
|
+
}): void;
|
|
21
|
+
getContext(): TypeContext | null;
|
|
22
|
+
thaw(): void;
|
|
23
|
+
private [_freeze];
|
|
24
|
+
private [_drawFrame];
|
|
25
|
+
private [_retainQueueOneItem];
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
@@ -12,7 +12,7 @@ var DrawStatus;
|
|
|
12
12
|
DrawStatus["DRAWING"] = "drawing";
|
|
13
13
|
DrawStatus["FREEZE"] = "freeze";
|
|
14
14
|
})(DrawStatus || (DrawStatus = {}));
|
|
15
|
-
export class Renderer extends RendererEvent {
|
|
15
|
+
export default class Renderer extends RendererEvent {
|
|
16
16
|
constructor(opts) {
|
|
17
17
|
super();
|
|
18
18
|
this[_a] = [];
|
|
@@ -45,7 +45,8 @@ export class Renderer extends RendererEvent {
|
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
if (!this[_ctx]) {
|
|
48
|
-
if (this[_opts] &&
|
|
48
|
+
if (this[_opts] &&
|
|
49
|
+
Object.prototype.toString.call(target) === '[object HTMLCanvasElement]') {
|
|
49
50
|
const { width, height, contextWidth, contextHeight, devicePixelRatio } = this[_opts];
|
|
50
51
|
const canvas = target;
|
|
51
52
|
canvas.width = width * devicePixelRatio;
|
|
@@ -66,7 +67,7 @@ export class Renderer extends RendererEvent {
|
|
|
66
67
|
if ([DrawStatus.FREEZE].includes(this[_status])) {
|
|
67
68
|
return;
|
|
68
69
|
}
|
|
69
|
-
const _data = deepClone({ data
|
|
70
|
+
const _data = deepClone({ data });
|
|
70
71
|
this[_queue].push(_data);
|
|
71
72
|
this[_drawFrame]();
|
|
72
73
|
this[_loader].load(data, changeResourceUUIDs || []);
|
|
@@ -117,7 +118,9 @@ export class Renderer extends RendererEvent {
|
|
|
117
118
|
this[_status] = DrawStatus.FREE;
|
|
118
119
|
}
|
|
119
120
|
this.trigger('drawFrame', { t: Date.now() });
|
|
120
|
-
if (this[_loader].isComplete() === true &&
|
|
121
|
+
if (this[_loader].isComplete() === true &&
|
|
122
|
+
this[_queue].length === 1 &&
|
|
123
|
+
this[_status] === DrawStatus.FREE) {
|
|
121
124
|
if (ctx && this[_queue][0] && this[_queue][0].data) {
|
|
122
125
|
drawContext(ctx, this[_queue][0].data, this[_loader]);
|
|
123
126
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { TypeElement, TypeElemDesc, TypePoint } from '@idraw/types';
|
|
2
|
+
export declare function parseRadianToAngle(radian: number): number;
|
|
3
|
+
export declare function parseAngleToRadian(angle: number): number;
|
|
4
|
+
export declare function calcElementCenter(elem: TypeElement<keyof TypeElemDesc>): TypePoint;
|
|
5
|
+
export declare function calcRadian(center: TypePoint, start: TypePoint, end: TypePoint): number;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export function parseRadianToAngle(radian) {
|
|
2
|
+
return radian / Math.PI * 180;
|
|
3
|
+
}
|
|
4
|
+
export function parseAngleToRadian(angle) {
|
|
5
|
+
return angle / 180 * Math.PI;
|
|
6
|
+
}
|
|
7
|
+
export function calcElementCenter(elem) {
|
|
8
|
+
const p = {
|
|
9
|
+
x: elem.x + elem.w / 2,
|
|
10
|
+
y: elem.y + elem.h / 2,
|
|
11
|
+
};
|
|
12
|
+
return p;
|
|
13
|
+
}
|
|
14
|
+
export function calcRadian(center, start, end) {
|
|
15
|
+
const startAngle = calcLineAngle(center, start);
|
|
16
|
+
const endAngle = calcLineAngle(center, end);
|
|
17
|
+
if (endAngle !== null && startAngle !== null) {
|
|
18
|
+
if (startAngle > Math.PI * 3 / 2 && endAngle < Math.PI / 2) {
|
|
19
|
+
return endAngle + (Math.PI * 2 - startAngle);
|
|
20
|
+
}
|
|
21
|
+
else if (endAngle > Math.PI * 3 / 2 && startAngle < Math.PI / 2) {
|
|
22
|
+
return startAngle + (Math.PI * 2 - endAngle);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
return endAngle - startAngle;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function calcLineAngle(center, p) {
|
|
33
|
+
const x = p.x - center.x;
|
|
34
|
+
const y = center.y - p.y;
|
|
35
|
+
if (x === 0) {
|
|
36
|
+
if (y < 0) {
|
|
37
|
+
return Math.PI / 2;
|
|
38
|
+
}
|
|
39
|
+
else if (y > 0) {
|
|
40
|
+
return Math.PI * (3 / 2);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else if (y === 0) {
|
|
44
|
+
if (x < 0) {
|
|
45
|
+
return Math.PI;
|
|
46
|
+
}
|
|
47
|
+
else if (x > 0) {
|
|
48
|
+
return 0;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (x > 0 && y < 0) {
|
|
52
|
+
return Math.atan(Math.abs(y) / Math.abs(x));
|
|
53
|
+
}
|
|
54
|
+
else if (x < 0 && y < 0) {
|
|
55
|
+
return Math.PI - Math.atan(Math.abs(y) / Math.abs(x));
|
|
56
|
+
}
|
|
57
|
+
else if (x < 0 && y > 0) {
|
|
58
|
+
return Math.PI + Math.atan(Math.abs(y) / Math.abs(x));
|
|
59
|
+
}
|
|
60
|
+
else if (x > 0 && y > 0) {
|
|
61
|
+
return Math.PI * 2 - Math.atan(Math.abs(y) / Math.abs(x));
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TypeElement, TypeData, TypeElemDesc } from '@idraw/types';
|
|
2
|
+
export declare function isChangeImageElementResource(before: TypeElement<'image'>, after: TypeElement<'image'>): boolean;
|
|
3
|
+
export declare function isChangeSVGElementResource(before: TypeElement<'svg'>, after: TypeElement<'svg'>): boolean;
|
|
4
|
+
export declare function isChangeHTMLElementResource(before: TypeElement<'html'>, after: TypeElement<'html'>): boolean;
|
|
5
|
+
export declare function diffElementResourceChange(before: TypeElement<keyof TypeElemDesc>, after: TypeElement<keyof TypeElemDesc>): string | null;
|
|
6
|
+
export declare function diffElementResourceChangeList(before: TypeData, after: TypeData): string[];
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export function isChangeImageElementResource(before, after) {
|
|
2
|
+
var _a, _b;
|
|
3
|
+
return (((_a = before === null || before === void 0 ? void 0 : before.desc) === null || _a === void 0 ? void 0 : _a.src) !== ((_b = after === null || after === void 0 ? void 0 : after.desc) === null || _b === void 0 ? void 0 : _b.src));
|
|
4
|
+
}
|
|
5
|
+
export function isChangeSVGElementResource(before, after) {
|
|
6
|
+
var _a, _b;
|
|
7
|
+
return (((_a = before === null || before === void 0 ? void 0 : before.desc) === null || _a === void 0 ? void 0 : _a.svg) !== ((_b = after === null || after === void 0 ? void 0 : after.desc) === null || _b === void 0 ? void 0 : _b.svg));
|
|
8
|
+
}
|
|
9
|
+
export function isChangeHTMLElementResource(before, after) {
|
|
10
|
+
var _a, _b, _c, _d, _e, _f;
|
|
11
|
+
return (((_a = before === null || before === void 0 ? void 0 : before.desc) === null || _a === void 0 ? void 0 : _a.html) !== ((_b = after === null || after === void 0 ? void 0 : after.desc) === null || _b === void 0 ? void 0 : _b.html)
|
|
12
|
+
|| ((_c = before === null || before === void 0 ? void 0 : before.desc) === null || _c === void 0 ? void 0 : _c.width) !== ((_d = after === null || after === void 0 ? void 0 : after.desc) === null || _d === void 0 ? void 0 : _d.width)
|
|
13
|
+
|| ((_e = before === null || before === void 0 ? void 0 : before.desc) === null || _e === void 0 ? void 0 : _e.height) !== ((_f = after === null || after === void 0 ? void 0 : after.desc) === null || _f === void 0 ? void 0 : _f.height));
|
|
14
|
+
}
|
|
15
|
+
export function diffElementResourceChange(before, after) {
|
|
16
|
+
let result = null;
|
|
17
|
+
let isChange = false;
|
|
18
|
+
switch (after.type) {
|
|
19
|
+
case 'image': {
|
|
20
|
+
isChange = isChangeImageElementResource(before, after);
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
case 'svg': {
|
|
24
|
+
isChange = isChangeSVGElementResource(before, after);
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
case 'html': {
|
|
28
|
+
isChange = isChangeHTMLElementResource(before, after);
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
default: break;
|
|
32
|
+
}
|
|
33
|
+
if (isChange === true) {
|
|
34
|
+
result = after.uuid;
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
export function diffElementResourceChangeList(before, after) {
|
|
39
|
+
var _a;
|
|
40
|
+
const uuids = [];
|
|
41
|
+
const beforeMap = parseDataElementMap(before);
|
|
42
|
+
const afterMap = parseDataElementMap(after);
|
|
43
|
+
for (const uuid in afterMap) {
|
|
44
|
+
if (['image', 'svg', 'html'].includes((_a = afterMap[uuid]) === null || _a === void 0 ? void 0 : _a.type) !== true) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (beforeMap[uuid]) {
|
|
48
|
+
let isChange = false;
|
|
49
|
+
switch (beforeMap[uuid].type) {
|
|
50
|
+
case 'image': {
|
|
51
|
+
isChange = isChangeImageElementResource(beforeMap[uuid], afterMap[uuid]);
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
case 'svg': {
|
|
55
|
+
isChange = isChangeSVGElementResource(beforeMap[uuid], afterMap[uuid]);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case 'html': {
|
|
59
|
+
isChange = isChangeHTMLElementResource(beforeMap[uuid], afterMap[uuid]);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
default: break;
|
|
63
|
+
}
|
|
64
|
+
if (isChange === true) {
|
|
65
|
+
uuids.push(uuid);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
uuids.push(uuid);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return uuids;
|
|
73
|
+
}
|
|
74
|
+
function parseDataElementMap(data) {
|
|
75
|
+
const elemMap = {};
|
|
76
|
+
data.elements.forEach((elem) => {
|
|
77
|
+
elemMap[elem.uuid] = elem;
|
|
78
|
+
});
|
|
79
|
+
return elemMap;
|
|
80
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { TypeContext, TypeElement } from '@idraw/types';
|
|
2
|
+
export declare function clearContext(ctx: TypeContext): void;
|
|
3
|
+
export declare function drawBgColor(ctx: TypeContext, color: string): void;
|
|
4
|
+
export declare function drawBox(ctx: TypeContext, elem: TypeElement<'text' | 'rect'>, pattern: string | CanvasPattern | null): void;
|
|
5
|
+
export declare function drawBoxBorder(ctx: TypeContext, elem: TypeElement<'text' | 'rect'>): void;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { is, istype, isColorStr } from '@idraw/util';
|
|
2
|
+
import { rotateElement } from './../transform';
|
|
3
|
+
export function clearContext(ctx) {
|
|
4
|
+
ctx.setFillStyle('#000000');
|
|
5
|
+
ctx.setStrokeStyle('#000000');
|
|
6
|
+
ctx.setLineDash([]);
|
|
7
|
+
ctx.setGlobalAlpha(1);
|
|
8
|
+
ctx.setShadowColor('#00000000');
|
|
9
|
+
ctx.setShadowOffsetX(0);
|
|
10
|
+
ctx.setShadowOffsetY(0);
|
|
11
|
+
ctx.setShadowBlur(0);
|
|
12
|
+
}
|
|
13
|
+
export function drawBgColor(ctx, color) {
|
|
14
|
+
const size = ctx.getSize();
|
|
15
|
+
ctx.setFillStyle(color);
|
|
16
|
+
ctx.fillRect(0, 0, size.contextWidth, size.contextHeight);
|
|
17
|
+
}
|
|
18
|
+
export function drawBox(ctx, elem, pattern) {
|
|
19
|
+
clearContext(ctx);
|
|
20
|
+
drawBoxBorder(ctx, elem);
|
|
21
|
+
clearContext(ctx);
|
|
22
|
+
rotateElement(ctx, elem, () => {
|
|
23
|
+
const { x, y, w, h } = elem;
|
|
24
|
+
let r = elem.desc.borderRadius || 0;
|
|
25
|
+
r = Math.min(r, w / 2, h / 2);
|
|
26
|
+
if (w < r * 2 || h < r * 2) {
|
|
27
|
+
r = 0;
|
|
28
|
+
}
|
|
29
|
+
ctx.beginPath();
|
|
30
|
+
ctx.moveTo(x + r, y);
|
|
31
|
+
ctx.arcTo(x + w, y, x + w, y + h, r);
|
|
32
|
+
ctx.arcTo(x + w, y + h, x, y + h, r);
|
|
33
|
+
ctx.arcTo(x, y + h, x, y, r);
|
|
34
|
+
ctx.arcTo(x, y, x + w, y, r);
|
|
35
|
+
ctx.closePath();
|
|
36
|
+
if (typeof pattern === 'string') {
|
|
37
|
+
ctx.setFillStyle(pattern);
|
|
38
|
+
}
|
|
39
|
+
else if (['CanvasPattern'].includes(istype.type(pattern))) {
|
|
40
|
+
ctx.setFillStyle(pattern);
|
|
41
|
+
}
|
|
42
|
+
ctx.fill();
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
export function drawBoxBorder(ctx, elem) {
|
|
46
|
+
clearContext(ctx);
|
|
47
|
+
rotateElement(ctx, elem, () => {
|
|
48
|
+
if (!(elem.desc.borderWidth && elem.desc.borderWidth > 0)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const bw = elem.desc.borderWidth;
|
|
52
|
+
let borderColor = '#000000';
|
|
53
|
+
if (isColorStr(elem.desc.borderColor) === true) {
|
|
54
|
+
borderColor = elem.desc.borderColor;
|
|
55
|
+
}
|
|
56
|
+
const x = elem.x - bw / 2;
|
|
57
|
+
const y = elem.y - bw / 2;
|
|
58
|
+
const w = elem.w + bw;
|
|
59
|
+
const h = elem.h + bw;
|
|
60
|
+
let r = elem.desc.borderRadius || 0;
|
|
61
|
+
r = Math.min(r, w / 2, h / 2);
|
|
62
|
+
if (r < w / 2 && r < h / 2) {
|
|
63
|
+
r = r + bw / 2;
|
|
64
|
+
}
|
|
65
|
+
const { desc } = elem;
|
|
66
|
+
if (desc.shadowColor !== undefined && isColorStr(desc.shadowColor)) {
|
|
67
|
+
ctx.setShadowColor(desc.shadowColor);
|
|
68
|
+
}
|
|
69
|
+
if (desc.shadowOffsetX !== undefined && is.number(desc.shadowOffsetX)) {
|
|
70
|
+
ctx.setShadowOffsetX(desc.shadowOffsetX);
|
|
71
|
+
}
|
|
72
|
+
if (desc.shadowOffsetY !== undefined && is.number(desc.shadowOffsetY)) {
|
|
73
|
+
ctx.setShadowOffsetY(desc.shadowOffsetY);
|
|
74
|
+
}
|
|
75
|
+
if (desc.shadowBlur !== undefined && is.number(desc.shadowBlur)) {
|
|
76
|
+
ctx.setShadowBlur(desc.shadowBlur);
|
|
77
|
+
}
|
|
78
|
+
ctx.beginPath();
|
|
79
|
+
ctx.setLineWidth(bw);
|
|
80
|
+
ctx.setStrokeStyle(borderColor);
|
|
81
|
+
ctx.moveTo(x + r, y);
|
|
82
|
+
ctx.arcTo(x + w, y, x + w, y + h, r);
|
|
83
|
+
ctx.arcTo(x + w, y + h, x, y + h, r);
|
|
84
|
+
ctx.arcTo(x, y + h, x, y, r);
|
|
85
|
+
ctx.arcTo(x, y, x + w, y, r);
|
|
86
|
+
ctx.closePath();
|
|
87
|
+
ctx.stroke();
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { rotateElement } from './../transform';
|
|
2
|
+
import { clearContext } from './base';
|
|
3
|
+
export function drawCircle(ctx, elem) {
|
|
4
|
+
clearContext(ctx);
|
|
5
|
+
rotateElement(ctx, elem, (ctx) => {
|
|
6
|
+
const { x, y, w, h, desc } = elem;
|
|
7
|
+
const { bgColor = '#000000', borderColor = '#000000', borderWidth = 0, } = desc;
|
|
8
|
+
const a = w / 2;
|
|
9
|
+
const b = h / 2;
|
|
10
|
+
const centerX = x + a;
|
|
11
|
+
const centerY = y + b;
|
|
12
|
+
if (borderWidth && borderWidth > 0) {
|
|
13
|
+
const ba = borderWidth / 2 + a;
|
|
14
|
+
const bb = borderWidth / 2 + b;
|
|
15
|
+
ctx.beginPath();
|
|
16
|
+
ctx.setStrokeStyle(borderColor);
|
|
17
|
+
ctx.setLineWidth(borderWidth);
|
|
18
|
+
ctx.ellipse(centerX, centerY, ba, bb, 0, 0, 2 * Math.PI);
|
|
19
|
+
ctx.closePath();
|
|
20
|
+
ctx.stroke();
|
|
21
|
+
}
|
|
22
|
+
ctx.beginPath();
|
|
23
|
+
ctx.setFillStyle(bgColor);
|
|
24
|
+
ctx.ellipse(centerX, centerY, a, b, 0, 0, 2 * Math.PI);
|
|
25
|
+
ctx.closePath();
|
|
26
|
+
ctx.fill();
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { rotateElement } from '../transform';
|
|
2
|
+
export function drawHTML(ctx, elem, loader) {
|
|
3
|
+
const content = loader.getContent(elem.uuid);
|
|
4
|
+
rotateElement(ctx, elem, () => {
|
|
5
|
+
if (content) {
|
|
6
|
+
ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { rotateElement } from '../transform';
|
|
2
|
+
export function drawImage(ctx, elem, loader) {
|
|
3
|
+
const content = loader.getContent(elem.uuid);
|
|
4
|
+
rotateElement(ctx, elem, () => {
|
|
5
|
+
if (content) {
|
|
6
|
+
ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { isColorStr } from '@idraw/util';
|
|
2
|
+
import { clearContext, drawBgColor } from './base';
|
|
3
|
+
import { drawRect } from './rect';
|
|
4
|
+
import { drawImage } from './image';
|
|
5
|
+
import { drawSVG } from './svg';
|
|
6
|
+
import { drawHTML } from './html';
|
|
7
|
+
import { drawText } from './text';
|
|
8
|
+
import { drawCircle } from './circle';
|
|
9
|
+
export function drawContext(ctx, data, loader) {
|
|
10
|
+
var _a;
|
|
11
|
+
clearContext(ctx);
|
|
12
|
+
const size = ctx.getSize();
|
|
13
|
+
ctx.clearRect(0, 0, size.contextWidth, size.contextHeight);
|
|
14
|
+
if (typeof data.bgColor === 'string' && isColorStr(data.bgColor)) {
|
|
15
|
+
drawBgColor(ctx, data.bgColor);
|
|
16
|
+
}
|
|
17
|
+
if (!(data.elements.length > 0)) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
for (let i = 0; i < data.elements.length; i++) {
|
|
21
|
+
const elem = data.elements[i];
|
|
22
|
+
if (((_a = elem === null || elem === void 0 ? void 0 : elem.operation) === null || _a === void 0 ? void 0 : _a.invisible) === true) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
switch (elem.type) {
|
|
26
|
+
case 'rect': {
|
|
27
|
+
drawRect(ctx, elem);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case 'text': {
|
|
31
|
+
drawText(ctx, elem, loader);
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
case 'image': {
|
|
35
|
+
drawImage(ctx, elem, loader);
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
case 'svg': {
|
|
39
|
+
drawSVG(ctx, elem, loader);
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
case 'html': {
|
|
43
|
+
drawHTML(ctx, elem, loader);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case 'circle': {
|
|
47
|
+
drawCircle(ctx, elem);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
default: {
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { rotateElement } from '../transform';
|
|
2
|
+
export function drawSVG(ctx, elem, loader) {
|
|
3
|
+
const content = loader.getContent(elem.uuid);
|
|
4
|
+
rotateElement(ctx, elem, () => {
|
|
5
|
+
if (content) {
|
|
6
|
+
ctx.drawImage(content, elem.x, elem.y, elem.w, elem.h);
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { is, isColorStr } from '@idraw/util';
|
|
2
|
+
import { clearContext, drawBox } from './base';
|
|
3
|
+
import { rotateElement } from './../transform';
|
|
4
|
+
export function drawText(ctx, elem, loader) {
|
|
5
|
+
clearContext(ctx);
|
|
6
|
+
drawBox(ctx, elem, elem.desc.bgColor || 'transparent');
|
|
7
|
+
rotateElement(ctx, elem, () => {
|
|
8
|
+
const desc = Object.assign({
|
|
9
|
+
fontSize: 12,
|
|
10
|
+
fontFamily: 'sans-serif',
|
|
11
|
+
textAlign: 'center',
|
|
12
|
+
}, elem.desc);
|
|
13
|
+
ctx.setFillStyle(elem.desc.color);
|
|
14
|
+
ctx.setTextBaseline('top');
|
|
15
|
+
ctx.setFont({
|
|
16
|
+
fontWeight: desc.fontWeight,
|
|
17
|
+
fontSize: desc.fontSize,
|
|
18
|
+
fontFamily: desc.fontFamily
|
|
19
|
+
});
|
|
20
|
+
const descText = desc.text.replace(/\r\n/ig, '\n');
|
|
21
|
+
const fontHeight = desc.lineHeight || desc.fontSize;
|
|
22
|
+
const descTextList = descText.split('\n');
|
|
23
|
+
const lines = [];
|
|
24
|
+
let lineNum = 0;
|
|
25
|
+
descTextList.forEach((tempText, idx) => {
|
|
26
|
+
let lineText = '';
|
|
27
|
+
if (tempText.length > 0) {
|
|
28
|
+
for (let i = 0; i < tempText.length; i++) {
|
|
29
|
+
if (ctx.measureText(lineText + (tempText[i] || '')).width < ctx.calcDeviceNum(elem.w)) {
|
|
30
|
+
lineText += (tempText[i] || '');
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
lines.push({
|
|
34
|
+
text: lineText,
|
|
35
|
+
width: ctx.calcScreenNum(ctx.measureText(lineText).width),
|
|
36
|
+
});
|
|
37
|
+
lineText = (tempText[i] || '');
|
|
38
|
+
lineNum++;
|
|
39
|
+
}
|
|
40
|
+
if ((lineNum + 1) * fontHeight > elem.h) {
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
if (tempText.length - 1 === i) {
|
|
44
|
+
if ((lineNum + 1) * fontHeight < elem.h) {
|
|
45
|
+
lines.push({
|
|
46
|
+
text: lineText,
|
|
47
|
+
width: ctx.calcScreenNum(ctx.measureText(lineText).width),
|
|
48
|
+
});
|
|
49
|
+
if (idx < descTextList.length - 1) {
|
|
50
|
+
lineNum++;
|
|
51
|
+
}
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
lines.push({
|
|
59
|
+
text: '',
|
|
60
|
+
width: 0,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
{
|
|
65
|
+
let _y = elem.y;
|
|
66
|
+
if (lines.length * fontHeight < elem.h) {
|
|
67
|
+
_y += ((elem.h - lines.length * fontHeight) / 2);
|
|
68
|
+
}
|
|
69
|
+
if (desc.textShadowColor !== undefined && isColorStr(desc.textShadowColor)) {
|
|
70
|
+
ctx.setShadowColor(desc.textShadowColor);
|
|
71
|
+
}
|
|
72
|
+
if (desc.textShadowOffsetX !== undefined && is.number(desc.textShadowOffsetX)) {
|
|
73
|
+
ctx.setShadowOffsetX(desc.textShadowOffsetX);
|
|
74
|
+
}
|
|
75
|
+
if (desc.textShadowOffsetY !== undefined && is.number(desc.textShadowOffsetY)) {
|
|
76
|
+
ctx.setShadowOffsetY(desc.textShadowOffsetY);
|
|
77
|
+
}
|
|
78
|
+
if (desc.textShadowBlur !== undefined && is.number(desc.textShadowBlur)) {
|
|
79
|
+
ctx.setShadowBlur(desc.textShadowBlur);
|
|
80
|
+
}
|
|
81
|
+
lines.forEach((line, i) => {
|
|
82
|
+
let _x = elem.x;
|
|
83
|
+
if (desc.textAlign === 'center') {
|
|
84
|
+
_x = elem.x + (elem.w - line.width) / 2;
|
|
85
|
+
}
|
|
86
|
+
else if (desc.textAlign === 'right') {
|
|
87
|
+
_x = elem.x + (elem.w - line.width);
|
|
88
|
+
}
|
|
89
|
+
ctx.fillText(line.text, _x, _y + fontHeight * i);
|
|
90
|
+
});
|
|
91
|
+
clearContext(ctx);
|
|
92
|
+
}
|
|
93
|
+
if (isColorStr(desc.strokeColor) && desc.strokeWidth !== undefined && desc.strokeWidth > 0) {
|
|
94
|
+
let _y = elem.y;
|
|
95
|
+
if (lines.length * fontHeight < elem.h) {
|
|
96
|
+
_y += ((elem.h - lines.length * fontHeight) / 2);
|
|
97
|
+
}
|
|
98
|
+
lines.forEach((line, i) => {
|
|
99
|
+
let _x = elem.x;
|
|
100
|
+
if (desc.textAlign === 'center') {
|
|
101
|
+
_x = elem.x + (elem.w - line.width) / 2;
|
|
102
|
+
}
|
|
103
|
+
else if (desc.textAlign === 'right') {
|
|
104
|
+
_x = elem.x + (elem.w - line.width);
|
|
105
|
+
}
|
|
106
|
+
if (desc.strokeColor !== undefined) {
|
|
107
|
+
ctx.setStrokeStyle(desc.strokeColor);
|
|
108
|
+
}
|
|
109
|
+
if (desc.strokeWidth !== undefined && desc.strokeWidth > 0) {
|
|
110
|
+
ctx.setLineWidth(desc.strokeWidth);
|
|
111
|
+
}
|
|
112
|
+
ctx.strokeText(line.text, _x, _y + fontHeight * i);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|