uplot-webgpu 0.1.0 → 0.2.0
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.js +0 -17
- package/index.ts +5 -0
- package/package.json +4 -69
- package/paths/ts/bars.ts +253 -0
- package/paths/ts/catmullRomCentrip.ts +127 -0
- package/paths/ts/index.ts +9 -0
- package/paths/ts/linear.ts +172 -0
- package/paths/ts/monotoneCubic.ts +70 -0
- package/paths/ts/points.ts +70 -0
- package/paths/ts/spline.ts +105 -0
- package/paths/ts/stepped.ts +126 -0
- package/paths/ts/types.ts +143 -0
- package/paths/ts/utils.ts +303 -0
- package/scripts/ts/uPlot.ts +3732 -0
- package/scripts/ts/utils/dom.ts +124 -0
- package/scripts/ts/utils/domClasses.ts +22 -0
- package/scripts/ts/utils/feats.ts +13 -0
- package/scripts/ts/utils/fmtDate.ts +398 -0
- package/scripts/ts/utils/opts.ts +844 -0
- package/scripts/ts/utils/strings.ts +22 -0
- package/scripts/ts/utils/sync.ts +27 -0
- package/scripts/ts/utils/utils.ts +692 -0
- package/scripts/{webgpu → ts/webgpu}/GPUPath.ts +92 -41
- package/scripts/{webgpu → ts/webgpu}/WebGPURenderer.ts +176 -84
- package/scripts/ts/webgpu/exporters.ts +221 -0
- package/scripts/ts/webgpu/index.ts +31 -0
- package/scripts/{webgpu → ts/webgpu}/shaders.ts +0 -1
- package/scripts/uPlot.js +0 -2
- package/scripts/webgpu/GPUPath.js +513 -606
- package/scripts/webgpu/WebGPURenderer.js +3484 -4018
- package/scripts/webgpu/exporters.js +191 -201
- package/scripts/webgpu/index.js +12 -0
- package/scripts/webgpu/shaders.js +6 -3
- package/tinybuild.config.js +6 -6
- package/tsconfig.json +64 -0
- package/scripts/uPlot.d.ts +0 -26
- package/scripts/webgpu/GPUPath.d.ts +0 -46
- package/scripts/webgpu/WebGPURenderer.d.ts +0 -176
- package/scripts/webgpu/exporters.d.ts +0 -8
- package/scripts/webgpu/shaders.d.ts +0 -2
- package/scripts/webgpu/smokeTest.d.ts +0 -2
- package/scripts/webgpu/webgpu-ambient.d.ts +0 -41
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
// @
|
|
2
|
-
import { GPUPath } from './GPUPath
|
|
3
|
-
import {
|
|
1
|
+
// import "@webgpu/types"
|
|
2
|
+
import { GPUPath } from './GPUPath';
|
|
3
|
+
import type { GPUPathSubpath } from './GPUPath';
|
|
4
|
+
import { CHART_WGSL, IMAGE_WGSL } from './shaders';
|
|
4
5
|
|
|
5
6
|
const DEFAULT_COLOR = [0, 0, 0, 1];
|
|
6
7
|
const TRANSPARENT = [0, 0, 0, 0];
|
|
@@ -759,7 +760,7 @@ function parseCssColorWithDom(str) {
|
|
|
759
760
|
if (typeof document == 'undefined')
|
|
760
761
|
return null;
|
|
761
762
|
|
|
762
|
-
let probe = parseCssColorWithDom._probe;
|
|
763
|
+
let probe = (parseCssColorWithDom as any)._probe;
|
|
763
764
|
|
|
764
765
|
if (probe == null) {
|
|
765
766
|
probe = document.createElement('span');
|
|
@@ -768,7 +769,7 @@ function parseCssColorWithDom(str) {
|
|
|
768
769
|
probe.style.top = '-99999px';
|
|
769
770
|
probe.style.visibility = 'hidden';
|
|
770
771
|
document.documentElement.appendChild(probe);
|
|
771
|
-
parseCssColorWithDom._probe = probe;
|
|
772
|
+
(parseCssColorWithDom as any)._probe = probe;
|
|
772
773
|
}
|
|
773
774
|
|
|
774
775
|
probe.style.color = '';
|
|
@@ -1563,10 +1564,19 @@ function isImageDataLike(source) {
|
|
|
1563
1564
|
return source != null && source.data != null && Number.isFinite(Number(source.width)) && Number.isFinite(Number(source.height));
|
|
1564
1565
|
}
|
|
1565
1566
|
|
|
1566
|
-
function
|
|
1567
|
+
function createImageDataPixels(width: number, height: number, data: Uint8ClampedArray | null = null): Uint8ClampedArray<ArrayBuffer> {
|
|
1568
|
+
let pixels = new Uint8ClampedArray(width * height * 4);
|
|
1569
|
+
|
|
1570
|
+
if (data instanceof Uint8ClampedArray)
|
|
1571
|
+
pixels.set(data.subarray(0, pixels.length));
|
|
1572
|
+
|
|
1573
|
+
return pixels;
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
function createImageDataObject(width: number, height: number, data: Uint8ClampedArray | null = null) {
|
|
1567
1577
|
width = Math.max(0, Math.floor(Number(width) || 0));
|
|
1568
1578
|
height = Math.max(0, Math.floor(Number(height) || 0));
|
|
1569
|
-
let pixels =
|
|
1579
|
+
let pixels = createImageDataPixels(width, height, data);
|
|
1570
1580
|
|
|
1571
1581
|
if (typeof ImageData != 'undefined') {
|
|
1572
1582
|
try {
|
|
@@ -1897,12 +1907,11 @@ async function createWebGPURuntime(owner, epoch) {
|
|
|
1897
1907
|
|
|
1898
1908
|
let adapterInfo = null;
|
|
1899
1909
|
try {
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1910
|
+
let adapterWithLegacyInfo = adapter as GPUAdapter & {requestAdapterInfo?: () => Promise<GPUAdapterInfo>};
|
|
1911
|
+
let info = adapter.info || (typeof adapterWithLegacyInfo.requestAdapterInfo == 'function' ? await adapterWithLegacyInfo.requestAdapterInfo() : null);
|
|
1912
|
+
|
|
1913
|
+
if (info)
|
|
1904
1914
|
adapterInfo = {vendor: info.vendor || '', architecture: info.architecture || '', device: info.device || '', description: info.description || ''};
|
|
1905
|
-
}
|
|
1906
1915
|
}
|
|
1907
1916
|
catch (err) {
|
|
1908
1917
|
adapterInfo = null;
|
|
@@ -1928,7 +1937,7 @@ async function createWebGPURuntime(owner, epoch) {
|
|
|
1928
1937
|
});
|
|
1929
1938
|
let pipelineLayout = device.createPipelineLayout({bindGroupLayouts: [bindGroupLayout]});
|
|
1930
1939
|
let imagePipelineLayout = device.createPipelineLayout({bindGroupLayouts: [bindGroupLayout, imageBindGroupLayout]});
|
|
1931
|
-
let runtime = {
|
|
1940
|
+
let runtime: any = {
|
|
1932
1941
|
adapter,
|
|
1933
1942
|
adapterInfo,
|
|
1934
1943
|
device,
|
|
@@ -2296,7 +2305,7 @@ function measureTextWithDom(font, text, state = null) {
|
|
|
2296
2305
|
if (typeof document == 'undefined')
|
|
2297
2306
|
return null;
|
|
2298
2307
|
|
|
2299
|
-
let probe = measureTextWithDom._probe;
|
|
2308
|
+
let probe = (measureTextWithDom as any)._probe;
|
|
2300
2309
|
|
|
2301
2310
|
if (probe == null) {
|
|
2302
2311
|
probe = document.createElement('span');
|
|
@@ -2306,7 +2315,7 @@ function measureTextWithDom(font, text, state = null) {
|
|
|
2306
2315
|
probe.style.visibility = 'hidden';
|
|
2307
2316
|
probe.style.whiteSpace = 'pre';
|
|
2308
2317
|
document.documentElement.appendChild(probe);
|
|
2309
|
-
measureTextWithDom._probe = probe;
|
|
2318
|
+
(measureTextWithDom as any)._probe = probe;
|
|
2310
2319
|
}
|
|
2311
2320
|
|
|
2312
2321
|
probe.style.font = font || '12px sans-serif';
|
|
@@ -2323,8 +2332,91 @@ function measureTextWithDom(font, text, state = null) {
|
|
|
2323
2332
|
return rect.width;
|
|
2324
2333
|
}
|
|
2325
2334
|
|
|
2335
|
+
export type RGBA = [number, number, number, number];
|
|
2336
|
+
export type WebGPUMemoryMode = 'low' | 'balanced' | 'throughput';
|
|
2337
|
+
|
|
2338
|
+
export interface WebGPURendererOptions {
|
|
2339
|
+
sharedRuntime?: boolean;
|
|
2340
|
+
alpha?: boolean;
|
|
2341
|
+
colorSpace?: PredefinedColorSpace;
|
|
2342
|
+
desynchronized?: boolean;
|
|
2343
|
+
memory?: WebGPUMemoryMode;
|
|
2344
|
+
memoryMode?: WebGPUMemoryMode;
|
|
2345
|
+
retainCommands?: boolean;
|
|
2346
|
+
retainCommandsForReadback?: boolean;
|
|
2347
|
+
releaseCommandsAfterPresent?: boolean;
|
|
2348
|
+
maxRetainedUploadBytes?: number;
|
|
2349
|
+
maxRetainedVertexBufferBytes?: number;
|
|
2350
|
+
maxTextureBytes?: number;
|
|
2351
|
+
maxTextureRecords?: number;
|
|
2352
|
+
maxColorCacheEntries?: number;
|
|
2353
|
+
maxTextMeasureCacheEntries?: number;
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
export interface WebGPURendererFrameStats {
|
|
2357
|
+
cpuMs: number;
|
|
2358
|
+
prepMs: number;
|
|
2359
|
+
uploadMs: number;
|
|
2360
|
+
submitMs: number;
|
|
2361
|
+
drawCalls: number;
|
|
2362
|
+
vertices: number;
|
|
2363
|
+
bytes: number;
|
|
2364
|
+
writes: number;
|
|
2365
|
+
images: number;
|
|
2366
|
+
clips: number;
|
|
2367
|
+
retainedCPUBytes?: number;
|
|
2368
|
+
retainedGPUBytes?: number;
|
|
2369
|
+
textureBytes?: number;
|
|
2370
|
+
commandsReleased?: boolean;
|
|
2371
|
+
}
|
|
2372
|
+
|
|
2373
|
+
export interface WebGPURendererMemoryStats {
|
|
2374
|
+
mode: WebGPUMemoryMode;
|
|
2375
|
+
commands: number;
|
|
2376
|
+
commandsReleased: boolean;
|
|
2377
|
+
retainedCPUBytes: number;
|
|
2378
|
+
retainedGPUBytes: number;
|
|
2379
|
+
solidUploadBytes: number;
|
|
2380
|
+
imageUploadBytes: number;
|
|
2381
|
+
solidBufferBytes: number;
|
|
2382
|
+
imageBufferBytes: number;
|
|
2383
|
+
textureBytes: number;
|
|
2384
|
+
textureRecords: number;
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
export interface WebGPUWarmupStats {
|
|
2388
|
+
ok: boolean;
|
|
2389
|
+
ms: number;
|
|
2390
|
+
submitted?: boolean;
|
|
2391
|
+
sharedRuntime?: boolean;
|
|
2392
|
+
error?: string;
|
|
2393
|
+
runtime?: WebGPURuntimeStats;
|
|
2394
|
+
}
|
|
2395
|
+
|
|
2396
|
+
export interface WebGPURuntimeStats {
|
|
2397
|
+
enabled?: boolean;
|
|
2398
|
+
active?: boolean;
|
|
2399
|
+
refs?: number;
|
|
2400
|
+
adapterInfo?: Record<string, unknown>;
|
|
2401
|
+
format?: GPUTextureFormat | null;
|
|
2402
|
+
compositePipelines?: number;
|
|
2403
|
+
imagePipelines?: number;
|
|
2404
|
+
}
|
|
2405
|
+
|
|
2406
|
+
export interface CanvasGradientLike {
|
|
2407
|
+
addColorStop(offset: number, color: string): void;
|
|
2408
|
+
colorAt?(x: number, y: number, alpha?: number): number[];
|
|
2409
|
+
}
|
|
2410
|
+
|
|
2411
|
+
export interface CanvasPatternLike {
|
|
2412
|
+
setTransform?(transform?: DOMMatrix2DInit): void;
|
|
2413
|
+
}
|
|
2414
|
+
|
|
2326
2415
|
export class WebGPURenderer {
|
|
2327
|
-
|
|
2416
|
+
[key: string]: any;
|
|
2417
|
+
static sharedRuntimeEnabled: boolean;
|
|
2418
|
+
static sharedRuntime: any;
|
|
2419
|
+
constructor(canvas: HTMLCanvasElement | OffscreenCanvas, options: WebGPURendererOptions | null = null) {
|
|
2328
2420
|
this.canvas = canvas;
|
|
2329
2421
|
this.options = options || {};
|
|
2330
2422
|
this.memory = normalizeMemoryOptions(this.options);
|
|
@@ -2434,15 +2526,15 @@ export class WebGPURenderer {
|
|
|
2434
2526
|
this.initPromise = this.init();
|
|
2435
2527
|
}
|
|
2436
2528
|
|
|
2437
|
-
static setSharedRuntimeEnabled(enabled) {
|
|
2529
|
+
static setSharedRuntimeEnabled(enabled: boolean): void {
|
|
2438
2530
|
sharedRuntimeEnabled = enabled !== false;
|
|
2439
2531
|
}
|
|
2440
2532
|
|
|
2441
|
-
static async prewarm(options = null) {
|
|
2533
|
+
static async prewarm(options: {sharedRuntime?: boolean; width?: number; height?: number; canvas?: HTMLCanvasElement | OffscreenCanvas} | null = null): Promise<WebGPUWarmupStats> {
|
|
2442
2534
|
return prewarmWebGPURuntime(options);
|
|
2443
2535
|
}
|
|
2444
2536
|
|
|
2445
|
-
static getSharedRuntimeStats() {
|
|
2537
|
+
static getSharedRuntimeStats(): WebGPURuntimeStats {
|
|
2446
2538
|
return {
|
|
2447
2539
|
enabled: sharedRuntimeEnabled,
|
|
2448
2540
|
active: sharedRuntime != null && !sharedRuntime.lost,
|
|
@@ -2454,23 +2546,23 @@ export class WebGPURenderer {
|
|
|
2454
2546
|
};
|
|
2455
2547
|
}
|
|
2456
2548
|
|
|
2457
|
-
getLastFrameStats() {
|
|
2549
|
+
getLastFrameStats(): WebGPURendererFrameStats {
|
|
2458
2550
|
return {...this.lastFrameStats};
|
|
2459
2551
|
}
|
|
2460
2552
|
|
|
2461
|
-
getDevice() {
|
|
2553
|
+
getDevice(): GPUDevice | null {
|
|
2462
2554
|
return this.device;
|
|
2463
2555
|
}
|
|
2464
2556
|
|
|
2465
|
-
getRuntime() {
|
|
2557
|
+
getRuntime(): unknown {
|
|
2466
2558
|
return this.runtime;
|
|
2467
2559
|
}
|
|
2468
2560
|
|
|
2469
|
-
getCanvasContext() {
|
|
2561
|
+
getCanvasContext(): GPUCanvasContext | null {
|
|
2470
2562
|
return this.context;
|
|
2471
2563
|
}
|
|
2472
2564
|
|
|
2473
|
-
async init() {
|
|
2565
|
+
async init(): Promise<void> {
|
|
2474
2566
|
let epoch = ++this.initEpoch;
|
|
2475
2567
|
|
|
2476
2568
|
if (this.disposed)
|
|
@@ -2598,7 +2690,7 @@ export class WebGPURenderer {
|
|
|
2598
2690
|
}
|
|
2599
2691
|
}
|
|
2600
2692
|
|
|
2601
|
-
mount(wrap, canvas) {
|
|
2693
|
+
mount(wrap: Element, canvas: any): void {
|
|
2602
2694
|
if (typeof document == 'undefined' || this.textLayer != null)
|
|
2603
2695
|
return;
|
|
2604
2696
|
|
|
@@ -2686,7 +2778,7 @@ export class WebGPURenderer {
|
|
|
2686
2778
|
return this.disposed || this.failed || this.device == null && this.initPromise == null;
|
|
2687
2779
|
}
|
|
2688
2780
|
|
|
2689
|
-
async flush() {
|
|
2781
|
+
async flush(): Promise<void> {
|
|
2690
2782
|
if (!this.ready && this.initPromise != null)
|
|
2691
2783
|
await this.initPromise;
|
|
2692
2784
|
try {
|
|
@@ -2740,7 +2832,7 @@ export class WebGPURenderer {
|
|
|
2740
2832
|
this.contextConfigured = false;
|
|
2741
2833
|
}
|
|
2742
2834
|
|
|
2743
|
-
getMemoryStats() {
|
|
2835
|
+
getMemoryStats(): WebGPURendererMemoryStats {
|
|
2744
2836
|
let retainedCPUBytes = byteLengthOf(this.solidUploadArray) + byteLengthOf(this.imageUploadArray) + byteLengthOf(this.uniformUploadArray);
|
|
2745
2837
|
let retainedGPUBytes = (this.vertexBufferSize || 0) + (this.imageVertexBufferSize || 0) + (this.uniformBuffer ? 16 : 0) + (this.textureBytes || 0);
|
|
2746
2838
|
return {
|
|
@@ -2788,7 +2880,7 @@ export class WebGPURenderer {
|
|
|
2788
2880
|
}
|
|
2789
2881
|
}
|
|
2790
2882
|
|
|
2791
|
-
trimMemory() {
|
|
2883
|
+
trimMemory(): WebGPURendererMemoryStats {
|
|
2792
2884
|
this.trimUploadArrays();
|
|
2793
2885
|
this.trimCaches();
|
|
2794
2886
|
if (this.commands.length == 0 || this.frameCommandsReleased)
|
|
@@ -2932,7 +3024,7 @@ export class WebGPURenderer {
|
|
|
2932
3024
|
if (this.textureRecords.size <= maxRecords && this.textureBytes <= maxBytes)
|
|
2933
3025
|
return;
|
|
2934
3026
|
|
|
2935
|
-
let victims = Array.from(this.textureRecords).filter(record => record !== keep).sort((a, b) => (a.lastUsed || 0) - (b.lastUsed || 0));
|
|
3027
|
+
let victims = Array.from(this.textureRecords as Set<any>).filter(record => record !== keep).sort((a, b) => (a.lastUsed || 0) - (b.lastUsed || 0));
|
|
2936
3028
|
for (let record of victims) {
|
|
2937
3029
|
if (this.textureRecords.size <= maxRecords && this.textureBytes <= maxBytes)
|
|
2938
3030
|
break;
|
|
@@ -2940,7 +3032,7 @@ export class WebGPURenderer {
|
|
|
2940
3032
|
}
|
|
2941
3033
|
}
|
|
2942
3034
|
|
|
2943
|
-
clearRect(x = 0, y = 0, w = this.canvas.width || 0, h = this.canvas.height || 0) {
|
|
3035
|
+
clearRect(x = 0, y = 0, w = this.canvas.width || 0, h = this.canvas.height || 0): void {
|
|
2944
3036
|
this.syncFrameMetrics();
|
|
2945
3037
|
|
|
2946
3038
|
let m = this.state.transform;
|
|
@@ -2978,7 +3070,7 @@ export class WebGPURenderer {
|
|
|
2978
3070
|
if (regions.length == 0)
|
|
2979
3071
|
return;
|
|
2980
3072
|
|
|
2981
|
-
for (let node of Array.from(this.textLayer.children)) {
|
|
3073
|
+
for (let node of Array.from(this.textLayer.children) as HTMLElement[]) {
|
|
2982
3074
|
let x = Number(node.dataset.uplotX);
|
|
2983
3075
|
let y = Number(node.dataset.uplotY);
|
|
2984
3076
|
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
@@ -3152,7 +3244,7 @@ export class WebGPURenderer {
|
|
|
3152
3244
|
pass.end();
|
|
3153
3245
|
}
|
|
3154
3246
|
|
|
3155
|
-
present() {
|
|
3247
|
+
present(): void {
|
|
3156
3248
|
this.syncFrameMetrics();
|
|
3157
3249
|
|
|
3158
3250
|
if (!this.ready) {
|
|
@@ -3234,7 +3326,7 @@ export class WebGPURenderer {
|
|
|
3234
3326
|
}
|
|
3235
3327
|
}
|
|
3236
3328
|
|
|
3237
|
-
getImageData(sx = 0, sy = 0, sw = this.frameWidth, sh = this.frameHeight) {
|
|
3329
|
+
getImageData(sx = 0, sy = 0, sw = this.frameWidth, sh = this.frameHeight): any {
|
|
3238
3330
|
this.syncFrameMetrics();
|
|
3239
3331
|
let rect = normalizeReadRect(sx, sy, sw, sh, this.frameWidth, this.frameHeight);
|
|
3240
3332
|
if (rect == null)
|
|
@@ -3242,7 +3334,7 @@ export class WebGPURenderer {
|
|
|
3242
3334
|
return createImageDataObject(rect.w, rect.h);
|
|
3243
3335
|
}
|
|
3244
3336
|
|
|
3245
|
-
async getImageDataAsync(sx = 0, sy = 0, sw = this.frameWidth, sh = this.frameHeight) {
|
|
3337
|
+
async getImageDataAsync(sx = 0, sy = 0, sw = this.frameWidth, sh = this.frameHeight): Promise<any> {
|
|
3246
3338
|
this.syncFrameMetrics();
|
|
3247
3339
|
let rect = normalizeReadRect(sx, sy, sw, sh, this.frameWidth, this.frameHeight);
|
|
3248
3340
|
if (rect == null)
|
|
@@ -3308,7 +3400,7 @@ export class WebGPURenderer {
|
|
|
3308
3400
|
}
|
|
3309
3401
|
}
|
|
3310
3402
|
|
|
3311
|
-
async exportImageBytes(type = 'image/png') {
|
|
3403
|
+
async exportImageBytes(type = 'image/png'): Promise<{type: string; bytes: Uint8Array}> {
|
|
3312
3404
|
let tools = await loadExportTools();
|
|
3313
3405
|
type = tools.normalizeExportType(type);
|
|
3314
3406
|
let img = await this.getImageDataAsync(0, 0, this.frameWidth, this.frameHeight);
|
|
@@ -3316,27 +3408,27 @@ export class WebGPURenderer {
|
|
|
3316
3408
|
return {type, bytes};
|
|
3317
3409
|
}
|
|
3318
3410
|
|
|
3319
|
-
async convertToBlob(options = {}) {
|
|
3411
|
+
async convertToBlob(options: any = {}): Promise<Blob | {type: string; size: number; arrayBuffer(): Promise<ArrayBuffer>}> {
|
|
3320
3412
|
let tools = await loadExportTools();
|
|
3321
3413
|
let requestedType = typeof options == 'string' ? options : options?.type;
|
|
3322
3414
|
let {type, bytes} = await this.exportImageBytes(requestedType || 'image/png');
|
|
3323
3415
|
return tools.bytesToBlob(bytes, type);
|
|
3324
3416
|
}
|
|
3325
3417
|
|
|
3326
|
-
async toDataURLAsync(type = 'image/png') {
|
|
3418
|
+
async toDataURLAsync(type = 'image/png'): Promise<string> {
|
|
3327
3419
|
let tools = await loadExportTools();
|
|
3328
3420
|
let exported = await this.exportImageBytes(type);
|
|
3329
3421
|
return `data:${exported.type};base64,${tools.bytesToBase64(exported.bytes)}`;
|
|
3330
3422
|
}
|
|
3331
3423
|
|
|
3332
|
-
async toBlob(callback, type = 'image/png') {
|
|
3424
|
+
async toBlob(callback?: (blob: Blob | {type: string; size: number; arrayBuffer(): Promise<ArrayBuffer>}) => void, type = 'image/png'): Promise<Blob | {type: string; size: number; arrayBuffer(): Promise<ArrayBuffer>}> {
|
|
3333
3425
|
let blob = await this.convertToBlob({type});
|
|
3334
3426
|
if (typeof callback == 'function')
|
|
3335
3427
|
callback(blob);
|
|
3336
3428
|
return blob;
|
|
3337
3429
|
}
|
|
3338
3430
|
|
|
3339
|
-
async toSVGStringAsync(options = {}) {
|
|
3431
|
+
async toSVGStringAsync(options: any = {}): Promise<string> {
|
|
3340
3432
|
let tools = await loadExportTools();
|
|
3341
3433
|
let pixelRatio = this.pixelRatio || 1;
|
|
3342
3434
|
let width = this.frameWidth / pixelRatio;
|
|
@@ -3352,36 +3444,36 @@ export class WebGPURenderer {
|
|
|
3352
3444
|
return `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}">${image}${text}</svg>`;
|
|
3353
3445
|
}
|
|
3354
3446
|
|
|
3355
|
-
async toSVGBlob(options = {}) {
|
|
3447
|
+
async toSVGBlob(options: any = {}): Promise<Blob | {type: string; size: number; arrayBuffer(): Promise<ArrayBuffer>}> {
|
|
3356
3448
|
let tools = await loadExportTools();
|
|
3357
3449
|
let svg = await this.toSVGStringAsync(options);
|
|
3358
3450
|
return tools.textToBlob(svg, 'image/svg+xml');
|
|
3359
3451
|
}
|
|
3360
3452
|
|
|
3361
|
-
async toSVGDataURLAsync(options = {}) {
|
|
3453
|
+
async toSVGDataURLAsync(options: any = {}): Promise<string> {
|
|
3362
3454
|
let tools = await loadExportTools();
|
|
3363
3455
|
let svg = await this.toSVGStringAsync(options);
|
|
3364
3456
|
return `data:image/svg+xml;base64,${tools.encodeTextBase64(svg)}`;
|
|
3365
3457
|
}
|
|
3366
3458
|
|
|
3367
|
-
save() {
|
|
3459
|
+
save(): void {
|
|
3368
3460
|
this.stack.push(cloneState(this.state));
|
|
3369
3461
|
}
|
|
3370
3462
|
|
|
3371
|
-
restore() {
|
|
3463
|
+
restore(): void {
|
|
3372
3464
|
let state = this.stack.pop();
|
|
3373
3465
|
if (state != null)
|
|
3374
3466
|
this.state = state;
|
|
3375
3467
|
}
|
|
3376
3468
|
|
|
3377
|
-
translate(x, y) {
|
|
3469
|
+
translate(x: number, y: number): void {
|
|
3378
3470
|
x = Number(x);
|
|
3379
3471
|
y = Number(y);
|
|
3380
3472
|
if (Number.isFinite(x) && Number.isFinite(y))
|
|
3381
3473
|
this.state.transform = multiply(this.state.transform, [1, 0, 0, 1, x, y]);
|
|
3382
3474
|
}
|
|
3383
3475
|
|
|
3384
|
-
rotate(angle) {
|
|
3476
|
+
rotate(angle: number): void {
|
|
3385
3477
|
angle = Number(angle);
|
|
3386
3478
|
if (!Number.isFinite(angle))
|
|
3387
3479
|
return;
|
|
@@ -3390,20 +3482,20 @@ export class WebGPURenderer {
|
|
|
3390
3482
|
this.state.transform = multiply(this.state.transform, [c, s, -s, c, 0, 0]);
|
|
3391
3483
|
}
|
|
3392
3484
|
|
|
3393
|
-
scale(x, y = x) {
|
|
3485
|
+
scale(x: number, y = x): void {
|
|
3394
3486
|
x = Number(x);
|
|
3395
3487
|
y = Number(y);
|
|
3396
3488
|
if (Number.isFinite(x) && Number.isFinite(y))
|
|
3397
3489
|
this.state.transform = multiply(this.state.transform, [x, 0, 0, y, 0, 0]);
|
|
3398
3490
|
}
|
|
3399
3491
|
|
|
3400
|
-
transform(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0) {
|
|
3492
|
+
transform(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0): void {
|
|
3401
3493
|
let next = finiteTransform([a, b, c, d, e, f]);
|
|
3402
3494
|
if (next != null)
|
|
3403
3495
|
this.state.transform = multiply(this.state.transform, next);
|
|
3404
3496
|
}
|
|
3405
3497
|
|
|
3406
|
-
getTransform() {
|
|
3498
|
+
getTransform(): DOMMatrix | {a: number; b: number; c: number; d: number; e: number; f: number; is2D: boolean} {
|
|
3407
3499
|
let [a, b, c, d, e, f] = this.state.transform;
|
|
3408
3500
|
|
|
3409
3501
|
if (typeof DOMMatrix != 'undefined')
|
|
@@ -3412,7 +3504,7 @@ export class WebGPURenderer {
|
|
|
3412
3504
|
return {a, b, c, d, e, f, is2D: true};
|
|
3413
3505
|
}
|
|
3414
3506
|
|
|
3415
|
-
setTransform(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0) {
|
|
3507
|
+
setTransform(a: any = 1, b: any = 0, c: any = 0, d: any = 1, e: any = 0, f: any = 0): void {
|
|
3416
3508
|
let next = typeof a == 'object' && a != null
|
|
3417
3509
|
? finiteTransform([a.a, a.b, a.c, a.d, a.e, a.f])
|
|
3418
3510
|
: finiteTransform([a, b, c, d, e, f]);
|
|
@@ -3420,15 +3512,15 @@ export class WebGPURenderer {
|
|
|
3420
3512
|
this.state.transform = next;
|
|
3421
3513
|
}
|
|
3422
3514
|
|
|
3423
|
-
resetTransform() {
|
|
3515
|
+
resetTransform(): void {
|
|
3424
3516
|
this.state.transform = identity();
|
|
3425
3517
|
}
|
|
3426
3518
|
|
|
3427
|
-
getLineDash() {
|
|
3519
|
+
getLineDash(): number[] {
|
|
3428
3520
|
return this.state.lineDash.slice();
|
|
3429
3521
|
}
|
|
3430
3522
|
|
|
3431
|
-
setLineDash(dash) {
|
|
3523
|
+
setLineDash(dash: number[]): void {
|
|
3432
3524
|
if (dash == null) {
|
|
3433
3525
|
this.state.lineDash = [];
|
|
3434
3526
|
return;
|
|
@@ -3442,55 +3534,55 @@ export class WebGPURenderer {
|
|
|
3442
3534
|
}
|
|
3443
3535
|
}
|
|
3444
3536
|
|
|
3445
|
-
beginPath() {
|
|
3537
|
+
beginPath(): void {
|
|
3446
3538
|
this.path.clear();
|
|
3447
3539
|
}
|
|
3448
3540
|
|
|
3449
|
-
moveTo(x, y) {
|
|
3541
|
+
moveTo(x: number, y: number): void {
|
|
3450
3542
|
this.path.moveTo(x, y);
|
|
3451
3543
|
}
|
|
3452
3544
|
|
|
3453
|
-
lineTo(x, y) {
|
|
3545
|
+
lineTo(x: number, y: number): void {
|
|
3454
3546
|
this.path.lineTo(x, y);
|
|
3455
3547
|
}
|
|
3456
3548
|
|
|
3457
|
-
rect(x, y, w, h) {
|
|
3549
|
+
rect(x: number, y: number, w: number, h: number): void {
|
|
3458
3550
|
this.path.rect(x, y, w, h);
|
|
3459
3551
|
}
|
|
3460
3552
|
|
|
3461
|
-
arc(x, y, r, startAngle, endAngle, counterclockwise = false) {
|
|
3553
|
+
arc(x: number, y: number, r: number, startAngle: number, endAngle: number, counterclockwise = false): void {
|
|
3462
3554
|
this.path.arc(x, y, r, startAngle, endAngle, counterclockwise);
|
|
3463
3555
|
}
|
|
3464
3556
|
|
|
3465
|
-
arcTo(x1, y1, x2, y2, r) {
|
|
3557
|
+
arcTo(x1: number, y1: number, x2: number, y2: number, r: number): void {
|
|
3466
3558
|
this.path.arcTo(x1, y1, x2, y2, r);
|
|
3467
3559
|
}
|
|
3468
3560
|
|
|
3469
|
-
ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise = false) {
|
|
3561
|
+
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise = false): void {
|
|
3470
3562
|
this.path.ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise);
|
|
3471
3563
|
}
|
|
3472
3564
|
|
|
3473
|
-
roundRect(x, y, w, h, radii = 0) {
|
|
3565
|
+
roundRect(x: number, y: number, w: number, h: number, radii: number | number[] | DOMPointInit | DOMPointInit[] = 0): void {
|
|
3474
3566
|
this.path.roundRect(x, y, w, h, radii);
|
|
3475
3567
|
}
|
|
3476
3568
|
|
|
3477
|
-
closePath() {
|
|
3569
|
+
closePath(): void {
|
|
3478
3570
|
this.path.closePath();
|
|
3479
3571
|
}
|
|
3480
3572
|
|
|
3481
|
-
quadraticCurveTo(cpx, cpy, x, y) {
|
|
3573
|
+
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void {
|
|
3482
3574
|
this.path.quadraticCurveTo(cpx, cpy, x, y);
|
|
3483
3575
|
}
|
|
3484
3576
|
|
|
3485
|
-
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
|
|
3577
|
+
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void {
|
|
3486
3578
|
this.path.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
|
|
3487
3579
|
}
|
|
3488
3580
|
|
|
3489
|
-
addPath(path, transform = null) {
|
|
3581
|
+
addPath(path: GPUPath, transform: DOMMatrix | number[] | null = null): void {
|
|
3490
3582
|
this.path.addPath(path, transform);
|
|
3491
3583
|
}
|
|
3492
3584
|
|
|
3493
|
-
clip(path = this.path, fillRule = 'nonzero') {
|
|
3585
|
+
clip(path: GPUPath | CanvasFillRule = this.path, fillRule: CanvasFillRule = 'nonzero'): void {
|
|
3494
3586
|
if (typeof path == 'string') {
|
|
3495
3587
|
fillRule = path;
|
|
3496
3588
|
path = this.path;
|
|
@@ -3589,7 +3681,7 @@ export class WebGPURenderer {
|
|
|
3589
3681
|
this.enqueueShadowVertices(vertices);
|
|
3590
3682
|
}
|
|
3591
3683
|
|
|
3592
|
-
stroke(path = this.path) {
|
|
3684
|
+
stroke(path: GPUPath = this.path): void {
|
|
3593
3685
|
if (typeof path == 'string')
|
|
3594
3686
|
path = this.path;
|
|
3595
3687
|
|
|
@@ -3609,7 +3701,7 @@ export class WebGPURenderer {
|
|
|
3609
3701
|
let dashOffset = this.state.lineDashOffset * scale;
|
|
3610
3702
|
|
|
3611
3703
|
for (let sub of subpaths) {
|
|
3612
|
-
let transformed = sub.map(p => transformPoint(m, p[0], p[1]));
|
|
3704
|
+
let transformed = sub.map(p => transformPoint(m, p[0], p[1])) as GPUPathSubpath;
|
|
3613
3705
|
transformed.closed = !!sub.closed && dash.length == 0;
|
|
3614
3706
|
let segments = dashSegments(transformed, dash, dashOffset);
|
|
3615
3707
|
|
|
@@ -3621,7 +3713,7 @@ export class WebGPURenderer {
|
|
|
3621
3713
|
this.addGeometry(vertices);
|
|
3622
3714
|
}
|
|
3623
3715
|
|
|
3624
|
-
fill(path = this.path, fillRule = 'nonzero', kind = 'draw') {
|
|
3716
|
+
fill(path: GPUPath | CanvasFillRule = this.path, fillRule: CanvasFillRule = 'nonzero', kind = 'draw'): void {
|
|
3625
3717
|
if (typeof path == 'string') {
|
|
3626
3718
|
fillRule = path;
|
|
3627
3719
|
path = this.path;
|
|
@@ -3714,7 +3806,7 @@ export class WebGPURenderer {
|
|
|
3714
3806
|
}
|
|
3715
3807
|
}
|
|
3716
3808
|
|
|
3717
|
-
createImageData(width, height) {
|
|
3809
|
+
createImageData(width: any, height?: any): any {
|
|
3718
3810
|
if (typeof width == 'object' && width != null) {
|
|
3719
3811
|
height = width.height;
|
|
3720
3812
|
width = width.width;
|
|
@@ -3723,7 +3815,7 @@ export class WebGPURenderer {
|
|
|
3723
3815
|
return createImageDataObject(width, height);
|
|
3724
3816
|
}
|
|
3725
3817
|
|
|
3726
|
-
putImageData(imageData, dx, dy, dirtyX = 0, dirtyY = 0, dirtyWidth = imageData?.width || 0, dirtyHeight = imageData?.height || 0) {
|
|
3818
|
+
putImageData(imageData: ImageData, dx: number, dy: number, dirtyX = 0, dirtyY = 0, dirtyWidth = imageData?.width || 0, dirtyHeight = imageData?.height || 0): void {
|
|
3727
3819
|
if (!isImageDataLike(imageData))
|
|
3728
3820
|
return;
|
|
3729
3821
|
|
|
@@ -3749,7 +3841,7 @@ export class WebGPURenderer {
|
|
|
3749
3841
|
this.state.globalAlpha = prevAlpha;
|
|
3750
3842
|
}
|
|
3751
3843
|
|
|
3752
|
-
drawImage(source, ...args) {
|
|
3844
|
+
drawImage(source: CanvasImageSource | ImageBitmap, ...args: number[]): void {
|
|
3753
3845
|
let sw0 = sourceWidth(source);
|
|
3754
3846
|
let sh0 = sourceHeight(source);
|
|
3755
3847
|
if (sw0 <= 0 || sh0 <= 0)
|
|
@@ -3844,13 +3936,13 @@ export class WebGPURenderer {
|
|
|
3844
3936
|
});
|
|
3845
3937
|
}
|
|
3846
3938
|
|
|
3847
|
-
createPattern(source, repetition = 'repeat') {
|
|
3939
|
+
createPattern(source: CanvasImageSource | ImageBitmap, repetition = 'repeat'): CanvasPatternLike | null {
|
|
3848
3940
|
if (source == null)
|
|
3849
3941
|
return null;
|
|
3850
3942
|
return makePattern(source, repetition);
|
|
3851
3943
|
}
|
|
3852
3944
|
|
|
3853
|
-
fillRect(x, y, w, h) {
|
|
3945
|
+
fillRect(x: number, y: number, w: number, h: number): void {
|
|
3854
3946
|
if (isPatternPaint(this.state.fillStyle)) {
|
|
3855
3947
|
let path = new GPUPath();
|
|
3856
3948
|
path.rect(x, y, w, h);
|
|
@@ -3870,7 +3962,7 @@ export class WebGPURenderer {
|
|
|
3870
3962
|
this.addGeometry(vertices);
|
|
3871
3963
|
}
|
|
3872
3964
|
|
|
3873
|
-
strokeRect(x, y, w, h) {
|
|
3965
|
+
strokeRect(x: number, y: number, w: number, h: number): void {
|
|
3874
3966
|
let path = new GPUPath();
|
|
3875
3967
|
path.rect(x, y, w, h);
|
|
3876
3968
|
this.stroke(path);
|
|
@@ -3895,7 +3987,7 @@ export class WebGPURenderer {
|
|
|
3895
3987
|
this.commands.push({vertices, kind, composite});
|
|
3896
3988
|
}
|
|
3897
3989
|
|
|
3898
|
-
isPointInPath(path, x, y, fillRule = 'nonzero') {
|
|
3990
|
+
isPointInPath(path: any, x: any, y?: any, fillRule: any = 'nonzero'): boolean {
|
|
3899
3991
|
if (typeof path == 'number') {
|
|
3900
3992
|
fillRule = y || 'nonzero';
|
|
3901
3993
|
y = x;
|
|
@@ -3917,7 +4009,7 @@ export class WebGPURenderer {
|
|
|
3917
4009
|
return fillContainsPoint(point, polygons, fillRule);
|
|
3918
4010
|
}
|
|
3919
4011
|
|
|
3920
|
-
isPointInStroke(path, x, y) {
|
|
4012
|
+
isPointInStroke(path: GPUPath, x: number, y: number): boolean {
|
|
3921
4013
|
if (typeof path == 'number') {
|
|
3922
4014
|
y = x;
|
|
3923
4015
|
x = path;
|
|
@@ -3954,11 +4046,11 @@ export class WebGPURenderer {
|
|
|
3954
4046
|
return false;
|
|
3955
4047
|
}
|
|
3956
4048
|
|
|
3957
|
-
strokeText(text, x, y, maxWidth) {
|
|
4049
|
+
strokeText(text: string, x: number, y: number, maxWidth?: number): void {
|
|
3958
4050
|
this.drawText(text, x, y, true, maxWidth);
|
|
3959
4051
|
}
|
|
3960
4052
|
|
|
3961
|
-
fillText(text, x, y, maxWidth) {
|
|
4053
|
+
fillText(text: string, x: number, y: number, maxWidth?: number): void {
|
|
3962
4054
|
this.drawText(text, x, y, false, maxWidth);
|
|
3963
4055
|
}
|
|
3964
4056
|
|
|
@@ -4047,7 +4139,7 @@ export class WebGPURenderer {
|
|
|
4047
4139
|
return {x, y};
|
|
4048
4140
|
}
|
|
4049
4141
|
|
|
4050
|
-
measureText(text) {
|
|
4142
|
+
measureText(text: string): any {
|
|
4051
4143
|
let key = [this.state.font, this.state.fontKerning, this.state.fontStretch, this.state.fontVariantCaps, this.state.letterSpacing, this.state.textRendering, this.state.wordSpacing, text].join('\n');
|
|
4052
4144
|
let width = this.textMeasureCache.get(key);
|
|
4053
4145
|
|
|
@@ -4078,7 +4170,7 @@ export class WebGPURenderer {
|
|
|
4078
4170
|
};
|
|
4079
4171
|
}
|
|
4080
4172
|
|
|
4081
|
-
destroy() {
|
|
4173
|
+
destroy(): void {
|
|
4082
4174
|
this.disposed = true;
|
|
4083
4175
|
this.initEpoch++;
|
|
4084
4176
|
this.ready = false;
|
|
@@ -4119,20 +4211,20 @@ export class WebGPURenderer {
|
|
|
4119
4211
|
return color;
|
|
4120
4212
|
}
|
|
4121
4213
|
|
|
4122
|
-
createLinearGradient(x0, y0, x1, y1) {
|
|
4214
|
+
createLinearGradient(x0: number, y0: number, x1: number, y1: number): CanvasGradientLike {
|
|
4123
4215
|
let a = transformPoint(this.state.transform, x0, y0);
|
|
4124
4216
|
let b = transformPoint(this.state.transform, x1, y1);
|
|
4125
4217
|
return makeLinearGradient(a[0], a[1], b[0], b[1]);
|
|
4126
4218
|
}
|
|
4127
4219
|
|
|
4128
|
-
createRadialGradient(x0, y0, r0, x1, y1, r1) {
|
|
4220
|
+
createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): CanvasGradientLike {
|
|
4129
4221
|
let a = transformPoint(this.state.transform, x0, y0);
|
|
4130
4222
|
let b = transformPoint(this.state.transform, x1, y1);
|
|
4131
4223
|
let scale = transformScale(this.state.transform);
|
|
4132
4224
|
return makeRadialGradient(a[0], a[1], r0 * scale, b[0], b[1], r1 * scale);
|
|
4133
4225
|
}
|
|
4134
4226
|
|
|
4135
|
-
createConicGradient(startAngle, x, y) {
|
|
4227
|
+
createConicGradient(startAngle: number, x: number, y: number): CanvasGradientLike {
|
|
4136
4228
|
let c = transformPoint(this.state.transform, x, y);
|
|
4137
4229
|
let stops = [];
|
|
4138
4230
|
return {
|
|
@@ -4233,7 +4325,7 @@ export class WebGPURenderer {
|
|
|
4233
4325
|
set filter(v) { this.state.filter = filterStyleValue(v); }
|
|
4234
4326
|
}
|
|
4235
4327
|
|
|
4236
|
-
export const WebGPURendererInternals = {
|
|
4328
|
+
export const WebGPURendererInternals: Record<string, unknown> = {
|
|
4237
4329
|
blendForComposite,
|
|
4238
4330
|
clampCompositeMode,
|
|
4239
4331
|
clipVertices,
|