import { config } from '../config';
import type { Abortable, TCrossOrigin, TMat2D, TSize } from '../typedefs';
import { ifNaN } from '../util/internals/ifNaN';
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 '../ClassRegistry';
import type {
PatternRepeat,
PatternOptions,
SerializedPatternOptions,
} from './types';
import { log } from '../util/internals/console';
/**
* @see {@link http://fabricjs.com/patterns demo}
* @see {@link http://fabricjs.com/dynamic-patterns demo}
*/
export class Pattern {
static type = 'Pattern';
/**
* Legacy identifier of the class. Prefer using this.constructor.type 'Pattern'
* or utils like isPattern, or instance of to indentify a pattern in your code.
* Will be removed in future versiones
* @TODO add sustainable warning message
* @type string
* @deprecated
*/
get type() {
return 'pattern';
}
set type(value) {
log('warn', 'Setting type has no effect', value);
}
/**
* @type PatternRepeat
* @defaults
*/
repeat: PatternRepeat = '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.
* @todo verify if using the identity matrix as default makes the rest of the code more easy
* @type Array
* @default
*/
declare patternTransform?: TMat2D;
/**
* The actual pixel source of the pattern
*/
declare source: CanvasImageSource;
/**
* If true, this object will not be exported during the serialization of a canvas
* @type boolean
*/
declare excludeFromExport?: boolean;
/**
* ID used for SVG export functionalities
* @type number
*/
declare readonly id: number;
/**
* Constructor
* @param {Object} [options] Options object
* @param {option.source} [source] the pattern source, eventually empty or a drawable
*/
constructor(options: PatternOptions) {
this.id = uid();
Object.assign(this, options);
}
/**
* @returns true if {@link source} is an
element
*/
isImageSource(): this is { source: HTMLImageElement } {
return (
!!this.source && typeof (this.source as HTMLImageElement).src === 'string'
);
}
/**
* @returns true if {@link source} is a