@buncanvas/core 0.1.6 → 0.1.7

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/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export { Event } from "./src/Event";
5
5
  export { MouseEvent } from "./src/MouseEvent";
6
6
  export { WheelEvent } from "./src/WheelEvent";
7
7
  export { KeyboardEvent } from "./src/KeyboardEvent";
8
- export { Canvas } from "./src/Canvas";
8
+ export { Canvas, CanvasRenderingContext2D, CanvasGradient } from "./src/Canvas";
9
9
  export { DOMMatrix } from "./dom/DOMMatrix";
10
10
  export { Path2D } from "./src/Path2D";
11
11
  export { Window, requestAnimationFrame } from "./src/Window";
package/index.js CHANGED
@@ -612,6 +612,12 @@ class FontFace {
612
612
  }
613
613
  }
614
614
  }
615
+ // BunCanvas/internal/ConstructEnforcer.ts
616
+ var INTERNAL_TOKEN = Object.freeze({});
617
+ var throwIllegalConstructor = (object) => {
618
+ throw new TypeError(`Failed to construct '${object.constructor.name}': Illegal constructor`);
619
+ };
620
+
615
621
  // BunCanvas/src/TextMetrics.ts
616
622
  class TextMetrics {
617
623
  width;
@@ -626,7 +632,9 @@ class TextMetrics {
626
632
  alphabeticBaseline;
627
633
  hangingBaseline;
628
634
  ideographicBaseline;
629
- constructor(width, actualBoundingBoxLeft, actualBoundingBoxRight, actualBoundingBoxAscent, actualBoundingBoxDescent, fontBoundingBoxAscent, fontBoundingBoxDescent, emHeightAscent, emHeightDescent, alphabeticBaseline, hangingBaseline, ideographicBaseline) {
635
+ constructor(token, width, actualBoundingBoxLeft, actualBoundingBoxRight, actualBoundingBoxAscent, actualBoundingBoxDescent, fontBoundingBoxAscent, fontBoundingBoxDescent, emHeightAscent, emHeightDescent, alphabeticBaseline, hangingBaseline, ideographicBaseline) {
636
+ if (token !== INTERNAL_TOKEN)
637
+ throwIllegalConstructor(this);
630
638
  this.width = width;
631
639
  this.actualBoundingBoxLeft = actualBoundingBoxLeft;
632
640
  this.actualBoundingBoxRight = actualBoundingBoxRight;
@@ -1121,10 +1129,49 @@ class ImageData {
1121
1129
  width;
1122
1130
  height;
1123
1131
  data;
1124
- constructor(width, height, data) {
1132
+ colorSpace;
1133
+ constructor(arg1, arg2, arg3, arg4) {
1134
+ let data;
1135
+ let width;
1136
+ let height;
1137
+ let settings;
1138
+ if (arg1 instanceof Uint8ClampedArray) {
1139
+ data = arg1;
1140
+ width = arg2;
1141
+ if (typeof arg3 === "number") {
1142
+ height = arg3;
1143
+ settings = arg4;
1144
+ } else {
1145
+ settings = arg3;
1146
+ height = data.length / 4 / width;
1147
+ }
1148
+ if (data.length === 0 || data.length % 4 !== 0) {
1149
+ throw new SyntaxError("The input data array length must be a non-zero multiple of 4.");
1150
+ }
1151
+ if (width <= 0 || !Number.isInteger(width)) {
1152
+ throw new SyntaxError("Width must be a positive integer.");
1153
+ }
1154
+ if (height <= 0 || !Number.isInteger(height)) {
1155
+ throw new SyntaxError("Height must be a positive integer or derived from array length.");
1156
+ }
1157
+ if (data.length !== width * height * 4) {
1158
+ throw new SyntaxError("The input data array length does not match width * height * 4.");
1159
+ }
1160
+ } else if (typeof arg1 === "number" && typeof arg2 === "number") {
1161
+ width = arg1;
1162
+ height = arg2;
1163
+ settings = arg3;
1164
+ if (width <= 0 || !Number.isInteger(width) || height <= 0 || !Number.isInteger(height)) {
1165
+ throw new SyntaxError("Width and height must be positive integers.");
1166
+ }
1167
+ data = new Uint8ClampedArray(width * height * 4);
1168
+ } else {
1169
+ throw new TypeError("Failed to construct 'ImageData': Invalid arguments provided.");
1170
+ }
1125
1171
  this.width = width;
1126
1172
  this.height = height;
1127
1173
  this.data = data;
1174
+ this.colorSpace = settings?.colorSpace ?? "srgb";
1128
1175
  }
1129
1176
  }
1130
1177
 
@@ -2221,7 +2268,6 @@ function isValidColor(str) {
2221
2268
  var ptrs3 = new WeakMap;
2222
2269
  var canvasFinalizationRegistry = new FinalizationRegistry((ptr) => {
2223
2270
  lib.symbols.canvas_destroy(ptr);
2224
- console.log("destroyed Canvas!");
2225
2271
  });
2226
2272
  class CanvasGradient {
2227
2273
  type;
@@ -2229,28 +2275,30 @@ class CanvasGradient {
2229
2275
  offsets = [];
2230
2276
  colors = [];
2231
2277
  constructor(...args) {
2232
- this.type = args[0];
2278
+ if (args[0] !== INTERNAL_TOKEN)
2279
+ throwIllegalConstructor(this);
2280
+ this.type = args[1];
2233
2281
  switch (this.type) {
2234
2282
  case 0 /* Linear */: {
2235
- this.points[0] = args[1];
2236
- this.points[1] = args[2];
2237
- this.points[2] = args[3];
2238
- this.points[3] = args[4];
2283
+ this.points[0] = args[2];
2284
+ this.points[1] = args[3];
2285
+ this.points[2] = args[4];
2286
+ this.points[3] = args[5];
2239
2287
  break;
2240
2288
  }
2241
2289
  case 1 /* Radial */: {
2242
- this.points[0] = args[1];
2243
- this.points[1] = args[2];
2244
- this.points[2] = args[3];
2245
- this.points[3] = args[4];
2246
- this.points[4] = args[5];
2247
- this.points[5] = args[6];
2290
+ this.points[0] = args[2];
2291
+ this.points[1] = args[3];
2292
+ this.points[2] = args[4];
2293
+ this.points[3] = args[5];
2294
+ this.points[4] = args[6];
2295
+ this.points[5] = args[7];
2248
2296
  break;
2249
2297
  }
2250
2298
  case 2 /* Conic */: {
2251
- this.points[0] = args[1];
2252
- this.points[1] = args[2];
2253
- this.points[2] = args[3];
2299
+ this.points[0] = args[2];
2300
+ this.points[1] = args[3];
2301
+ this.points[2] = args[4];
2254
2302
  break;
2255
2303
  }
2256
2304
  }
@@ -2266,6 +2314,21 @@ class CanvasGradient {
2266
2314
  this.colors.splice(insertIdx, 0, encodeCString(color));
2267
2315
  }
2268
2316
  }
2317
+ {
2318
+ const proto = CanvasGradient.prototype;
2319
+ for (const key of Object.getOwnPropertyNames(proto)) {
2320
+ const desc = Object.getOwnPropertyDescriptor(proto, key);
2321
+ if (key == "constructor") {
2322
+ desc && (desc.value.toString = () => {
2323
+ return `function ${proto.constructor.name}() { [native code] }`;
2324
+ });
2325
+ continue;
2326
+ }
2327
+ if (typeof desc?.value === "function") {
2328
+ desc.value.toString = () => `function ${key}() { [native code] }`;
2329
+ }
2330
+ }
2331
+ }
2269
2332
 
2270
2333
  class Render2DState {
2271
2334
  lineWidth = 0;
@@ -2548,9 +2611,9 @@ class CanvasRenderingContext2D {
2548
2611
  throw new TypeError("Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(Image or Canvas)'.");
2549
2612
  }
2550
2613
  getImageData(x, y, w, h) {
2551
- const arr = new Uint8Array(w * h * 4);
2614
+ const arr = new Uint8ClampedArray(w * h * 4);
2552
2615
  lib.symbols.canvas_get_image_data(ptrs3.get(this), x, y, w, h, ptr3(arr));
2553
- return new ImageData(w, h, arr);
2616
+ return new ImageData(arr, w, h);
2554
2617
  }
2555
2618
  putImageData(data, dx, dy, dirtyX = 0, dirtyY = 0, dirtyWidth = data.width, dirtyHeight = data.height) {
2556
2619
  if (!(data instanceof ImageData)) {
@@ -2577,17 +2640,17 @@ class CanvasRenderingContext2D {
2577
2640
  createConicGradient(...args) {
2578
2641
  if (args.length < 3)
2579
2642
  throw new TypeError(`Failed to execute 'createLinearGradient' on 'CanvasRenderingContext2D': 3 arguments required, but only ${args.length} present.`);
2580
- return new CanvasGradient(2 /* Conic */, ...args);
2643
+ return new CanvasGradient(INTERNAL_TOKEN, 2 /* Conic */, ...args);
2581
2644
  }
2582
2645
  createLinearGradient(...args) {
2583
2646
  if (args.length < 4)
2584
2647
  throw new TypeError(`Failed to execute 'createLinearGradient' on 'CanvasRenderingContext2D': 4 arguments required, but only ${args.length} present.`);
2585
- return new CanvasGradient(0 /* Linear */, ...args);
2648
+ return new CanvasGradient(INTERNAL_TOKEN, 0 /* Linear */, ...args);
2586
2649
  }
2587
2650
  createRadialGradient(...args) {
2588
2651
  if (args.length < 6)
2589
2652
  throw new TypeError(`Failed to execute 'createLinearGradient' on 'CanvasRenderingContext2D': 6 arguments required, but only ${args.length} present.`);
2590
- return new CanvasGradient(1 /* Radial */, ...args);
2653
+ return new CanvasGradient(INTERNAL_TOKEN, 1 /* Radial */, ...args);
2591
2654
  }
2592
2655
  getTransform() {
2593
2656
  let arr = new Float32Array(16);
@@ -2697,9 +2760,11 @@ class CanvasRenderingContext2D {
2697
2760
  measureText(text) {
2698
2761
  const pArr = new Float32Array(12);
2699
2762
  lib.symbols.canvas_measure_text(ptrs3.get(this), encoder.encode(`${text}\x00`), ptr3(pArr));
2700
- return new TextMetrics(pArr[0], pArr[1], pArr[2], pArr[3], pArr[4], pArr[5], pArr[6], pArr[7], pArr[8], pArr[9], pArr[10], pArr[11]);
2763
+ return new TextMetrics(INTERNAL_TOKEN, pArr[0], pArr[1], pArr[2], pArr[3], pArr[4], pArr[5], pArr[6], pArr[7], pArr[8], pArr[9], pArr[10], pArr[11]);
2701
2764
  }
2702
- constructor(iptr, cnv, args) {
2765
+ constructor(iptr, cnv, args, token) {
2766
+ if (token !== INTERNAL_TOKEN)
2767
+ throwIllegalConstructor(this);
2703
2768
  const { desynchronized = false, alpha = true } = args ?? {};
2704
2769
  const cb = new JSCallback(this.#reset.bind(this), {
2705
2770
  args: [],
@@ -2760,7 +2825,7 @@ class Canvas {
2760
2825
  }
2761
2826
  getContext(contextType, args) {
2762
2827
  if (contextType == "2d") {
2763
- return new CanvasRenderingContext2D(ptrs3.get(this), this, args);
2828
+ return new CanvasRenderingContext2D(ptrs3.get(this), this, args, INTERNAL_TOKEN);
2764
2829
  }
2765
2830
  }
2766
2831
  getBoundingClientRect() {
@@ -3358,6 +3423,8 @@ class Audio {
3358
3423
  export {
3359
3424
  Audio,
3360
3425
  Canvas,
3426
+ CanvasGradient,
3427
+ CanvasRenderingContext2D,
3361
3428
  DOMMatrix,
3362
3429
  Event,
3363
3430
  FontFace,
@@ -0,0 +1,2 @@
1
+ export declare const INTERNAL_TOKEN: Readonly<{}>;
2
+ export declare const throwIllegalConstructor: (object: any) => never;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@buncanvas/core",
3
3
  "module": "index.js",
4
4
  "type": "module",
5
- "version": "0.1.6",
5
+ "version": "0.1.7",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
@@ -13,9 +13,9 @@
13
13
  "typescript": "^7"
14
14
  },
15
15
  "optionalDependencies": {
16
- "@buncanvas/native-linux-x64": "0.1.6",
17
- "@buncanvas/native-win32-x64": "0.1.6",
18
- "@buncanvas/native-darwin-x64": "0.1.6"
16
+ "@buncanvas/native-linux-x64": "0.1.7",
17
+ "@buncanvas/native-win32-x64": "0.1.7",
18
+ "@buncanvas/native-darwin-x64": "0.1.7"
19
19
  },
20
20
  "keywords": [
21
21
  "canvas",
package/src/Canvas.d.ts CHANGED
@@ -9,17 +9,17 @@ declare enum GradientType {
9
9
  Radial = 1,
10
10
  Conic = 2
11
11
  }
12
- declare class CanvasGradient {
12
+ export declare class CanvasGradient {
13
13
  type: GradientType;
14
14
  points: Float32Array<ArrayBuffer>;
15
15
  offsets: number[];
16
16
  colors: Uint8Array[];
17
- constructor(type: GradientType, a: number, x1: number, y1: number);
18
- constructor(type: GradientType, x1: number, y1: number, x2: number, y2: number);
19
- constructor(type: GradientType, x1: number, y1: number, x2: number, y2: number, r0: number, r1: number);
17
+ constructor(token: {}, type: GradientType, a: number, x1: number, y1: number);
18
+ constructor(token: {}, type: GradientType, x1: number, y1: number, x2: number, y2: number);
19
+ constructor(token: {}, type: GradientType, x1: number, y1: number, x2: number, y2: number, r0: number, r1: number);
20
20
  addColorStop(offset: number, color: string): void;
21
21
  }
22
- declare class CanvasRenderingContext2D {
22
+ export declare class CanvasRenderingContext2D {
23
23
  #private;
24
24
  get canvas(): Canvas;
25
25
  set font(css: string);
@@ -87,7 +87,7 @@ declare class CanvasRenderingContext2D {
87
87
  set textBaseline(baseline: string);
88
88
  set textAlign(alignment: string);
89
89
  measureText(text: string): TextMetrics;
90
- constructor(iptr: Pointer | null, cnv: Canvas, args: any);
90
+ constructor(iptr: Pointer | null, cnv: Canvas, args: any, token: {});
91
91
  }
92
92
  export declare class Canvas {
93
93
  #private;
@@ -1,6 +1,13 @@
1
+ type ImageDataOptions = {
2
+ colorSpace?: "srgb" | "display-p3";
3
+ pixelFormat?: "rgba-unorm8";
4
+ };
1
5
  export declare class ImageData {
2
- width: number;
3
- height: number;
4
- data: Uint8Array;
5
- constructor(width: number, height: number, data: Uint8Array);
6
+ readonly width: number;
7
+ readonly height: number;
8
+ readonly data: Uint8ClampedArray;
9
+ readonly colorSpace: "srgb" | "display-p3";
10
+ constructor(width: number, height: number, settings?: ImageDataOptions);
11
+ constructor(dataArray: Uint8ClampedArray, width: number, height?: number, settings?: ImageDataOptions);
6
12
  }
13
+ export {};
@@ -11,5 +11,5 @@ export declare class TextMetrics {
11
11
  readonly alphabeticBaseline: number;
12
12
  readonly hangingBaseline: number;
13
13
  readonly ideographicBaseline: number;
14
- constructor(width: number, actualBoundingBoxLeft: number, actualBoundingBoxRight: number, actualBoundingBoxAscent: number, actualBoundingBoxDescent: number, fontBoundingBoxAscent: number, fontBoundingBoxDescent: number, emHeightAscent: number, emHeightDescent: number, alphabeticBaseline: number, hangingBaseline: number, ideographicBaseline: number);
14
+ constructor(token: {}, width: number, actualBoundingBoxLeft: number, actualBoundingBoxRight: number, actualBoundingBoxAscent: number, actualBoundingBoxDescent: number, fontBoundingBoxAscent: number, fontBoundingBoxDescent: number, emHeightAscent: number, emHeightDescent: number, alphabeticBaseline: number, hangingBaseline: number, ideographicBaseline: number);
15
15
  }