deno.land / x / skia_canvas@0.5.8 / src / context2d.ts
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253import { Canvas } from "./canvas.ts";import { DOMMatrix } from "./dommatrix.ts";import ffi, { cstr } from "./ffi.ts";import { FilterType, parseFilterString } from "./filter.ts";import { CanvasGradient } from "./gradient.ts";import { Image, ImageData } from "./image.ts";import { parseFont } from "./parse_font.ts";import { Path2D, type RoundRectRadii, roundRectRadiiArg } from "./path2d.ts";import { CanvasPattern, type CanvasPatternImage, type CanvasPatternRepeat,} from "./pattern.ts";
const { sk_context_clear_rect, sk_context_set_fill_style, sk_context_save, sk_context_restore, sk_context_fill_rect, sk_context_set_stroke_style, sk_context_stroke_rect, sk_context_begin_path, sk_context_close_path, sk_context_line_to, sk_context_move_to, sk_context_fill, sk_context_stroke, sk_context_get_global_alpha, sk_context_get_line_width, sk_context_get_miter_limit, sk_context_set_global_alpha, sk_context_set_line_width, sk_context_set_miter_limit, sk_context_set_shadow_color, sk_context_rect, sk_context_clip, sk_context_arc, sk_context_arc_to, sk_context_bezier_curve_to, sk_context_ellipse, sk_context_quadratic_curve_to, sk_context_reset_transform, sk_context_rotate, sk_context_scale, sk_context_set_transform, sk_context_transform, sk_context_translate, sk_context_get_line_cap, sk_context_get_line_dash_offset, sk_context_get_shadow_blur, sk_context_get_shadow_offset_x, sk_context_get_shadow_offset_y, sk_context_get_text_align, sk_context_get_text_baseline, sk_context_get_text_direction, sk_context_set_font, sk_context_set_line_cap, sk_context_set_line_dash_offset, sk_context_set_shadow_blur, sk_context_set_shadow_offset_x, sk_context_set_shadow_offset_y, sk_context_set_text_align, sk_context_set_text_baseline, sk_context_set_text_direction, sk_context_draw_image, sk_context_text, sk_context_get_line_join, sk_context_set_line_join, sk_context_set_line_dash, sk_context_is_point_in_path, sk_context_is_point_in_stroke, sk_context_get_global_composite_operation, sk_context_set_global_composite_operation, sk_context_get_image_smoothing_enabled, sk_context_set_image_smoothing_enabled, sk_context_round_rect, sk_context_get_transform, sk_context_get_image_smoothing_quality, sk_context_set_image_smoothing_quality, sk_context_put_image_data_dirty, sk_context_put_image_data, sk_gradient_create_conic, sk_gradient_create_linear, sk_gradient_create_radial, sk_context_set_fill_style_gradient, sk_context_set_stroke_style_gradient, sk_context_set_fill_style_pattern, sk_context_set_stroke_style_pattern, sk_context_filter_contrast, sk_context_filter_invert, sk_context_filter_brightness, sk_context_filter_blur, sk_context_filter_drop_shadow, sk_context_filter_grayscale, sk_context_filter_hue_rotate, sk_context_filter_opacity, sk_context_filter_reset, sk_context_filter_saturated, sk_context_filter_sepia, sk_context_get_word_spacing, sk_context_get_letter_spacing, sk_context_set_word_spacing, sk_context_set_letter_spacing, sk_context_set_font_stretch, sk_context_set_font_variant_caps,} = ffi;
export type FillRule = "nonzero" | "evenodd";
enum CLineCap { butt = 0, round = 1, square = 2,}
enum CLineJoin { miter = 0, round = 1, bevel = 2,}
enum CTextAlign { left = 0, center = 1, right = 2, start = 3, end = 4,}
enum CTextDirection { inherit = 0, ltr = 1, rtl = 2,}
enum CTextBaseline { top, hanging, middle, alphabetic, ideographic, bottom,}
enum CImageSmoothingQuality { low = 1, medium = 2, high = 3,}
// deno-fmt-ignoreconst CGlobalCompositeOperation = { ["source-over"]: 0, ["source-in"]: 1, ["source-out"]: 2, ["source-atop"]: 3, ["destination-over"]: 4, ["destination-in"]: 5, ["destination-out"]: 6, ["destination-atop"]: 7, ["xor"]: 8, ["copy"]: 10, // modulate (TODO: is this right) ["screen"]: 11, ["overlay"]: 12, ["darken"]: 13, ["lighten"]: 14, ["color-dodge"]: 15, ["color-burn"]: 16, ["hard-light"]: 17, ["soft-light"]: 18, ["difference"]: 19, ["exclusion"]: 20, ["multiply"]: 21, ["hue"]:22, ["saturation"] : 23, ["color"] : 24, ["luminosity"] : 25,} as const;
export type TextAlign = keyof typeof CTextAlign;export type TextDirection = keyof typeof CTextDirection;export type TextBaseline = keyof typeof CTextBaseline;export type LineCap = keyof typeof CLineCap;export type LineJoin = keyof typeof CLineJoin;export type CanvasImageSource = Canvas | Image;export type GlobalCompositeOperation = keyof typeof CGlobalCompositeOperation;export type ImageSmoothingQuality = keyof typeof CImageSmoothingQuality;
const METRICS = new Float32Array(10);const METRICS_PTR = Deno.UnsafePointer.of(METRICS);
export interface TextMetrics { width: number; actualBoundingBoxLeft: number; actualBoundingBoxRight: number; actualBoundingBoxAscent: number; actualBoundingBoxDescent: number; fontBoundingBoxAscent: number; fontBoundingBoxDescent: number; alphabeticBaseline: number; emHeightAscent: number; emHeightDescent: number; hangingBaseline: number; ideographicBaseline: number;}
export type Style = string | CanvasGradient | CanvasPattern;
const CFontStretch = { ["ultra-condensed"]: 1, ["50%"]: 1, ["extra-condensed"]: 2, ["62.5%"]: 2, ["condensed"]: 3, ["75%"]: 3, ["semi-condensed"]: 4, ["87.5%"]: 4, ["normal"]: 5, ["100%"]: 5, ["semi-expanded"]: 6, ["112.5%"]: 6, ["expanded"]: 7, ["125%"]: 7, ["extra-expanded"]: 8, ["150%"]: 8, ["ultra-expanded"]: 9, ["200%"]: 9,} as const;
const CFontVariantCaps = { ["normal"]: 0, ["small-caps"]: 1,} as const;
export type FontStretch = keyof typeof CFontStretch;export type FontVariantCaps = keyof typeof CFontVariantCaps;export type FontKerning = "auto" | "none" | "normal";export type TextRendering = | "auto" | "geometricPrecision" | "optimizeLegibility" | "optimizeSpeed";
const _canvas = Symbol("[[canvas]]");const _ptr = Symbol("[[ptr]]");const _fillStyle = Symbol("[[fillStyle]]");const _strokeStyle = Symbol("[[strokeStyle]]");const _shadowColor = Symbol("[[shadowColor]]");const _font = Symbol("[[font]]");const _fontStretch = Symbol("[[fontStretch]]");const _fontVariantCaps = Symbol("[[fontVariantCaps]]");const _lineDash = Symbol("[[lineDash]]");const _filter = Symbol("[[filter]]");
/** * @link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D */export class CanvasRenderingContext2D { /// Internal State
[_canvas]: Canvas; [_ptr]: Deno.PointerValue;
[_fillStyle]: Style = "black"; [_strokeStyle]: Style = "black"; [_shadowColor] = "black"; [_font] = "10px sans-serif"; [_fontStretch]: FontStretch = "normal"; [_fontVariantCaps]: FontVariantCaps = "normal"; [_lineDash]: number[] = []; [_filter] = "none";
/// For FFI interface get _unsafePointer(): Deno.PointerValue { return this[_ptr]; }
set _unsafePointer(ptr: Deno.PointerValue) { this[_ptr] = ptr; this[_fillStyle] = "black"; this[_strokeStyle] = "black"; this[_shadowColor] = "black"; this[_font] = "10px sans-serif"; this[_fontStretch] = "normal"; this[_fontVariantCaps] = "normal"; this[_lineDash] = []; this[_filter] = "none"; }
constructor(canvas: Canvas, ptr: Deno.PointerValue) { this[_canvas] = canvas; this[_ptr] = ptr; if (this[_ptr] === null) { throw new Error("Failed to create context"); } }
/// Drawing rectangles
clearRect(x: number, y: number, width: number, height: number) { sk_context_clear_rect(this[_ptr], x, y, width, height); }
fillRect(x: number, y: number, width: number, height: number) { sk_context_fill_rect(this[_ptr], x, y, width, height); }
strokeRect(x: number, y: number, width: number, height: number) { sk_context_stroke_rect(this[_ptr], x, y, width, height); }
/// Drawing text
fillText(text: string, x: number, y: number, maxWidth?: number) { const encoded = new TextEncoder().encode(text); if ( !sk_context_text( this[_ptr], encoded, encoded.byteLength, x, y, maxWidth ?? 100_000, 1, null, ) ) { throw new Error("failed to fill text"); } }
strokeText(text: string, x: number, y: number, maxWidth?: number) { const encoded = new TextEncoder().encode(text); if ( !sk_context_text( this[_ptr], encoded, encoded.byteLength, x, y, maxWidth ?? 100_000, 0, null, ) ) { throw new Error("failed to stroke text"); } }
measureText(text: string): TextMetrics { if (text.length === 0) { return { width: 0, actualBoundingBoxLeft: 0, actualBoundingBoxRight: 0, actualBoundingBoxAscent: 0, actualBoundingBoxDescent: 0, fontBoundingBoxAscent: 0, fontBoundingBoxDescent: 0, alphabeticBaseline: 0, emHeightAscent: 0, emHeightDescent: 0, ideographicBaseline: 0, hangingBaseline: 0, }; } const encoded = new TextEncoder().encode(text); if ( !sk_context_text( this[_ptr], encoded, encoded.byteLength, 0, 0, 100_000, 1, METRICS_PTR, ) ) { throw new Error("failed to measure text"); } return { width: METRICS[4], actualBoundingBoxLeft: METRICS[2], actualBoundingBoxRight: METRICS[3], actualBoundingBoxAscent: METRICS[0], actualBoundingBoxDescent: METRICS[1], fontBoundingBoxAscent: METRICS[5], fontBoundingBoxDescent: METRICS[6], alphabeticBaseline: METRICS[7], emHeightAscent: METRICS[5], emHeightDescent: METRICS[6], ideographicBaseline: METRICS[8], hangingBaseline: METRICS[9], }; }
/// Line styles
get lineWidth(): number { return sk_context_get_line_width(this[_ptr]); }
set lineWidth(value: number) { sk_context_set_line_width(this[_ptr], value); }
get lineCap(): LineCap { return CLineCap[sk_context_get_line_cap(this[_ptr])] as LineCap; }
set lineCap(value: LineCap) { sk_context_set_line_cap(this[_ptr], CLineCap[value]); }
get lineJoin(): LineJoin { return CLineJoin[sk_context_get_line_join(this[_ptr])] as LineJoin; }
set lineJoin(value: LineJoin) { sk_context_set_line_join(this[_ptr], CLineJoin[value]); }
get miterLimit(): number { return sk_context_get_miter_limit(this[_ptr]); }
set miterLimit(value: number) { sk_context_set_miter_limit(this[_ptr], value); }
getLineDash(): number[] { return this[_lineDash]; }
setLineDash(value: number[]) { this[_lineDash] = value; sk_context_set_line_dash(this[_ptr], new Float32Array(value), value.length); }
get lineDashOffset(): number { return sk_context_get_line_dash_offset(this[_ptr]); }
set lineDashOffset(value: number) { sk_context_set_line_dash_offset(this[_ptr], Number(value)); }
/// Text styles
set font(value: string) { const font = parseFont(value); if (font) { if ( sk_context_set_font( this[_ptr], font.size, cstr(font.family), font.weight, font.style, font.variant, font.stretch, ) ) { this[_font] = value; } } else { throw new Error("Invalid font"); } }
get font(): string { return this[_font]; }
get textAlign(): TextAlign { return CTextAlign[sk_context_get_text_align(this[_ptr])] as TextAlign; }
set textAlign(value: TextAlign) { sk_context_set_text_align(this[_ptr], CTextAlign[value]); }
get textBaseline(): TextBaseline { return CTextBaseline[ sk_context_get_text_baseline(this[_ptr]) ] as TextBaseline; }
set textBaseline(value: TextBaseline) { sk_context_set_text_baseline(this[_ptr], CTextBaseline[value]); }
get direction(): TextDirection { return CTextDirection[ sk_context_get_text_direction(this[_ptr]) ] as TextDirection; }
set direction(value: TextDirection) { sk_context_set_text_direction(this[_ptr], CTextDirection[value]); }
get letterSpacing(): string { return sk_context_get_letter_spacing(this[_ptr]) + "px"; }
set letterSpacing(value: number | string) { sk_context_set_letter_spacing( this[_ptr], typeof value === "number" ? value : parseFloat(value), ); }
get wordSpacing(): string { return sk_context_get_word_spacing(this[_ptr]) + "px"; }
set wordSpacing(value: number | string) { sk_context_set_word_spacing( this[_ptr], typeof value === "number" ? value : parseFloat(value), ); }
get fontKerning(): FontKerning { return "auto"; }
set fontKerning(value: FontKerning) { if (value !== "auto") { throw new Error("fontKerning only supports 'auto'"); } }
get fontStretch(): FontStretch { return this[_fontStretch]; }
set fontStretch(value: FontStretch) { const c = CFontStretch[value]; if (typeof c !== "number") { throw new Error("invalid fontStretch"); } this[_fontStretch] = value; sk_context_set_font_stretch(this[_ptr], c); }
get fontVariantCaps(): FontVariantCaps { return this[_fontVariantCaps]; }
set fontVariant(value: FontVariantCaps) { const c = CFontVariantCaps[value]; if (typeof c !== "number") { throw new Error("invalid fontVariantCaps"); } this[_fontVariantCaps] = value; sk_context_set_font_variant_caps(this[_ptr], c); }
get textRendering(): TextRendering { return "auto"; }
set textRendering(value: TextRendering) { if (value !== "auto") { throw new Error("textRendering only supports 'auto'"); } }
/// Fill and stroke styles
get fillStyle(): Style { return this[_fillStyle]; }
set fillStyle(value: Style) { if (typeof value === "string") { if (sk_context_set_fill_style(this[_ptr], cstr(value))) { this[_fillStyle] = value; } } else if ( typeof value === "object" && value !== null && value instanceof CanvasGradient ) { sk_context_set_fill_style_gradient(this[_ptr], value._unsafePointer); this[_fillStyle] = value; } else if ( typeof value === "object" && value !== null && value instanceof CanvasPattern ) { sk_context_set_fill_style_pattern(this[_ptr], value._unsafePointer); this[_fillStyle] = value; } else { throw new Error("Invalid fill style"); } }
get strokeStyle(): Style { return this[_strokeStyle]; }
set strokeStyle(value: Style) { if (typeof value === "string") { if (sk_context_set_stroke_style(this[_ptr], cstr(value))) { this[_strokeStyle] = value; } } else if ( typeof value === "object" && value !== null && value instanceof CanvasGradient ) { sk_context_set_stroke_style_gradient(this[_ptr], value._unsafePointer); this[_strokeStyle] = value; } else if ( typeof value === "object" && value !== null && value instanceof CanvasPattern ) { sk_context_set_stroke_style_pattern(this[_ptr], value._unsafePointer); this[_strokeStyle] = value; } else { throw new Error("Invalid stroke style"); } }
/// Gradients and patterns
createConicGradient( r: number, x: number, y: number, ): CanvasGradient { return new CanvasGradient(sk_gradient_create_conic(x, y, r)); }
createLinearGradient( x0: number, y0: number, x1: number, y1: number, ): CanvasGradient { return new CanvasGradient(sk_gradient_create_linear(x0, y0, x1, y1)); }
createRadialGradient( x0: number, y0: number, r0: number, x1: number, y1: number, r1: number, ): CanvasGradient { return new CanvasGradient( sk_gradient_create_radial(x0, y0, r0, x1, y1, r1), ); }
createPattern( image: CanvasPatternImage, repetition?: CanvasPatternRepeat, ): CanvasPattern { return new CanvasPattern(image, repetition || "repeat"); }
/// Shadows
get shadowBlur(): number { return sk_context_get_shadow_blur(this[_ptr]); }
set shadowBlur(value: number) { sk_context_set_shadow_blur(this[_ptr], value); }
get shadowColor(): string { return this[_shadowColor]; }
set shadowColor(value: string) { if (sk_context_set_shadow_color(this[_ptr], cstr(value))) { this[_shadowColor] = value; } }
get shadowOffsetX(): number { return sk_context_get_shadow_offset_x(this[_ptr]); }
set shadowOffsetX(value: number) { sk_context_set_shadow_offset_x(this[_ptr], value); }
get shadowOffsetY(): number { return sk_context_get_shadow_offset_y(this[_ptr]); }
set shadowOffsetY(value: number) { sk_context_set_shadow_offset_y(this[_ptr], value); }
/// Paths
beginPath() { sk_context_begin_path(this[_ptr]); }
closePath() { sk_context_close_path(this[_ptr]); }
moveTo(x: number, y: number) { sk_context_move_to(this[_ptr], x, y); }
lineTo(x: number, y: number) { sk_context_line_to(this[_ptr], x, y); }
bezierCurveTo( cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number, ) { sk_context_bezier_curve_to(this[_ptr], cp1x, cp1y, cp2x, cp2y, x, y); }
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number) { sk_context_quadratic_curve_to(this[_ptr], cpx, cpy, x, y); }
arc( x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise: boolean, ) { sk_context_arc( this[_ptr], x, y, radius, startAngle, endAngle, anticlockwise ? 1 : 0, ); }
arcTo( x1: number, y1: number, x2: number, y2: number, radius: number, ) { sk_context_arc_to(this[_ptr], x1, y1, x2, y2, radius); }
ellipse( x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise: boolean, ) { sk_context_ellipse( this[_ptr], x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise ? 1 : 0, ); }
rect(x: number, y: number, width: number, height: number) { sk_context_rect(this[_ptr], x, y, width, height); }
roundRect( x: number, y: number, width: number, height: number, r: RoundRectRadii, ) { sk_context_round_rect( this[_ptr], x, y, width, height, ...roundRectRadiiArg(r), ); }
/// Drawing paths
fill(): void; fill(path: Path2D): void; fill(rule: FillRule): void; fill(path: Path2D, rule: FillRule): void; fill(path?: Path2D | FillRule, rule?: FillRule) { const pathptr = typeof path === "object" && path !== null && path instanceof Path2D ? path._unsafePointer : null; const irule = (typeof path === "string" ? path : rule) ?? "nonzero"; sk_context_fill(this[_ptr], pathptr, irule === "evenodd" ? 1 : 0); }
stroke(path?: Path2D) { sk_context_stroke( this[_ptr], path ? path._unsafePointer : null, ); }
drawFocusIfNeeded() { throw new Error("Context.drawFocusIfNeeded() not implemented"); }
scrollPathIntoView() { throw new Error("Context.scrollPathIntoView() not implemented"); }
clip(): void; clip(path: Path2D): void; clip(fillRule: FillRule): void; clip(path: Path2D, fillRule: FillRule): void; clip( path?: Path2D | FillRule, fillRule?: FillRule, ) { const pathptr = typeof path === "object" && path !== null ? path._unsafePointer : null; const fillRuleStr = typeof path === "string" ? path : fillRule; const ifillRule = fillRuleStr === "evenodd" ? 1 : 0; sk_context_clip(this[_ptr], pathptr, ifillRule); }
isPointInPath( x: number, y: number, fillRule?: FillRule, ): boolean; isPointInPath( path: Path2D, x: number, y: number, fillRule?: FillRule, ): boolean; isPointInPath( path: Path2D | number, x: number, y?: number | FillRule, fillRule?: FillRule, ): boolean { const pathptr = typeof path === "object" && path !== null ? path._unsafePointer : null; const ifillRule = (typeof y === "string" ? y : fillRule) === "evenodd" ? 1 : 0; return sk_context_is_point_in_path( this[_ptr], typeof path === "number" ? path : x, typeof path === "number" ? x : y as number, pathptr, ifillRule, ) === 1; }
isPointInStroke( x: number, y: number, ): boolean; isPointInStroke( path: Path2D, x: number, y: number, ): boolean; isPointInStroke( path: Path2D | number, x: number, y?: number, ): boolean { const pathptr = typeof path === "object" && path !== null ? path._unsafePointer : null; return sk_context_is_point_in_stroke( this[_ptr], typeof path === "number" ? path : x, typeof path === "number" ? x : y as number, pathptr, ) === 1; }
/// Transformations
getTransform(): DOMMatrix { const f32 = new Float32Array(6); sk_context_get_transform(this[_ptr], f32); return new DOMMatrix(f32[0], f32[1], f32[2], f32[3], f32[4], f32[5]); }
rotate(angle: number) { sk_context_rotate(this[_ptr], angle); }
scale(x: number, y: number) { sk_context_scale(this[_ptr], x, y); }
translate(x: number, y: number) { sk_context_translate(this[_ptr], x, y); }
transform( a: number, b: number, c: number, d: number, e: number, f: number, ) { sk_context_transform(this[_ptr], a, b, c, d, e, f); }
setTransform( a: number, b: number, c: number, d: number, e: number, f: number, ): void; setTransform(matrix: DOMMatrix): void; setTransform( a: number | DOMMatrix, b?: number, c?: number, d?: number, e?: number, f?: number, ) { if (typeof a === "number") { sk_context_set_transform(this[_ptr], a, b!, c!, d!, e!, f!); } else { sk_context_set_transform(this[_ptr], a.a, a.b, a.c, a.d, a.e, a.f); } }
resetTransform() { sk_context_reset_transform(this[_ptr]); }
/// Compositing
get globalAlpha(): number { return sk_context_get_global_alpha(this[_ptr]); }
set globalAlpha(value: number) { sk_context_set_global_alpha(this[_ptr], value); }
get globalCompositeOperation(): GlobalCompositeOperation { const op = sk_context_get_global_composite_operation(this[_ptr]); return Object.entries(CGlobalCompositeOperation).find((e) => e[1] === op )![0] as GlobalCompositeOperation; }
set globalCompositeOperation(value: GlobalCompositeOperation) { sk_context_set_global_composite_operation( this[_ptr], CGlobalCompositeOperation[value], ); }
/// Drawing images
drawImage(image: CanvasImageSource, dx: number, dy: number): void; drawImage( image: CanvasImageSource, dx: number, dy: number, dw: number, dh: number, ): void; drawImage( image: CanvasImageSource, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number, ): void; drawImage( image: CanvasImageSource, adx: number, ady: number, adw?: number, adh?: number, asx?: number, asy?: number, asw?: number, ash?: number, ) { if (image instanceof Image && image._unsafePointer === null) { return; } const dx = asx ?? adx; const dy = asy ?? ady; const dw = asw ?? adw ?? image.width; const dh = ash ?? adh ?? image.height; const sx = asx === undefined ? 0 : adx; const sy = asy === undefined ? 0 : ady; const sw = asw === undefined ? image.width : adw ?? image.width; const sh = ash === undefined ? image.height : adh ?? image.height; sk_context_draw_image( this[_ptr], image instanceof Canvas ? image._unsafePointer : null, image instanceof Image ? image._unsafePointer : null, dx, dy, dw, dh, sx, sy, sw, sh, ); }
/// Pixel manipulation
createImageData(sw: number, sh: number): ImageData; createImageData(imagedata: ImageData): ImageData; createImageData(sw: number | ImageData, sh?: number) { if (typeof sw === "number") { return new ImageData(sw, sh!); } else { return new ImageData(sw.width, sw.height); } }
getImageData(sx: number, sy: number, sw: number, sh: number): ImageData { if (!(this[_canvas] instanceof Canvas)) { throw new Error("getImageData is only supported on Canvas"); } const data = new Uint8Array(sw * sh * 4); this[_canvas].readPixels(sx, sy, sw, sh, data, "srgb"); return new ImageData(data, sw, sh); }
putImageData( imagedata: ImageData, dx: number, dy: number, dirtyX?: number, dirtyY?: number, dirtyWidth?: number, dirtyHeight?: number, ) { if (dirtyX !== undefined) { dirtyX = dirtyX ?? 0; dirtyY = dirtyY ?? 0; dirtyWidth = dirtyWidth ?? imagedata.width; dirtyHeight = dirtyHeight ?? imagedata.height; if (dirtyWidth < 0) { dirtyX += dirtyWidth; dirtyWidth = Math.abs(dirtyWidth); } if (dirtyHeight < 0) { dirtyY += dirtyHeight; dirtyHeight = Math.abs(dirtyHeight); } if (dirtyX < 0) { dirtyWidth += dirtyX; dirtyX = 0; } if (dirtyY < 0) { dirtyHeight += dirtyY; dirtyY = 0; } if (dirtyWidth <= 0 || dirtyHeight <= 0) { return; } sk_context_put_image_data_dirty( this[_ptr], imagedata.width, imagedata.height, new Uint8Array(imagedata.data.buffer), imagedata.width * 4, imagedata.data.byteLength, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight, imagedata.colorSpace === "srgb" ? 0 : 1, ); } else { sk_context_put_image_data( this[_ptr], imagedata.width, imagedata.height, new Uint8Array(imagedata.data.buffer), imagedata.width * 4, dx, dy, ); } }
/// Image smoothing
get imageSmoothingEnabled(): boolean { return sk_context_get_image_smoothing_enabled(this[_ptr]) === 1; }
set imageSmoothingEnabled(value: boolean) { sk_context_set_image_smoothing_enabled(this[_ptr], value ? 1 : 0); }
get imageSmoothingQuality(): ImageSmoothingQuality { const quality = sk_context_get_image_smoothing_quality(this[_ptr]); return CImageSmoothingQuality[quality] as ImageSmoothingQuality; }
set imageSmoothingQuality(value: ImageSmoothingQuality) { sk_context_set_image_smoothing_quality( this[_ptr], CImageSmoothingQuality[value], ); }
/// The canvas state
save() { sk_context_save(this[_ptr]); }
restore() { sk_context_restore(this[_ptr]); }
get canvas(): Canvas { return this[_canvas]; }
getContextAttributes(): { alpha: boolean; desynchronized: boolean; } { return { alpha: true, desynchronized: false, }; }
reset() { throw new Error("TODO: Context.reset()"); }
isContextLost(): boolean { return false; }
/// Filters
get filter(): string { return this[_filter]; }
set filter(value: string) { if (value === "none" || value === "") { sk_context_filter_reset(this[_ptr]); this[_filter] = value; return; } const filters = parseFilterString(value); this[_filter] = value; for (const filter of filters) { switch (filter.type) { case FilterType.Blur: sk_context_filter_blur(this[_ptr], filter.value); break;
case FilterType.Brightness: sk_context_filter_brightness(this[_ptr], filter.value); break;
case FilterType.Contrast: sk_context_filter_contrast(this[_ptr], filter.value); break;
case FilterType.DropShadow: sk_context_filter_drop_shadow( this[_ptr], filter.dx, filter.dy, filter.radius, cstr(filter.color), ); break;
case FilterType.Grayscale: sk_context_filter_grayscale(this[_ptr], filter.value); break;
case FilterType.HueRotate: sk_context_filter_hue_rotate(this[_ptr], filter.value); break;
case FilterType.Invert: sk_context_filter_invert(this[_ptr], filter.value); break;
case FilterType.Opacity: sk_context_filter_opacity(this[_ptr], filter.value); break;
case FilterType.Saturate: sk_context_filter_saturated(this[_ptr], filter.value); break;
case FilterType.Sepia: sk_context_filter_sepia(this[_ptr], filter.value); break; } } }}
Version Info