-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
pattern.class.ts
210 lines (183 loc) · 5.47 KB
/
pattern.class.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//@ts-nocheck
import { config } from './config';
import { TCrossOrigin, TMat2D, TSize } from './typedefs';
import { ifNaN } from './util/internals';
import { uid } from './util/internals/uid';
import { loadImage } from './util/misc/objectEnlive';
import { pick } from './util/misc/pick';
import { toFixed } from './util/misc/toFixed';
import { classRegistry } from './util/class_registry';
export type TPatternRepeat = 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat';
type TExportedKeys =
| 'crossOrigin'
| 'offsetX'
| 'offsetY'
| 'patternTransform'
| 'repeat'
| 'source';
export type TPatternOptions = Partial<Pick<Pattern, TExportedKeys>>;
export type TPatternSerialized = TPatternOptions & {
source: string;
};
export type TPatternHydrationOptions = {
/**
* handle aborting
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortController/signal
*/
signal?: AbortSignal;
};
type TImageSource = { source: HTMLImageElement };
type TCanvasSource = { source: HTMLCanvasElement };
/**
* @see {@link http://fabricjs.com/patterns demo}
* @see {@link http://fabricjs.com/dynamic-patterns demo}
*/
export class Pattern {
type = 'pattern';
/**
* @type TPatternRepeat
* @defaults
*/
repeat: TPatternRepeat = 'repeat';
/**
* Pattern horizontal offset from object's left/top corner
* @type Number
* @default
*/
offsetX = 0;
/**
* Pattern vertical offset from object's left/top corner
* @type Number
* @default
*/
offsetY = 0;
/**
* @type TCrossOrigin
* @default
*/
crossOrigin: TCrossOrigin = '';
/**
* transform matrix to change the pattern, imported from svgs.
* @type Array
* @default
*/
patternTransform: TMat2D | null = null;
/**
* The actual pixel source of the pattern
*/
source!: CanvasImageSource;
/**
* If true, this object will not be exported during the serialization of a canvas
* @type boolean
*/
excludeFromExport?: boolean;
readonly id: number;
/**
* Constructor
* @param {Object} [options] Options object
* @param {option.source} [source] the pattern source, eventually empty or a drawable
*/
constructor(options: TPatternOptions = {}) {
this.id = uid();
this.setOptions(options);
}
setOptions<K extends TExportedKeys>(options: Record<K, this[K]>) {
for (const prop in options) {
this[prop] = options[prop];
}
}
/**
* @returns true if {@link source} is an <img> element
*/
isImageSource(): this is TImageSource {
return typeof this.source.src === 'string';
}
/**
* @returns true if {@link source} is a <canvas> element
*/
isCanvasSource(): this is TCanvasSource {
return typeof this.source === 'object' && this.source.toDataURL;
}
sourceToString() {
return this.isImageSource()
? this.source.src
: this.isCanvasSource()
? this.source.toDataURL()
: '';
}
/**
* Returns an instance of CanvasPattern
* @param {CanvasRenderingContext2D} ctx Context to create pattern
* @return {CanvasPattern}
*/
toLive(ctx: CanvasRenderingContext2D): CanvasPattern | string {
if (
// if the image failed to load, return, and allow rest to continue loading
!this.source ||
// if an image
(this.isImageSource() &&
(!this.source.complete ||
this.source.naturalWidth === 0 ||
this.source.naturalHeight === 0))
) {
return '';
}
return ctx.createPattern(this.source, this.repeat);
}
/**
* Returns object representation of a pattern
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
* @return {object} Object representation of a pattern instance
*/
toObject(propertiesToInclude?: (keyof this | string)[]) {
return {
...pick(this, propertiesToInclude),
type: 'pattern',
source: this.sourceToString(),
repeat: this.repeat,
crossOrigin: this.crossOrigin,
offsetX: toFixed(this.offsetX, config.NUM_FRACTION_DIGITS),
offsetY: toFixed(this.offsetY, config.NUM_FRACTION_DIGITS),
patternTransform: this.patternTransform
? this.patternTransform.concat()
: null,
};
}
/* _TO_SVG_START_ */
/**
* Returns SVG representation of a pattern
*/
toSVG({ width, height }: TSize) {
const patternSource = this.source,
patternOffsetX = ifNaN(this.offsetX / width, 0),
patternOffsetY = ifNaN(this.offsetY / height, 0),
patternWidth =
this.repeat === 'repeat-y' || this.repeat === 'no-repeat'
? 1 + Math.abs(patternOffsetX || 0)
: ifNaN(patternSource.width / width, 0),
patternHeight =
this.repeat === 'repeat-x' || this.repeat === 'no-repeat'
? 1 + Math.abs(patternOffsetY || 0)
: ifNaN(patternSource.height / height, 0);
return [
`<pattern id="SVGID_${this.id}" x="${patternOffsetX}" y="${patternOffsetY}" width="${patternWidth}" height="${patternHeight}">`,
`<image x="0" y="0" width="${patternSource.width}" height="${
patternSource.height
}" xlink:href="${this.sourceToString()}"></image>`,
`</pattern>`,
'',
].join('\n');
}
/* _TO_SVG_END_ */
static async fromObject(
{ source, ...serialized }: TPatternSerialized,
options: TPatternHydrationOptions
) {
const img = await loadImage(source, {
...options,
crossOrigin: serialized.crossOrigin,
});
return new this({ ...serialized, source: img });
}
}
classRegistry.setClass(Pattern, 'pattern');