• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Adds compile-time JS functions to augment the CanvasKit interface.
2// Specifically, anything that should only be on the Skottie builds of canvaskit.
3
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.
7
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)
10
11// soundMap is an optional object that maps string names to AudioPlayers
12// AudioPlayers manage a single audio layer with a seek function
13
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)
17CanvasKit.MakeManagedAnimation = function(json, assets, prop_filter_prefix, soundMap, logger) {
18  if (!CanvasKit._MakeManagedAnimation) {
19    throw 'Not compiled with MakeManagedAnimation';
20  }
21  if (!prop_filter_prefix) {
22    prop_filter_prefix = '';
23  }
24  if (!assets) {
25    return CanvasKit._MakeManagedAnimation(json, 0, nullptr, nullptr, nullptr, prop_filter_prefix,
26                                           soundMap, logger);
27  }
28  var assetNamePtrs = [];
29  var assetDataPtrs = [];
30  var assetSizes    = [];
31
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);
37
38    var iptr = CanvasKit._malloc(data.byteLength);
39    CanvasKit.HEAPU8.set(data, iptr);
40    assetDataPtrs.push(iptr);
41    assetSizes.push(data.byteLength);
42
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);
48
49    stringToUTF8(key, strPtr, strLen);
50    assetNamePtrs.push(strPtr);
51  }
52
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");
58
59  var anim = CanvasKit._MakeManagedAnimation(json, assetKeys.length, namesPtr,
60                                             assetsPtr, assetSizesPtr, prop_filter_prefix,
61                                             soundMap, logger);
62
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);
67
68  return anim;
69};
70
71(function(CanvasKit){
72  CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
73  CanvasKit._extraInitializations.push(function() {
74
75  CanvasKit.Animation.prototype.render = function(canvas, dstRect) {
76    copyRectToWasm(dstRect, _scratchFourFloatsAPtr);
77    this._render(canvas, _scratchFourFloatsAPtr);
78  };
79
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']();
84    if (optSize) {
85      // We cannot call optSize.set() because it is an error to call .set() with
86      // a source bigger than the destination.
87      optSize[0] = ta[0];
88      optSize[1] = ta[1];
89      return optSize;
90    }
91    // Be sure to return a copy of just the first 2 values.
92    return ta.slice(0, 2);
93  };
94
95  if (CanvasKit.ManagedAnimation) {
96    CanvasKit.ManagedAnimation.prototype.render = function(canvas, dstRect) {
97    copyRectToWasm(dstRect, _scratchFourFloatsAPtr);
98    this._render(canvas, _scratchFourFloatsAPtr);
99    };
100
101    CanvasKit.ManagedAnimation.prototype.seek = function(t, optDamageRect) {
102      this._seek(t, _scratchFourFloatsAPtr);
103      var ta = _scratchFourFloatsA['toTypedArray']();
104      if (optDamageRect) {
105        optDamageRect.set(ta);
106        return optDamageRect;
107      }
108      return ta.slice();
109    };
110
111    CanvasKit.ManagedAnimation.prototype.seekFrame = function(frame, optDamageRect) {
112      this._seekFrame(frame, _scratchFourFloatsAPtr);
113      var ta = _scratchFourFloatsA['toTypedArray']();
114      if (optDamageRect) {
115        optDamageRect.set(ta);
116        return optDamageRect;
117      }
118      return ta.slice();
119    };
120
121    CanvasKit.ManagedAnimation.prototype.setColor = function(key, color) {
122      var cPtr = copyColorToWasm(color);
123      return this._setColor(key, cPtr);
124    };
125
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']();
130      if (optSize) {
131        // We cannot call optSize.set() because it is an error to call .set() with
132        // a source bigger than the destination.
133        optSize[0] = ta[0];
134        optSize[1] = ta[1];
135        return optSize;
136      }
137      // Be sure to return a copy of just the first 2 values.
138      return ta.slice(0, 2);
139    };
140  }
141
142
143});
144}(Module)); // When this file is loaded in, the high level object is "Module";
145