Skip to content

Commit 767f1a1

Browse files
saschanazsandersn
authored andcommitted
expose OffscreenCanvas types (#677)
1 parent 128463a commit 767f1a1

4 files changed

Lines changed: 238 additions & 15 deletions

File tree

baselines/dom.generated.d.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,11 @@ interface IIRFilterOptions extends AudioNodeOptions {
542542
feedforward: number[];
543543
}
544544

545+
interface ImageEncodeOptions {
546+
quality?: number;
547+
type?: string;
548+
}
549+
545550
interface IntersectionObserverEntryInit {
546551
boundingClientRect: DOMRectInit;
547552
intersectionRatio: number;
@@ -6174,6 +6179,7 @@ interface HTMLCanvasElement extends HTMLElement {
61746179
* @param type The standard MIME type for the image format to return. If you do not specify this parameter, the default value is a PNG format image.
61756180
*/
61766181
toDataURL(type?: string, quality?: any): string;
6182+
transferControlToOffscreen(): OffscreenCanvas;
61776183
addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLCanvasElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
61786184
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
61796185
removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLCanvasElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
@@ -10976,6 +10982,62 @@ declare var OfflineAudioContext: {
1097610982
new(numberOfChannels: number, length: number, sampleRate: number): OfflineAudioContext;
1097710983
};
1097810984

10985+
interface OffscreenCanvas extends EventTarget {
10986+
/**
10987+
* These attributes return the dimensions of the OffscreenCanvas object's bitmap.
10988+
* They can be set, to replace the bitmap with a
10989+
* new, transparent black bitmap of the specified dimensions (effectively resizing
10990+
* it).
10991+
*/
10992+
height: number;
10993+
width: number;
10994+
/**
10995+
* Returns a promise that will fulfill with a new Blob object representing a file
10996+
* containing the image in the OffscreenCanvas object.
10997+
* The argument, if provided, is a dictionary that controls the encoding options of the image
10998+
* file to be created. The type
10999+
* field specifies the file format and has a default value of "image/png"; that type
11000+
* is also used if the requested type isn't supported. If the image format supports variable
11001+
* quality (such as "image/jpeg"), then the quality field is a number in the range 0.0
11002+
* to 1.0 inclusive indicating the desired quality level for the resulting image.
11003+
*/
11004+
convertToBlob(options?: ImageEncodeOptions): Promise<Blob>;
11005+
/**
11006+
* Returns an object that exposes an API for drawing on the OffscreenCanvas
11007+
* object. contextId specifies the desired API: "2d" or "webgl". options is handled by that
11008+
* API.
11009+
* This specification defines the "2d" context below,
11010+
* which is similar but distinct from the "2d"
11011+
* context that is created from a canvas element. There is also a specification that
11012+
* defines a "webgl" context. [WEBGL]
11013+
* Returns null if the canvas has already been initialized with another context type (e.g.,
11014+
* trying to get a "2d" context after getting a
11015+
* "webgl" context).
11016+
*/
11017+
getContext(contextId: OffscreenRenderingContextId, options?: any): OffscreenRenderingContext | null;
11018+
/**
11019+
* Returns a newly created ImageBitmap object with the image in the
11020+
* OffscreenCanvas object. The image in the OffscreenCanvas object is
11021+
* replaced with a new blank image.
11022+
*/
11023+
transferToImageBitmap(): ImageBitmap;
11024+
}
11025+
11026+
declare var OffscreenCanvas: {
11027+
prototype: OffscreenCanvas;
11028+
new(width: number, height: number): OffscreenCanvas;
11029+
};
11030+
11031+
interface OffscreenCanvasRenderingContext2D extends CanvasState, CanvasTransform, CanvasCompositing, CanvasImageSmoothing, CanvasFillStrokeStyles, CanvasShadowStyles, CanvasFilters, CanvasRect, CanvasDrawPath, CanvasText, CanvasDrawImage, CanvasImageData, CanvasPathDrawingStyles, CanvasTextDrawingStyles, CanvasPath {
11032+
readonly canvas: OffscreenCanvas;
11033+
commit(): void;
11034+
}
11035+
11036+
declare var OffscreenCanvasRenderingContext2D: {
11037+
prototype: OffscreenCanvasRenderingContext2D;
11038+
new(): OffscreenCanvasRenderingContext2D;
11039+
};
11040+
1097911041
/** The OscillatorNode interface represents a periodic waveform, such as a sine wave. It is an AudioScheduledSourceNode audio-processing module that causes a specified frequency of a given wave to be created—in effect, a constant tone. */
1098011042
interface OscillatorNode extends AudioScheduledSourceNode {
1098111043
readonly detune: AudioParam;
@@ -18482,7 +18544,8 @@ type RequestInfo = Request | string;
1848218544
type DOMHighResTimeStamp = number;
1848318545
type RenderingContext = CanvasRenderingContext2D | ImageBitmapRenderingContext | WebGLRenderingContext;
1848418546
type HTMLOrSVGImageElement = HTMLImageElement | SVGImageElement;
18485-
type CanvasImageSource = HTMLOrSVGImageElement | HTMLVideoElement | HTMLCanvasElement | ImageBitmap;
18547+
type CanvasImageSource = HTMLOrSVGImageElement | HTMLVideoElement | HTMLCanvasElement | ImageBitmap | OffscreenCanvas;
18548+
type OffscreenRenderingContext = OffscreenCanvasRenderingContext2D | WebGLRenderingContext;
1848618549
type MessageEventSource = WindowProxy | MessagePort | ServiceWorker;
1848718550
type HTMLOrSVGScriptElement = HTMLScriptElement | SVGScriptElement;
1848818551
type ImageBitmapSource = CanvasImageSource | Blob | ImageData;
@@ -18583,6 +18646,7 @@ type NavigationReason = "up" | "down" | "left" | "right";
1858318646
type NavigationType = "navigate" | "reload" | "back_forward" | "prerender";
1858418647
type NotificationDirection = "auto" | "ltr" | "rtl";
1858518648
type NotificationPermission = "default" | "denied" | "granted";
18649+
type OffscreenRenderingContextId = "2d" | "webgl";
1858618650
type OrientationLockType = "any" | "natural" | "landscape" | "portrait" | "portrait-primary" | "portrait-secondary" | "landscape-primary" | "landscape-secondary";
1858718651
type OrientationType = "portrait-primary" | "portrait-secondary" | "landscape-primary" | "landscape-secondary";
1858818652
type OscillatorType = "sine" | "square" | "sawtooth" | "triangle" | "custom";

baselines/webworker.generated.d.ts

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ interface IDBVersionChangeEventInit extends EventInit {
215215
oldVersion?: number;
216216
}
217217

218+
interface ImageEncodeOptions {
219+
quality?: number;
220+
type?: string;
221+
}
222+
218223
interface JsonWebKey {
219224
alg?: string;
220225
crv?: string;
@@ -647,6 +652,43 @@ declare var CacheStorage: {
647652
new(): CacheStorage;
648653
};
649654

655+
interface CanvasCompositing {
656+
globalAlpha: number;
657+
globalCompositeOperation: string;
658+
}
659+
660+
interface CanvasDrawImage {
661+
drawImage(image: CanvasImageSource, dx: number, dy: number): void;
662+
drawImage(image: CanvasImageSource, dx: number, dy: number, dw: number, dh: number): void;
663+
drawImage(image: CanvasImageSource, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void;
664+
}
665+
666+
interface CanvasDrawPath {
667+
beginPath(): void;
668+
clip(fillRule?: CanvasFillRule): void;
669+
clip(path: Path2D, fillRule?: CanvasFillRule): void;
670+
fill(fillRule?: CanvasFillRule): void;
671+
fill(path: Path2D, fillRule?: CanvasFillRule): void;
672+
isPointInPath(x: number, y: number, fillRule?: CanvasFillRule): boolean;
673+
isPointInPath(path: Path2D, x: number, y: number, fillRule?: CanvasFillRule): boolean;
674+
isPointInStroke(x: number, y: number): boolean;
675+
isPointInStroke(path: Path2D, x: number, y: number): boolean;
676+
stroke(): void;
677+
stroke(path: Path2D): void;
678+
}
679+
680+
interface CanvasFillStrokeStyles {
681+
fillStyle: string | CanvasGradient | CanvasPattern;
682+
strokeStyle: string | CanvasGradient | CanvasPattern;
683+
createLinearGradient(x0: number, y0: number, x1: number, y1: number): CanvasGradient;
684+
createPattern(image: CanvasImageSource, repetition: string): CanvasPattern | null;
685+
createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): CanvasGradient;
686+
}
687+
688+
interface CanvasFilters {
689+
filter: string;
690+
}
691+
650692
/** An opaque object describing a gradient. It is returned by the methods CanvasRenderingContext2D.createLinearGradient() or CanvasRenderingContext2D.createRadialGradient(). */
651693
interface CanvasGradient {
652694
/**
@@ -664,6 +706,19 @@ declare var CanvasGradient: {
664706
new(): CanvasGradient;
665707
};
666708

709+
interface CanvasImageData {
710+
createImageData(sw: number, sh: number): ImageData;
711+
createImageData(imagedata: ImageData): ImageData;
712+
getImageData(sx: number, sy: number, sw: number, sh: number): ImageData;
713+
putImageData(imagedata: ImageData, dx: number, dy: number): void;
714+
putImageData(imagedata: ImageData, dx: number, dy: number, dirtyX: number, dirtyY: number, dirtyWidth: number, dirtyHeight: number): void;
715+
}
716+
717+
interface CanvasImageSmoothing {
718+
imageSmoothingEnabled: boolean;
719+
imageSmoothingQuality: ImageSmoothingQuality;
720+
}
721+
667722
interface CanvasPath {
668723
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise?: boolean): void;
669724
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void;
@@ -676,6 +731,16 @@ interface CanvasPath {
676731
rect(x: number, y: number, w: number, h: number): void;
677732
}
678733

734+
interface CanvasPathDrawingStyles {
735+
lineCap: CanvasLineCap;
736+
lineDashOffset: number;
737+
lineJoin: CanvasLineJoin;
738+
lineWidth: number;
739+
miterLimit: number;
740+
getLineDash(): number[];
741+
setLineDash(segments: number[]): void;
742+
}
743+
679744
/** An opaque object describing a pattern, based on an image, a canvas, or a video, created by the CanvasRenderingContext2D.createPattern() method. */
680745
interface CanvasPattern {
681746
/**
@@ -690,6 +755,48 @@ declare var CanvasPattern: {
690755
new(): CanvasPattern;
691756
};
692757

758+
interface CanvasRect {
759+
clearRect(x: number, y: number, w: number, h: number): void;
760+
fillRect(x: number, y: number, w: number, h: number): void;
761+
strokeRect(x: number, y: number, w: number, h: number): void;
762+
}
763+
764+
interface CanvasShadowStyles {
765+
shadowBlur: number;
766+
shadowColor: string;
767+
shadowOffsetX: number;
768+
shadowOffsetY: number;
769+
}
770+
771+
interface CanvasState {
772+
restore(): void;
773+
save(): void;
774+
}
775+
776+
interface CanvasText {
777+
fillText(text: string, x: number, y: number, maxWidth?: number): void;
778+
measureText(text: string): TextMetrics;
779+
strokeText(text: string, x: number, y: number, maxWidth?: number): void;
780+
}
781+
782+
interface CanvasTextDrawingStyles {
783+
direction: CanvasDirection;
784+
font: string;
785+
textAlign: CanvasTextAlign;
786+
textBaseline: CanvasTextBaseline;
787+
}
788+
789+
interface CanvasTransform {
790+
getTransform(): DOMMatrix;
791+
resetTransform(): void;
792+
rotate(angle: number): void;
793+
scale(x: number, y: number): void;
794+
setTransform(a: number, b: number, c: number, d: number, e: number, f: number): void;
795+
setTransform(transform?: DOMMatrix2DInit): void;
796+
transform(a: number, b: number, c: number, d: number, e: number, f: number): void;
797+
translate(x: number, y: number): void;
798+
}
799+
693800
/** The Client interface represents an executable context such as a Worker, or a SharedWorker. Window clients are represented by the more-specific WindowClient. You can get Client/WindowClient objects from methods such as Clients.matchAll() and Clients.get(). */
694801
interface Client {
695802
readonly id: string;
@@ -2231,6 +2338,62 @@ interface OES_vertex_array_object {
22312338
readonly VERTEX_ARRAY_BINDING_OES: GLenum;
22322339
}
22332340

2341+
interface OffscreenCanvas extends EventTarget {
2342+
/**
2343+
* These attributes return the dimensions of the OffscreenCanvas object's bitmap.
2344+
* They can be set, to replace the bitmap with a
2345+
* new, transparent black bitmap of the specified dimensions (effectively resizing
2346+
* it).
2347+
*/
2348+
height: number;
2349+
width: number;
2350+
/**
2351+
* Returns a promise that will fulfill with a new Blob object representing a file
2352+
* containing the image in the OffscreenCanvas object.
2353+
* The argument, if provided, is a dictionary that controls the encoding options of the image
2354+
* file to be created. The type
2355+
* field specifies the file format and has a default value of "image/png"; that type
2356+
* is also used if the requested type isn't supported. If the image format supports variable
2357+
* quality (such as "image/jpeg"), then the quality field is a number in the range 0.0
2358+
* to 1.0 inclusive indicating the desired quality level for the resulting image.
2359+
*/
2360+
convertToBlob(options?: ImageEncodeOptions): Promise<Blob>;
2361+
/**
2362+
* Returns an object that exposes an API for drawing on the OffscreenCanvas
2363+
* object. contextId specifies the desired API: "2d" or "webgl". options is handled by that
2364+
* API.
2365+
* This specification defines the "2d" context below,
2366+
* which is similar but distinct from the "2d"
2367+
* context that is created from a canvas element. There is also a specification that
2368+
* defines a "webgl" context. [WEBGL]
2369+
* Returns null if the canvas has already been initialized with another context type (e.g.,
2370+
* trying to get a "2d" context after getting a
2371+
* "webgl" context).
2372+
*/
2373+
getContext(contextId: OffscreenRenderingContextId, options?: any): OffscreenRenderingContext | null;
2374+
/**
2375+
* Returns a newly created ImageBitmap object with the image in the
2376+
* OffscreenCanvas object. The image in the OffscreenCanvas object is
2377+
* replaced with a new blank image.
2378+
*/
2379+
transferToImageBitmap(): ImageBitmap;
2380+
}
2381+
2382+
declare var OffscreenCanvas: {
2383+
prototype: OffscreenCanvas;
2384+
new(width: number, height: number): OffscreenCanvas;
2385+
};
2386+
2387+
interface OffscreenCanvasRenderingContext2D extends CanvasState, CanvasTransform, CanvasCompositing, CanvasImageSmoothing, CanvasFillStrokeStyles, CanvasShadowStyles, CanvasFilters, CanvasRect, CanvasDrawPath, CanvasText, CanvasDrawImage, CanvasImageData, CanvasPathDrawingStyles, CanvasTextDrawingStyles, CanvasPath {
2388+
readonly canvas: OffscreenCanvas;
2389+
commit(): void;
2390+
}
2391+
2392+
declare var OffscreenCanvasRenderingContext2D: {
2393+
prototype: OffscreenCanvasRenderingContext2D;
2394+
new(): OffscreenCanvasRenderingContext2D;
2395+
};
2396+
22342397
/** Of the Canvas 2D API is used to declare a path that can then be used on a CanvasRenderingContext2D object. The path methods of the CanvasRenderingContext2D interface are also present on this interface, which gives you the convenience of being able to retain and replay your path whenever desired. */
22352398
interface Path2D extends CanvasPath {
22362399
addPath(path: Path2D, transform?: DOMMatrix2DInit): void;
@@ -4512,7 +4675,8 @@ type HeadersInit = Headers | string[][] | Record<string, string>;
45124675
type BodyInit = Blob | BufferSource | FormData | URLSearchParams | ReadableStream<Uint8Array> | string;
45134676
type RequestInfo = Request | string;
45144677
type DOMHighResTimeStamp = number;
4515-
type CanvasImageSource = ImageBitmap;
4678+
type CanvasImageSource = ImageBitmap | OffscreenCanvas;
4679+
type OffscreenRenderingContext = OffscreenCanvasRenderingContext2D | WebGLRenderingContext;
45164680
type MessageEventSource = MessagePort | ServiceWorker;
45174681
type ImageBitmapSource = CanvasImageSource | Blob | ImageData;
45184682
type TimerHandler = string | Function;
@@ -4542,16 +4706,24 @@ type FormDataEntryValue = File | string;
45424706
type IDBValidKey = number | string | Date | BufferSource | IDBArrayKey;
45434707
type Transferable = ArrayBuffer | MessagePort | ImageBitmap;
45444708
type BinaryType = "blob" | "arraybuffer";
4709+
type CanvasDirection = "ltr" | "rtl" | "inherit";
4710+
type CanvasFillRule = "nonzero" | "evenodd";
4711+
type CanvasLineCap = "butt" | "round" | "square";
4712+
type CanvasLineJoin = "round" | "bevel" | "miter";
4713+
type CanvasTextAlign = "start" | "end" | "left" | "right" | "center";
4714+
type CanvasTextBaseline = "top" | "hanging" | "middle" | "alphabetic" | "ideographic" | "bottom";
45454715
type ClientTypes = "window" | "worker" | "sharedworker" | "all";
45464716
type EndingType = "transparent" | "native";
45474717
type IDBCursorDirection = "next" | "nextunique" | "prev" | "prevunique";
45484718
type IDBRequestReadyState = "pending" | "done";
45494719
type IDBTransactionMode = "readonly" | "readwrite" | "versionchange";
4720+
type ImageSmoothingQuality = "low" | "medium" | "high";
45504721
type KeyFormat = "raw" | "spki" | "pkcs8" | "jwk";
45514722
type KeyType = "public" | "private" | "secret";
45524723
type KeyUsage = "encrypt" | "decrypt" | "sign" | "verify" | "deriveKey" | "deriveBits" | "wrapKey" | "unwrapKey";
45534724
type NotificationDirection = "auto" | "ltr" | "rtl";
45544725
type NotificationPermission = "default" | "denied" | "granted";
4726+
type OffscreenRenderingContextId = "2d" | "webgl";
45554727
type PermissionName = "geolocation" | "notifications" | "push" | "midi" | "camera" | "microphone" | "speaker" | "device-info" | "background-sync" | "bluetooth" | "persistent-storage" | "ambient-light-sensor" | "accelerometer" | "gyroscope" | "magnetometer" | "clipboard";
45564728
type PermissionState = "granted" | "denied" | "prompt";
45574729
type PushEncryptionKeyName = "p256dh" | "auth";

inputfiles/overridingTypes.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -887,12 +887,6 @@
887887
}
888888
}
889889
},
890-
"OffscreenCanvas": {
891-
"exposed": ""
892-
},
893-
"OffscreenCanvasRenderingContext2D": {
894-
"exposed": ""
895-
},
896890
"BeforeUnloadEvent": {
897891
"name": "BeforeUnloadEvent",
898892
"properties": {

inputfiles/removedTypes.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@
103103
}
104104
}
105105
},
106-
"HTMLCanvasElement": {
107-
"methods": {
108-
"method": {
109-
"transferControlToOffscreen": null
110-
}
111-
}
112-
},
113106
"HTMLHRElement": {
114107
"implements": null
115108
},

0 commit comments

Comments
 (0)