forked from fabricjs/fabric.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
159 lines (139 loc) · 4.06 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
export type TConfiguration = Partial<BaseConfiguration>;
class BaseConfiguration {
/**
* Browser-specific constant to adjust CanvasRenderingContext2D.shadowBlur value,
* which is unitless and not rendered equally across browsers.
*
* Values that work quite well (as of October 2017) are:
* - Chrome: 1.5
* - Edge: 1.75
* - Firefox: 0.9
* - Safari: 0.95
*
* @since 2.0.0
* @type Number
* @default 1
*/
browserShadowBlurConstant = 1;
/**
* Pixel per Inch as a default value set to 96. Can be changed for more realistic conversion.
*/
DPI = 96;
/**
* Device Pixel Ratio
* @see https://developer.apple.com/library/safari/documentation/AudioVideo/Conceptual/HTML-canvas-guide/SettingUptheCanvas/SettingUptheCanvas.html
*/
devicePixelRatio = 1;
/**
* Pixel limit for cache canvases. 1Mpx , 4Mpx should be fine.
* @since 1.7.14
* @type Number
* @default
*/
perfLimitSizeTotal = 2097152;
/**
* Pixel limit for cache canvases width or height. IE fixes the maximum at 5000
* @since 1.7.14
* @type Number
* @default
*/
maxCacheSideLimit = 4096;
/**
* Lowest pixel limit for cache canvases, set at 256PX
* @since 1.7.14
* @type Number
* @default
*/
minCacheSideLimit = 256;
/**
* When 'true', style information is not retained when copy/pasting text, making
* pasted text use destination style.
* Defaults to 'false'.
* @type Boolean
* @default
* @deprecated
*/
disableStyleCopyPaste = false;
/**
* Enable webgl for filtering picture is available
* A filtering backend will be initialized, this will both take memory and
* time since a default 2048x2048 canvas will be created for the gl context
* @since 2.0.0
* @type Boolean
* @default
*/
enableGLFiltering = true;
/**
* if webgl is enabled and available, textureSize will determine the size
* of the canvas backend
*
* In order to support old hardware set to `2048` to avoid OOM
*
* @since 2.0.0
* @type Number
* @default
*/
textureSize = 4096;
/**
* Skip performance testing of setupGLContext and force the use of putImageData that seems to be the one that works best on
* Chrome + old hardware. if your users are experiencing empty images after filtering you may try to force this to true
* this has to be set before instantiating the filtering backend ( before filtering the first image )
* @type Boolean
* @default false
*/
forceGLPutImageData = false;
/**
* If disabled boundsOfCurveCache is not used. For apps that make heavy usage of pencil drawing probably disabling it is better
* @default true
*/
cachesBoundsOfCurve = true;
/**
* Map of font files
* Map<fontFamily, pathToFile> of font files
*/
fontPaths: Record</** fontFamily */ string, /** pathToFile */ string> = {};
/**
* Defines the number of fraction digits to use when serializing object values.
* Used in exporting methods (`toObject`, `toJSON`, `toSVG`)
* You can use it to increase/decrease precision of such values like left, top, scaleX, scaleY, etc.
*/
NUM_FRACTION_DIGITS = 4;
}
export class Configuration extends BaseConfiguration {
constructor(config?: TConfiguration) {
super();
this.configure(config);
}
configure(config: TConfiguration = {}) {
Object.assign(this, config);
}
/**
* Map<fontFamily, pathToFile> of font files
*/
addFonts(
paths: Record</** fontFamily */ string, /** pathToFile */ string> = {}
) {
this.fontPaths = {
...this.fontPaths,
...paths,
};
}
removeFonts(fontFamilys: string[] = []) {
fontFamilys.forEach((fontFamily) => {
delete this.fontPaths[fontFamily];
});
}
clearFonts() {
this.fontPaths = {};
}
restoreDefaults<T extends BaseConfiguration>(keys?: (keyof T)[]) {
const defaults = new BaseConfiguration() as T;
const config =
keys?.reduce((acc, key) => {
acc[key] = defaults[key];
return acc;
}, {} as T) || defaults;
this.configure(config);
}
}
export const config = new Configuration();