Skip to content

Commit aaa2bbe

Browse files
committed
[canvaskit] Add docs/types for Skottie and Particles
This should be it. Caught up with 0.18.0 Change-Id: Ibe846d756601e42e315ab510807abd4bbd9cf30d Bug: skia:10717 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/322323 Reviewed-by: Kevin Lubick <kjlubick@google.com>
1 parent c488fd3 commit aaa2bbe

File tree

2 files changed

+230
-14
lines changed

2 files changed

+230
-14
lines changed

modules/canvaskit/canvaskit/types/canvaskit-wasm-tests.ts

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@ import CanvasKitInit from "canvaskit-wasm/bin/canvaskit";
44
import {
55
CanvasKit,
66
Paragraph,
7-
PathCommand,
8-
PositionWithAffinity,
9-
PosTan,
107
ShapedText,
118
SkAnimatedImage,
129
SkCanvas,
1310
SkColorFilter,
14-
SkContourMeasure,
15-
SkContourMeasureIter,
1611
SkFont,
1712
SkFontMgr,
1813
SkImage,
@@ -23,18 +18,10 @@ import {
2318
SkPath,
2419
SkPathEffect,
2520
SkPicture,
26-
SkPictureRecorder,
27-
SkPoint,
2821
SkShader,
29-
SkSurface,
3022
SkTextBlob,
3123
SkTypeface,
3224
SkVertices,
33-
TonalColorsOutput,
34-
TypedArray,
35-
URange,
36-
Vector3,
37-
VectorN,
3825
} from "canvaskit-wasm";
3926

4027
CanvasKitInit({locateFile: (file: string) => '/node_modules/canvaskit/bin/' + file}).then((CK: CanvasKit) => {
@@ -53,13 +40,15 @@ CanvasKitInit({locateFile: (file: string) => '/node_modules/canvaskit/bin/' + fi
5340
maskFilterTests(CK);
5441
matrixTests(CK);
5542
paintTests(CK);
56-
paragraphBuilderTests(CK);
5743
paragraphTests(CK);
44+
paragraphBuilderTests(CK);
45+
particlesTests(CK);
5846
pathEffectTests(CK);
5947
pathTests(CK);
6048
pictureTests(CK);
6149
rectangleTests(CK);
6250
runtimeEffectTests(CK);
51+
skottieTests(CK);
6352
shaderTests(CK);
6453
shapedTextTests(CK);
6554
surfaceTests(CK);
@@ -515,6 +504,32 @@ function paragraphBuilderTests(CK: CanvasKit, fontMgr?: SkFontMgr, paint?: SkPai
515504
builder2.addPlaceholder(10, 20, CK.PlaceholderAlignment.Top, CK.TextBaseline.Ideographic, 3);
516505
}
517506

507+
function particlesTests(CK: CanvasKit, canvas?: SkCanvas) {
508+
if (!canvas) return;
509+
510+
const par = CK.MakeParticles('some json'); // $ExpectType Particles
511+
par.draw(canvas);
512+
par.effectUniforms()[0] = 1.2;
513+
const a = par.getEffectUniform(1); // $ExpectType ParticlesUniform
514+
const b = par.getEffectUniformCount(); // $ExpectType number
515+
const c = par.getEffectUniformFloatCount(); // $ExpectType number
516+
const d = par.getEffectUniformName(3); // $ExpectType string
517+
const e = par.getParticleUniform(3); // $ExpectType ParticlesUniform
518+
const f = par.getParticleUniformCount(); // $ExpectType number
519+
const g = par.getParticleUniformFloatCount(); // $ExpectType number
520+
const h = par.getParticleUniformName(3); // $ExpectType string
521+
par.particleUniforms()[2] = 4.5;
522+
par.setPosition([3, 5]);
523+
par.setRate(3);
524+
par.start(0, true);
525+
par.update(2);
526+
527+
const buff = new ArrayBuffer(10);
528+
const par2 = CK.MakeParticles('other json', { // $ExpectType Particles
529+
'flightAnim.gif': buff,
530+
});
531+
}
532+
518533
function pathEffectTests(CK: CanvasKit) {
519534
const pe1 = CK.SkPathEffect.MakeCorner(2); // $ExpectType SkPathEffect | null
520535
const pe2 = CK.SkPathEffect.MakeDash([2, 4]); // $ExpectType SkPathEffect
@@ -599,6 +614,32 @@ function runtimeEffectTests(CK: CanvasKit) {
599614
const s4 = rt.makeShaderWithChildren([4, 5], true, [s1, s2], someMatr); // $ExpectType SkShader
600615
}
601616

617+
function skottieTests(CK: CanvasKit, canvas?: SkCanvas) {
618+
if (!canvas) return;
619+
620+
const anim = CK.MakeAnimation('some json'); // $ExpectType SkottieAnimation
621+
const a = anim.duration(); // $ExpectType number
622+
const b = anim.fps(); // $ExpectType number
623+
const c = anim.version(); // $ExpectType string
624+
const d = anim.size(); // $ExpectType SkPoint
625+
const rect = anim.seek(0.5);
626+
anim.seek(0.6, rect);
627+
const rect2 = anim.seekFrame(12.3);
628+
anim.seekFrame(12.3, rect2);
629+
anim.render(canvas);
630+
anim.render(canvas, rect);
631+
632+
const buff = new ArrayBuffer(10);
633+
const mAnim = CK.MakeManagedAnimation('other json', { // $ExpectType ManagedSkottieAnimation
634+
'flightAnim.gif': buff,
635+
});
636+
mAnim.setColor('slider', CK.WHITE);
637+
mAnim.setOpacity('slider', 0.8);
638+
const e = mAnim.getMarkers(); // $ExpectType object[]
639+
const f = mAnim.getColorProps(); // $ExpectType object[]
640+
const g = mAnim.getOpacityProps(); // $ExpectType object[]
641+
}
642+
602643
function shaderTests(CK: CanvasKit) {
603644
const s1 = CK.SkShader.Color([0.8, 0.2, 0.5, 0.9], // $ExpectType SkShader
604645
CK.SkColorSpace.SRGB);

modules/canvaskit/canvaskit/types/index.d.ts

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,32 @@ export interface CanvasKit {
331331
colors?: Float32Array | ColorIntArray | null, indices?: number[] | null,
332332
isVolatile?: boolean): SkVertices;
333333

334+
/**
335+
* Returns a Skottie animation built from the provided json string.
336+
* Requires that Skottie be compiled into CanvasKit.
337+
* @param json
338+
*/
339+
MakeAnimation(json: string): SkottieAnimation;
340+
341+
/**
342+
* Returns a managed Skottie animation built from the provided json string and assets.
343+
* Requires that Skottie be compiled into CanvasKit.
344+
* @param json
345+
* @param assets - a dictionary of named blobs: { key: ArrayBuffer, ... }
346+
* @param filterPrefix - an optional string acting as a name filter for selecting "interesting"
347+
* Lottie properties (surfaced in the embedded player controls)
348+
*/
349+
MakeManagedAnimation(json: string, assets?: Record<string, ArrayBuffer>,
350+
filterPrefix?: string): ManagedSkottieAnimation;
351+
352+
/**
353+
* Returns a Particles effect built from the provided json string and assets.
354+
* Requires that Particles be compiled into CanvasKit
355+
* @param json
356+
* @param assets
357+
*/
358+
MakeParticles(json: string, assets?: Record<string, ArrayBuffer>): Particles;
359+
334360
/**
335361
* Returns the underlying data from SkData as a Uint8Array.
336362
* @param data
@@ -566,6 +592,14 @@ export interface MallocObj {
566592
toTypedArray(): TypedArray;
567593
}
568594

595+
export interface ManagedSkottieAnimation extends SkottieAnimation {
596+
setColor(key: string, color: InputColor): void;
597+
setOpacity(key: string, opacity: number): void;
598+
getMarkers(): object[];
599+
getColorProps(): object[];
600+
getOpacityProps(): object[];
601+
}
602+
569603
/**
570604
* See Paragraph.h for more information on this class. This is only available if Paragraph has
571605
* been compiled in.
@@ -674,6 +708,105 @@ export interface PositionWithAffinity {
674708
affinity: Affinity;
675709
}
676710

711+
/**
712+
* See SkParticleEffect.h for more details.
713+
*/
714+
export interface Particles extends EmbindObject<Particles> {
715+
/**
716+
* Draws the current state of the particles on the given canvas.
717+
* @param canvas
718+
*/
719+
draw(canvas: SkCanvas): void;
720+
721+
/**
722+
* Returns a Float32Array bound to the WASM memory of these uniforms. Changing these
723+
* floats will change the corresponding uniforms instantly.
724+
*/
725+
effectUniforms(): Float32Array;
726+
727+
/**
728+
* Returns the nth uniform from the effect.
729+
* @param index
730+
*/
731+
getEffectUniform(index: number): ParticlesUniform;
732+
733+
/**
734+
* Returns the number of uniforms on the effect.
735+
*/
736+
getEffectUniformCount(): number;
737+
738+
/**
739+
* Returns the number of float uniforms on the effect.
740+
*/
741+
getEffectUniformFloatCount(): number;
742+
743+
/**
744+
* Returns the name of the nth effect uniform.
745+
* @param index
746+
*/
747+
getEffectUniformName(index: number): string;
748+
749+
/**
750+
* Returns the nth uniform on the particles.
751+
* @param index
752+
*/
753+
getParticleUniform(index: number): ParticlesUniform;
754+
755+
/**
756+
* Returns the count of uniforms on the particles.
757+
*/
758+
getParticleUniformCount(): number;
759+
760+
/**
761+
* Returns the number of float uniforms on the particles.
762+
*/
763+
getParticleUniformFloatCount(): number;
764+
765+
/**
766+
* Returns the name of the nth particle uniform.
767+
* @param index
768+
*/
769+
getParticleUniformName(index: number): string;
770+
771+
/**
772+
* Returns a Float32Array bound to the WASM memory of these uniforms. Changing these
773+
* floats will change the corresponding uniforms instantly.
774+
*/
775+
particleUniforms(): Float32Array;
776+
777+
/**
778+
* Sets the base position of the effect.
779+
* @param point
780+
*/
781+
setPosition(point: SkPoint): void;
782+
783+
/**
784+
* Sets the base rate of the effect.
785+
* @param rate
786+
*/
787+
setRate(rate: number): void;
788+
789+
/**
790+
* Starts playing the effect.
791+
* @param now
792+
* @param looping
793+
*/
794+
start(now: number, looping: boolean): void;
795+
796+
/**
797+
* Updates the effect using the new time.
798+
* @param now
799+
*/
800+
update(now: number): void;
801+
}
802+
803+
export interface ParticlesUniform {
804+
columns: number;
805+
rows: number;
806+
/** The index into the uniforms array that this uniform begins. */
807+
slot: number;
808+
}
809+
677810
/**
678811
* A simple wrapper around SkTextBlob and the simple Text Shaper.
679812
*/
@@ -2181,6 +2314,48 @@ export interface SkVertices extends EmbindObject<SkVertices> {
21812314
uniqueID(): number;
21822315
}
21832316

2317+
export interface SkottieAnimation extends EmbindObject<SkottieAnimation> {
2318+
/**
2319+
* Returns the animation duration in seconds.
2320+
*/
2321+
duration(): number;
2322+
/**
2323+
* Returns the animation frame rate (frames / second).
2324+
*/
2325+
fps(): number;
2326+
2327+
/**
2328+
* Draws current animation frame. Must call seek or seekFrame first.
2329+
* @param canvas
2330+
* @param dstRect
2331+
*/
2332+
render(canvas: SkCanvas, dstRect?: InputRect): void;
2333+
2334+
/**
2335+
* [deprecated] - use seekFrame
2336+
* @param t - value from [0.0, 1.0]; 0 is first frame, 1 is final frame.
2337+
* @param damageRect - will copy damage frame into this if provided.
2338+
*/
2339+
seek(t: number, damageRect?: SkRect): SkRect;
2340+
2341+
/**
2342+
* Update the animation state to match |t|, specified as a frame index
2343+
* i.e. relative to duration() * fps().
2344+
*
2345+
* Returns the rectangle that was affected by this animation.
2346+
*
2347+
* @param frame - Fractional values are allowed and meaningful - e.g.
2348+
* 0.0 -> first frame
2349+
* 1.0 -> second frame
2350+
* 0.5 -> halfway between first and second frame
2351+
* @param damageRect - will copy damage frame into this if provided.
2352+
*/
2353+
seekFrame(frame: number, damageRect?: SkRect): SkRect;
2354+
2355+
size(): SkPoint;
2356+
version(): string;
2357+
}
2358+
21842359
/**
21852360
* Options used for SkPath.stroke(). If an option is omitted, a sensible default will be used.
21862361
*/

0 commit comments

Comments
 (0)