@spiffcommerce/preview 3.6.2-rc.7 → 3.6.2-rc.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.
@@ -0,0 +1,238 @@
1
+ import { T as ThinEngine, I as InternalTexture, a as InternalTextureSource, L as Logger } from './engine.esm.js';
2
+ import { T as Texture } from './helperFunctions.esm.js';
3
+
4
+ ThinEngine.prototype.createDynamicTexture = function (width, height, generateMipMaps, samplingMode) {
5
+ const texture = new InternalTexture(this, InternalTextureSource.Dynamic);
6
+ texture.baseWidth = width;
7
+ texture.baseHeight = height;
8
+ if (generateMipMaps) {
9
+ width = this.needPOTTextures ? ThinEngine.GetExponentOfTwo(width, this._caps.maxTextureSize) : width;
10
+ height = this.needPOTTextures ? ThinEngine.GetExponentOfTwo(height, this._caps.maxTextureSize) : height;
11
+ }
12
+ // this.resetTextureCache();
13
+ texture.width = width;
14
+ texture.height = height;
15
+ texture.isReady = false;
16
+ texture.generateMipMaps = generateMipMaps;
17
+ texture.samplingMode = samplingMode;
18
+ this.updateTextureSamplingMode(samplingMode, texture);
19
+ this._internalTexturesCache.push(texture);
20
+ return texture;
21
+ };
22
+ ThinEngine.prototype.updateDynamicTexture = function (texture, source, invertY, premulAlpha = false, format, forceBindTexture = false,
23
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
24
+ allowGPUOptimization = false) {
25
+ if (!texture) {
26
+ return;
27
+ }
28
+ const gl = this._gl;
29
+ const target = gl.TEXTURE_2D;
30
+ const wasPreviouslyBound = this._bindTextureDirectly(target, texture, true, forceBindTexture);
31
+ this._unpackFlipY(invertY === undefined ? texture.invertY : invertY);
32
+ if (premulAlpha) {
33
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
34
+ }
35
+ const textureType = this._getWebGLTextureType(texture.type);
36
+ const glformat = this._getInternalFormat(format ? format : texture.format);
37
+ const internalFormat = this._getRGBABufferInternalSizedFormat(texture.type, glformat);
38
+ gl.texImage2D(target, 0, internalFormat, glformat, textureType, source);
39
+ if (texture.generateMipMaps) {
40
+ gl.generateMipmap(target);
41
+ }
42
+ if (!wasPreviouslyBound) {
43
+ this._bindTextureDirectly(target, null);
44
+ }
45
+ if (premulAlpha) {
46
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
47
+ }
48
+ texture.isReady = true;
49
+ };
50
+
51
+ /**
52
+ * A class extending Texture allowing drawing on a texture
53
+ * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/dynamicTexture
54
+ */
55
+ class DynamicTexture extends Texture {
56
+ /**
57
+ * Creates a DynamicTexture
58
+ * @param name defines the name of the texture
59
+ * @param options provides 3 alternatives for width and height of texture, a canvas, object with width and height properties, number for both width and height
60
+ * @param scene defines the scene where you want the texture
61
+ * @param generateMipMaps defines the use of MinMaps or not (default is false)
62
+ * @param samplingMode defines the sampling mode to use (default is Texture.TRILINEAR_SAMPLINGMODE)
63
+ * @param format defines the texture format to use (default is Engine.TEXTUREFORMAT_RGBA)
64
+ * @param invertY defines if the texture needs to be inverted on the y axis during loading
65
+ */
66
+ constructor(name, options, scene = null, generateMipMaps = false, samplingMode = 3, format = 5, invertY) {
67
+ super(null, scene, !generateMipMaps, invertY, samplingMode, undefined, undefined, undefined, undefined, format);
68
+ this.name = name;
69
+ this.wrapU = Texture.CLAMP_ADDRESSMODE;
70
+ this.wrapV = Texture.CLAMP_ADDRESSMODE;
71
+ this._generateMipMaps = generateMipMaps;
72
+ const engine = this._getEngine();
73
+ if (!engine) {
74
+ return;
75
+ }
76
+ if (options.getContext) {
77
+ this._canvas = options;
78
+ this._texture = engine.createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
79
+ }
80
+ else {
81
+ this._canvas = engine.createCanvas(1, 1);
82
+ if (options.width || options.width === 0) {
83
+ this._texture = engine.createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
84
+ }
85
+ else {
86
+ this._texture = engine.createDynamicTexture(options, options, generateMipMaps, samplingMode);
87
+ }
88
+ }
89
+ const textureSize = this.getSize();
90
+ if (this._canvas.width !== textureSize.width) {
91
+ this._canvas.width = textureSize.width;
92
+ }
93
+ if (this._canvas.height !== textureSize.height) {
94
+ this._canvas.height = textureSize.height;
95
+ }
96
+ this._context = this._canvas.getContext("2d");
97
+ }
98
+ /**
99
+ * Get the current class name of the texture useful for serialization or dynamic coding.
100
+ * @returns "DynamicTexture"
101
+ */
102
+ getClassName() {
103
+ return "DynamicTexture";
104
+ }
105
+ /**
106
+ * Gets the current state of canRescale
107
+ */
108
+ get canRescale() {
109
+ return true;
110
+ }
111
+ _recreate(textureSize) {
112
+ this._canvas.width = textureSize.width;
113
+ this._canvas.height = textureSize.height;
114
+ this.releaseInternalTexture();
115
+ this._texture = this._getEngine().createDynamicTexture(textureSize.width, textureSize.height, this._generateMipMaps, this.samplingMode);
116
+ }
117
+ /**
118
+ * Scales the texture
119
+ * @param ratio the scale factor to apply to both width and height
120
+ */
121
+ scale(ratio) {
122
+ const textureSize = this.getSize();
123
+ textureSize.width *= ratio;
124
+ textureSize.height *= ratio;
125
+ this._recreate(textureSize);
126
+ }
127
+ /**
128
+ * Resizes the texture
129
+ * @param width the new width
130
+ * @param height the new height
131
+ */
132
+ scaleTo(width, height) {
133
+ const textureSize = this.getSize();
134
+ textureSize.width = width;
135
+ textureSize.height = height;
136
+ this._recreate(textureSize);
137
+ }
138
+ /**
139
+ * Gets the context of the canvas used by the texture
140
+ * @returns the canvas context of the dynamic texture
141
+ */
142
+ getContext() {
143
+ return this._context;
144
+ }
145
+ /**
146
+ * Clears the texture
147
+ */
148
+ clear() {
149
+ const size = this.getSize();
150
+ this._context.fillRect(0, 0, size.width, size.height);
151
+ }
152
+ /**
153
+ * Updates the texture
154
+ * @param invertY defines the direction for the Y axis (default is true - y increases downwards)
155
+ * @param premulAlpha defines if alpha is stored as premultiplied (default is false)
156
+ * @param allowGPUOptimization true to allow some specific GPU optimizations (subject to engine feature "allowGPUOptimizationsForGUI" being true)
157
+ */
158
+ update(invertY, premulAlpha = false, allowGPUOptimization = false) {
159
+ this._getEngine().updateDynamicTexture(this._texture, this._canvas, invertY === undefined ? true : invertY, premulAlpha, this._format || undefined, undefined, allowGPUOptimization);
160
+ }
161
+ /**
162
+ * Draws text onto the texture
163
+ * @param text defines the text to be drawn
164
+ * @param x defines the placement of the text from the left
165
+ * @param y defines the placement of the text from the top when invertY is true and from the bottom when false
166
+ * @param font defines the font to be used with font-style, font-size, font-name
167
+ * @param color defines the color used for the text
168
+ * @param clearColor defines the color for the canvas, use null to not overwrite canvas
169
+ * @param invertY defines the direction for the Y axis (default is true - y increases downwards)
170
+ * @param update defines whether texture is immediately update (default is true)
171
+ */
172
+ drawText(text, x, y, font, color, clearColor, invertY, update = true) {
173
+ const size = this.getSize();
174
+ if (clearColor) {
175
+ this._context.fillStyle = clearColor;
176
+ this._context.fillRect(0, 0, size.width, size.height);
177
+ }
178
+ this._context.font = font;
179
+ if (x === null || x === undefined) {
180
+ const textSize = this._context.measureText(text);
181
+ x = (size.width - textSize.width) / 2;
182
+ }
183
+ if (y === null || y === undefined) {
184
+ const fontSize = parseInt(font.replace(/\D/g, ""));
185
+ y = size.height / 2 + fontSize / 3.65;
186
+ }
187
+ this._context.fillStyle = color || "";
188
+ this._context.fillText(text, x, y);
189
+ if (update) {
190
+ this.update(invertY);
191
+ }
192
+ }
193
+ /**
194
+ * Clones the texture
195
+ * @returns the clone of the texture.
196
+ */
197
+ clone() {
198
+ const scene = this.getScene();
199
+ if (!scene) {
200
+ return this;
201
+ }
202
+ const textureSize = this.getSize();
203
+ const newTexture = new DynamicTexture(this.name, textureSize, scene, this._generateMipMaps);
204
+ // Base texture
205
+ newTexture.hasAlpha = this.hasAlpha;
206
+ newTexture.level = this.level;
207
+ // Dynamic Texture
208
+ newTexture.wrapU = this.wrapU;
209
+ newTexture.wrapV = this.wrapV;
210
+ return newTexture;
211
+ }
212
+ /**
213
+ * Serializes the dynamic texture. The scene should be ready before the dynamic texture is serialized
214
+ * @returns a serialized dynamic texture object
215
+ */
216
+ serialize() {
217
+ const scene = this.getScene();
218
+ if (scene && !scene.isReady()) {
219
+ Logger.Warn("The scene must be ready before serializing the dynamic texture");
220
+ }
221
+ const serializationObject = super.serialize();
222
+ if (DynamicTexture._IsCanvasElement(this._canvas)) {
223
+ serializationObject.base64String = this._canvas.toDataURL();
224
+ }
225
+ serializationObject.invertY = this._invertY;
226
+ serializationObject.samplingMode = this.samplingMode;
227
+ return serializationObject;
228
+ }
229
+ static _IsCanvasElement(canvas) {
230
+ return canvas.toDataURL !== undefined;
231
+ }
232
+ /** @internal */
233
+ _rebuild() {
234
+ this.update();
235
+ }
236
+ }
237
+
238
+ export { DynamicTexture as D };