modern-canvas 0.6.4 → 0.6.6

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/index.cjs CHANGED
@@ -21,7 +21,7 @@ function customNode(name, defaultProperties) {
21
21
  });
22
22
  if (defaultProperties) {
23
23
  Object.keys(defaultProperties).forEach((key) => {
24
- modernIdoc.defineProperty(constructor.prototype, key, {
24
+ modernIdoc.defineProperty(constructor, key, {
25
25
  fallback: defaultProperties[key]
26
26
  });
27
27
  });
@@ -131,245 +131,6 @@ async function nextTick(cb) {
131
131
  });
132
132
  }
133
133
 
134
- class EventEmitter {
135
- eventListeners = /* @__PURE__ */ new Map();
136
- removeAllListeners() {
137
- this.eventListeners.clear();
138
- return this;
139
- }
140
- hasEventListener(event) {
141
- return this.eventListeners.has(event);
142
- }
143
- on(type, listener, options) {
144
- const object = { value: listener, options };
145
- const listeners = this.eventListeners.get(type);
146
- if (!listeners) {
147
- this.eventListeners.set(type, object);
148
- } else if (Array.isArray(listeners)) {
149
- listeners.push(object);
150
- } else {
151
- this.eventListeners.set(type, [listeners, object]);
152
- }
153
- return this;
154
- }
155
- once(type, listener) {
156
- return this.on(type, listener, { once: true });
157
- }
158
- off(type, listener, options) {
159
- if (!listener) {
160
- this.eventListeners.delete(type);
161
- return this;
162
- }
163
- const listeners = this.eventListeners.get(type);
164
- if (!listeners) {
165
- return this;
166
- }
167
- if (Array.isArray(listeners)) {
168
- const events = [];
169
- for (let i = 0, length = listeners.length; i < length; i++) {
170
- const object = listeners[i];
171
- if (object.value !== listener || typeof options === "object" && options?.once && (typeof object.options === "boolean" || !object.options?.once)) {
172
- events.push(object);
173
- }
174
- }
175
- if (events.length) {
176
- this.eventListeners.set(type, events.length === 1 ? events[0] : events);
177
- } else {
178
- this.eventListeners.delete(type);
179
- }
180
- } else {
181
- if (listeners.value === listener && (typeof options === "boolean" || !options?.once || (typeof listeners.options === "boolean" || listeners.options?.once))) {
182
- this.eventListeners.delete(type);
183
- }
184
- }
185
- return this;
186
- }
187
- emit(type, ...args) {
188
- const listeners = this.eventListeners.get(type);
189
- if (listeners) {
190
- if (Array.isArray(listeners)) {
191
- for (let len = listeners.length, i = 0; i < len; i++) {
192
- const object = listeners[i];
193
- if (typeof object.options === "object" && object.options?.once) {
194
- this.off(type, object.value, object.options);
195
- }
196
- object.value.apply(this, args);
197
- }
198
- } else {
199
- if (typeof listeners.options === "object" && listeners.options?.once) {
200
- this.off(type, listeners.value, listeners.options);
201
- }
202
- listeners.value.apply(this, args);
203
- }
204
- return true;
205
- } else {
206
- return false;
207
- }
208
- }
209
- }
210
-
211
- let IID = 0;
212
- class CoreObject extends EventEmitter {
213
- instanceId = ++IID;
214
- _customPropertyAccessor;
215
- _properties = /* @__PURE__ */ new Map();
216
- _updatedProperties = /* @__PURE__ */ new Map();
217
- _changedProperties = /* @__PURE__ */ new Set();
218
- _updatingPromise = Promise.resolve();
219
- _updating = false;
220
- useCustomPropertyAccessor(accessor) {
221
- this._customPropertyAccessor = accessor;
222
- this.getPropertyDeclarations().forEach((declaration, key) => {
223
- const newValue = accessor.get(key, () => void 0);
224
- const oldValue = this._properties.get(key);
225
- if (newValue === void 0) {
226
- if (oldValue !== void 0) {
227
- accessor.set(key, oldValue);
228
- }
229
- } else if (newValue !== oldValue) {
230
- this._properties.set(key, newValue);
231
- this._updateProperty(key, newValue, oldValue, declaration);
232
- this.emit("updateProperty", key, newValue, oldValue, declaration);
233
- }
234
- });
235
- return this;
236
- }
237
- getter(key, context) {
238
- if (context.declaration.protected) {
239
- return this[context.internalKey];
240
- } else {
241
- return this._customPropertyAccessor ? this._customPropertyAccessor.get(key, () => this._properties.get(key)) : this._properties.get(key);
242
- }
243
- }
244
- setter(key, value, context) {
245
- if (context.declaration.protected) {
246
- this[context.internalKey] = value;
247
- } else {
248
- if (this._customPropertyAccessor) {
249
- this._customPropertyAccessor.set(key, value);
250
- }
251
- this._properties.set(key, value);
252
- }
253
- }
254
- equal(target) {
255
- return Boolean(target && this.instanceId === target.instanceId);
256
- }
257
- async _enqueueUpdate() {
258
- this._updating = true;
259
- try {
260
- await this._updatingPromise;
261
- } catch (e) {
262
- Promise.reject(e);
263
- }
264
- await nextTick();
265
- if (!this._updating)
266
- return;
267
- this.update();
268
- this._updating = false;
269
- }
270
- update() {
271
- this._update(this._updatedProperties);
272
- this._updatedProperties = /* @__PURE__ */ new Map();
273
- }
274
- // eslint-disable-next-line unused-imports/no-unused-vars
275
- _update(changed) {
276
- }
277
- // eslint-disable-next-line unused-imports/no-unused-vars
278
- _updateProperty(key, value, oldValue, declaration) {
279
- }
280
- isDirty(key) {
281
- return this._updatedProperties.has(key);
282
- }
283
- getPropertyDeclarations() {
284
- return modernIdoc.getDeclarations(this.constructor);
285
- }
286
- getPropertyDeclaration(key) {
287
- return this.getPropertyDeclarations().get(key);
288
- }
289
- getProperty(key) {
290
- return this[key];
291
- }
292
- setProperty(key, value) {
293
- this[key] = value;
294
- return this;
295
- }
296
- getProperties(keys) {
297
- const properties = {};
298
- for (const [name, property] of this.getPropertyDeclarations()) {
299
- if (!property.protected && !property.alias && (!keys || keys.includes(name))) {
300
- properties[name] = this.getProperty(name);
301
- }
302
- }
303
- return properties;
304
- }
305
- setProperties(properties) {
306
- if (properties && typeof properties === "object") {
307
- for (const [name] of this.getPropertyDeclarations()) {
308
- if (name in properties) {
309
- this.setProperty(name, properties[name]);
310
- }
311
- }
312
- }
313
- return this;
314
- }
315
- resetProperties() {
316
- for (const [name, property] of this.getPropertyDeclarations()) {
317
- this.setProperty(
318
- name,
319
- typeof property.fallback === "function" ? property.fallback() : property.fallback
320
- );
321
- }
322
- return this;
323
- }
324
- onUpdateProperty(key, newValue, oldValue, declaration) {
325
- this.requestUpdate(key, newValue, oldValue, declaration);
326
- }
327
- requestUpdate(key, newValue, oldValue, declaration) {
328
- if (key !== void 0) {
329
- if (!Object.is(newValue, oldValue)) {
330
- this._updatedProperties.set(key, oldValue);
331
- this._changedProperties.add(key);
332
- declaration ??= this.getPropertyDeclaration(key);
333
- this._updateProperty(key, newValue, oldValue, declaration);
334
- this.emit("updateProperty", key, newValue, oldValue, declaration);
335
- } else {
336
- return;
337
- }
338
- }
339
- if (!this._updating) {
340
- this._updatingPromise = this._enqueueUpdate();
341
- }
342
- }
343
- toJSON() {
344
- const json = {};
345
- this._properties.forEach((value, key) => {
346
- if (value === void 0) {
347
- return;
348
- }
349
- if (value && typeof value === "object") {
350
- if ("toJSON" in value && typeof value.toJSON === "function") {
351
- json[key] = value.toJSON();
352
- } else {
353
- json[key] = { ...value };
354
- }
355
- } else {
356
- json[key] = value;
357
- }
358
- });
359
- return json;
360
- }
361
- clone() {
362
- return new this.constructor(this.toJSON());
363
- }
364
- free() {
365
- this.removeAllListeners();
366
- }
367
- }
368
-
369
- class RefCounted extends CoreObject {
370
- //
371
- }
372
-
373
134
  const PI = Math.PI;
374
135
  const PI_2 = PI * 2;
375
136
  let UID = 0;
@@ -737,7 +498,7 @@ const TOUCH_TO_POINTER = {
737
498
  touchmove: "pointermove",
738
499
  touchcancel: "pointercancel"
739
500
  };
740
- class Input extends EventEmitter {
501
+ class Input extends modernIdoc.EventEmitter {
741
502
  target;
742
503
  cursor = "default";
743
504
  cursorStyles = {
@@ -1055,6 +816,172 @@ class Input extends EventEmitter {
1055
816
  };
1056
817
  }
1057
818
 
819
+ let IID = 0;
820
+ class CoreObject extends modernIdoc.EventEmitter {
821
+ instanceId = ++IID;
822
+ _customPropertyAccessor;
823
+ _properties = /* @__PURE__ */ new Map();
824
+ _updatedProperties = /* @__PURE__ */ new Map();
825
+ _changedProperties = /* @__PURE__ */ new Set();
826
+ _updatingPromise = Promise.resolve();
827
+ _updating = false;
828
+ useCustomPropertyAccessor(accessor) {
829
+ this._customPropertyAccessor = accessor;
830
+ this.getPropertyDeclarations().forEach((declaration, key) => {
831
+ const newValue = accessor.get(key, () => void 0);
832
+ const oldValue = this._properties.get(key);
833
+ if (newValue === void 0) {
834
+ if (oldValue !== void 0) {
835
+ accessor.set(key, oldValue);
836
+ }
837
+ } else if (newValue !== oldValue) {
838
+ if (declaration.alias && declaration.alias !== key) {
839
+ this.setProperty(key, newValue);
840
+ } else {
841
+ this._properties.set(key, newValue);
842
+ }
843
+ this._updateProperty(key, newValue, oldValue, declaration);
844
+ this.emit("updateProperty", key, newValue, oldValue, declaration);
845
+ }
846
+ });
847
+ return this;
848
+ }
849
+ getter(key, context) {
850
+ if (context?.declaration.protected) {
851
+ return this[context.internalKey];
852
+ } else {
853
+ return this._customPropertyAccessor ? this._customPropertyAccessor.get(key, () => this._properties.get(key)) : this._properties.get(key);
854
+ }
855
+ }
856
+ setter(key, value, context) {
857
+ if (context?.declaration.protected) {
858
+ this[context.internalKey] = value;
859
+ } else {
860
+ if (this._customPropertyAccessor) {
861
+ this._customPropertyAccessor.set(key, value);
862
+ }
863
+ this._properties.set(key, value);
864
+ }
865
+ }
866
+ equal(target) {
867
+ return Boolean(target && this.instanceId === target.instanceId);
868
+ }
869
+ async _enqueueUpdate() {
870
+ this._updating = true;
871
+ try {
872
+ await this._updatingPromise;
873
+ } catch (e) {
874
+ Promise.reject(e);
875
+ }
876
+ await nextTick();
877
+ if (!this._updating)
878
+ return;
879
+ this.update();
880
+ this._updating = false;
881
+ }
882
+ update() {
883
+ this._update(this._updatedProperties);
884
+ this._updatedProperties = /* @__PURE__ */ new Map();
885
+ }
886
+ // eslint-disable-next-line unused-imports/no-unused-vars
887
+ _update(changed) {
888
+ }
889
+ // eslint-disable-next-line unused-imports/no-unused-vars
890
+ _updateProperty(key, value, oldValue, declaration) {
891
+ }
892
+ isDirty(key) {
893
+ return this._updatedProperties.has(key);
894
+ }
895
+ getPropertyDeclarations() {
896
+ return modernIdoc.getDeclarations(this.constructor);
897
+ }
898
+ getPropertyDeclaration(key) {
899
+ return this.getPropertyDeclarations().get(key);
900
+ }
901
+ getProperty(key) {
902
+ return this[key];
903
+ }
904
+ setProperty(key, value) {
905
+ this[key] = value;
906
+ return this;
907
+ }
908
+ getProperties(keys) {
909
+ const properties = {};
910
+ for (const [name, property] of this.getPropertyDeclarations()) {
911
+ if (!property.protected && !property.alias && (!keys || keys.includes(name))) {
912
+ properties[name] = this.getProperty(name);
913
+ }
914
+ }
915
+ return properties;
916
+ }
917
+ setProperties(properties) {
918
+ if (properties && typeof properties === "object") {
919
+ for (const [name] of this.getPropertyDeclarations()) {
920
+ if (name in properties) {
921
+ this.setProperty(name, properties[name]);
922
+ }
923
+ }
924
+ }
925
+ return this;
926
+ }
927
+ resetProperties() {
928
+ for (const [name, property] of this.getPropertyDeclarations()) {
929
+ this.setProperty(
930
+ name,
931
+ typeof property.fallback === "function" ? property.fallback() : property.fallback
932
+ );
933
+ }
934
+ return this;
935
+ }
936
+ onUpdateProperty(key, newValue, oldValue, declaration) {
937
+ this.requestUpdate(key, newValue, oldValue, declaration);
938
+ }
939
+ requestUpdate(key, newValue, oldValue, declaration) {
940
+ if (key !== void 0) {
941
+ if (!Object.is(newValue, oldValue)) {
942
+ this._updatedProperties.set(key, oldValue);
943
+ this._changedProperties.add(key);
944
+ declaration ??= this.getPropertyDeclaration(key);
945
+ this._updateProperty(key, newValue, oldValue, declaration);
946
+ this.emit("updateProperty", key, newValue, oldValue, declaration);
947
+ } else {
948
+ return;
949
+ }
950
+ }
951
+ if (!this._updating) {
952
+ this._updatingPromise = this._enqueueUpdate();
953
+ }
954
+ }
955
+ toJSON() {
956
+ const json = {};
957
+ this._properties.forEach((value, key) => {
958
+ if (value === void 0) {
959
+ return;
960
+ }
961
+ if (value && typeof value === "object") {
962
+ if ("toJSON" in value && typeof value.toJSON === "function") {
963
+ json[key] = value.toJSON();
964
+ } else {
965
+ json[key] = { ...value };
966
+ }
967
+ } else {
968
+ json[key] = value;
969
+ }
970
+ });
971
+ return json;
972
+ }
973
+ clone() {
974
+ return new this.constructor(this.toJSON());
975
+ }
976
+ free() {
977
+ this.removeAllListeners();
978
+ }
979
+ }
980
+
981
+ class RefCounted extends CoreObject {
982
+ //
983
+ }
984
+
1058
985
  class Resource extends RefCounted {
1059
986
  //
1060
987
  }
@@ -1143,7 +1070,7 @@ class Color {
1143
1070
  }
1144
1071
  }
1145
1072
 
1146
- class Vector extends EventEmitter {
1073
+ class Vector extends modernIdoc.EventEmitter {
1147
1074
  constructor(dim) {
1148
1075
  super();
1149
1076
  this.dim = dim;
@@ -1280,7 +1207,7 @@ class Vector extends EventEmitter {
1280
1207
  }
1281
1208
  }
1282
1209
 
1283
- class Matrix extends EventEmitter {
1210
+ class Matrix extends modernIdoc.EventEmitter {
1284
1211
  constructor(rows, cols, array) {
1285
1212
  super();
1286
1213
  this.rows = rows;
@@ -9326,7 +9253,7 @@ class BaseElement2DStyle extends Resource {
9326
9253
  const defaultStyles$1 = modernIdoc.getDefaultStyle();
9327
9254
  for (const key in defaultStyles$1) {
9328
9255
  const fallback = defaultStyles$1[key];
9329
- modernIdoc.defineProperty(BaseElement2DStyle.prototype, key, { fallback });
9256
+ modernIdoc.defineProperty(BaseElement2DStyle, key, { fallback });
9330
9257
  }
9331
9258
 
9332
9259
  var __defProp$h = Object.defineProperty;
@@ -9342,6 +9269,15 @@ class BaseElement2DText extends CoreObject {
9342
9269
  constructor(parent) {
9343
9270
  super();
9344
9271
  this.parent = parent;
9272
+ this.base.on("updateProperty", (...args) => {
9273
+ switch (args[0]) {
9274
+ case "content":
9275
+ case "effects":
9276
+ this.setter(args[0], args[1]);
9277
+ break;
9278
+ }
9279
+ this._updateProperty(...args);
9280
+ });
9345
9281
  }
9346
9282
  base = new modernText.Text();
9347
9283
  measureResult;
@@ -9889,7 +9825,7 @@ const defaultStyles = {
9889
9825
  height: 0
9890
9826
  };
9891
9827
  for (const key in defaultStyles) {
9892
- modernIdoc.defineProperty(Element2DStyle.prototype, key, { fallback: defaultStyles[key] });
9828
+ modernIdoc.defineProperty(Element2DStyle, key, { fallback: defaultStyles[key] });
9893
9829
  }
9894
9830
 
9895
9831
  var __getOwnPropDesc$j = Object.getOwnPropertyDescriptor;
@@ -11162,7 +11098,7 @@ exports.Animation = __decorateClass$d([
11162
11098
  })
11163
11099
  ], exports.Animation);
11164
11100
 
11165
- class HTMLAudioContext extends EventEmitter {
11101
+ class HTMLAudioContext extends modernIdoc.EventEmitter {
11166
11102
  static _instance;
11167
11103
  static get instance() {
11168
11104
  if (!this._instance) {
@@ -11206,7 +11142,7 @@ class HTMLAudioContext extends EventEmitter {
11206
11142
  }
11207
11143
  }
11208
11144
 
11209
- class HTMLSound extends EventEmitter {
11145
+ class HTMLSound extends modernIdoc.EventEmitter {
11210
11146
  static PADDING = 0.1;
11211
11147
  _source = null;
11212
11148
  _audio = null;
@@ -11477,7 +11413,7 @@ class HTMLAudio {
11477
11413
  }
11478
11414
  }
11479
11415
 
11480
- class AudioPipeline extends EventEmitter {
11416
+ class AudioPipeline extends modernIdoc.EventEmitter {
11481
11417
  constructor(_input, _output) {
11482
11418
  super();
11483
11419
  this._input = _input;
@@ -11701,7 +11637,7 @@ class WebAudioContext extends AudioPipeline {
11701
11637
  }
11702
11638
  }
11703
11639
 
11704
- class WebSound extends EventEmitter {
11640
+ class WebSound extends modernIdoc.EventEmitter {
11705
11641
  _audio = null;
11706
11642
  _sourceNode = null;
11707
11643
  _gain = null;
@@ -12843,13 +12779,13 @@ exports.Scaler = class Scaler extends exports.Node {
12843
12779
  }
12844
12780
  };
12845
12781
  __decorateClass$4([
12846
- modernIdoc.property()
12782
+ modernIdoc.property({ default: 1 })
12847
12783
  ], exports.Scaler.prototype, "value", 2);
12848
12784
  __decorateClass$4([
12849
- modernIdoc.property()
12785
+ modernIdoc.property({ default: 0.05 })
12850
12786
  ], exports.Scaler.prototype, "minValue", 2);
12851
12787
  __decorateClass$4([
12852
- modernIdoc.property()
12788
+ modernIdoc.property({ default: 10 })
12853
12789
  ], exports.Scaler.prototype, "maxValue", 2);
12854
12790
  exports.Scaler = __decorateClass$4([
12855
12791
  customNode("Scaler", {
@@ -14014,7 +13950,6 @@ exports.DEVICE_PIXEL_RATIO = DEVICE_PIXEL_RATIO;
14014
13950
  exports.EffectMaterial = EffectMaterial;
14015
13951
  exports.Element2DStyle = Element2DStyle;
14016
13952
  exports.Engine = Engine;
14017
- exports.EventEmitter = EventEmitter;
14018
13953
  exports.FlexElement2DStyle = FlexElement2DStyle;
14019
13954
  exports.FlexLayout = FlexLayout;
14020
13955
  exports.FontLoader = FontLoader;