1 // Adds compile-time JS functions to augment the CanvasKit interface.
2 // Specifically, anything that should only be on the Skottie builds of canvaskit.
4 // assets is a dictionary of named blobs: { key: ArrayBuffer, ... }
5 // The keys should be well-behaved strings - they're turned into null-terminated
6 // strings for the native side.
8 // prop_filter_prefix is an optional string acting as a name filter for selecting
9 // "interesting" Lottie properties (surfaced in the embedded player controls)
11 // soundMap is an optional object that maps string names to AudioPlayers
12 // AudioPlayers manage a single audio layer with a seek function
14 // logger is an optional logging object, expected to provide two functions:
15 // - onError(err_str, json_node_str)
16 // - onWarning(wrn_str, json_node_str)
17 CanvasKit.MakeManagedAnimation = function(json, assets, prop_filter_prefix, soundMap, logger) {
18 if (!CanvasKit._MakeManagedAnimation) {
19 throw 'Not compiled with MakeManagedAnimation';
21 if (!prop_filter_prefix) {
22 prop_filter_prefix = '';
25 return CanvasKit._MakeManagedAnimation(json, 0, nullptr, nullptr, nullptr, prop_filter_prefix,
28 var assetNamePtrs = [];
29 var assetDataPtrs = [];
32 var assetKeys = Object.keys(assets || {});
33 for (var i = 0; i < assetKeys.length; i++) {
34 var key = assetKeys[i];
35 var buffer = assets[key];
36 var data = new Uint8Array(buffer);
38 var iptr = CanvasKit._malloc(data.byteLength);
39 CanvasKit.HEAPU8.set(data, iptr);
40 assetDataPtrs.push(iptr);
41 assetSizes.push(data.byteLength);
43 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
44 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
45 // Add 1 for null terminator
46 var strLen = lengthBytesUTF8(key) + 1;
47 var strPtr = CanvasKit._malloc(strLen);
49 stringToUTF8(key, strPtr, strLen);
50 assetNamePtrs.push(strPtr);
53 // Not entirely sure if it matters, but the uintptr_t are 32 bits
54 // we want to copy our array of uintptr_t into the right size memory.
55 var namesPtr = copy1dArray(assetNamePtrs, "HEAPU32");
56 var assetsPtr = copy1dArray(assetDataPtrs, "HEAPU32");
57 var assetSizesPtr = copy1dArray(assetSizes, "HEAPU32");
59 var anim = CanvasKit._MakeManagedAnimation(json, assetKeys.length, namesPtr,
60 assetsPtr, assetSizesPtr, prop_filter_prefix,
63 // The C++ code has made copies of the asset and string data, so free our copies.
64 CanvasKit._free(namesPtr);
65 CanvasKit._free(assetsPtr);
66 CanvasKit._free(assetSizesPtr);
72 CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
73 CanvasKit._extraInitializations.push(function() {
75 CanvasKit.Animation.prototype.render = function(canvas, dstRect) {
76 copyRectToWasm(dstRect, _scratchFourFloatsAPtr);
77 this._render(canvas, _scratchFourFloatsAPtr);
80 CanvasKit.Animation.prototype.size = function(optSize) {
81 // This will copy 2 floats into a space for 4 floats
82 this._size(_scratchFourFloatsAPtr);
83 var ta = _scratchFourFloatsA['toTypedArray']();
85 // We cannot call optSize.set() because it is an error to call .set() with
86 // a source bigger than the destination.
91 // Be sure to return a copy of just the first 2 values.
92 return ta.slice(0, 2);
95 if (CanvasKit.ManagedAnimation) {
96 CanvasKit.ManagedAnimation.prototype.render = function(canvas, dstRect) {
97 copyRectToWasm(dstRect, _scratchFourFloatsAPtr);
98 this._render(canvas, _scratchFourFloatsAPtr);
101 CanvasKit.ManagedAnimation.prototype.seek = function(t, optDamageRect) {
102 this._seek(t, _scratchFourFloatsAPtr);
103 var ta = _scratchFourFloatsA['toTypedArray']();
105 optDamageRect.set(ta);
106 return optDamageRect;
111 CanvasKit.ManagedAnimation.prototype.seekFrame = function(frame, optDamageRect) {
112 this._seekFrame(frame, _scratchFourFloatsAPtr);
113 var ta = _scratchFourFloatsA['toTypedArray']();
115 optDamageRect.set(ta);
116 return optDamageRect;
121 CanvasKit.ManagedAnimation.prototype.setColor = function(key, color) {
122 var cPtr = copyColorToWasm(color);
123 return this._setColor(key, cPtr);
126 CanvasKit.ManagedAnimation.prototype.size = function(optSize) {
127 // This will copy 2 floats into a space for 4 floats
128 this._size(_scratchFourFloatsAPtr);
129 var ta = _scratchFourFloatsA['toTypedArray']();
131 // We cannot call optSize.set() because it is an error to call .set() with
132 // a source bigger than the destination.
137 // Be sure to return a copy of just the first 2 values.
138 return ta.slice(0, 2);
144 }(Module)); // When this file is loaded in, the high level object is "Module";