1// Adds JS functions to augment the SkottieKit interface. 2// For example, if there is a wrapper around the C++ call or logic to allow 3// chaining, it should go here. 4 5// SkottieKit.onRuntimeInitialized is called after the WASM library has loaded. 6// Anything that modifies an exposed class (e.g. SkPath) should be set 7// after onRuntimeInitialized, otherwise, it can happen outside of that scope. 8SkottieKit.onRuntimeInitialized = function() { 9 // All calls to 'this' need to go in externs.js so closure doesn't minify them away. 10 11 SkottieKit.SkCanvas.prototype.clear = function (color4f) { 12 var cPtr = copy1dArray(color4f, SkottieKit.HEAPF32); 13 this._clear(cPtr); 14 SkottieKit._free(cPtr); 15 } 16 17 SkottieKit.SkSurface.prototype.requestAnimationFrame = function(callback, dirtyRect) { 18 if (!this._cached_canvas) { 19 this._cached_canvas = this.getCanvas(); 20 } 21 window.requestAnimationFrame(function() { 22 if (this._context !== undefined) { 23 SkottieKit.setCurrentContext(this._context); 24 } 25 26 callback(this._cached_canvas); 27 28 // We do not dispose() of the SkSurface here, as the client will typically 29 // call requestAnimationFrame again from within the supplied callback. 30 // For drawing a single frame, prefer drawOnce(). 31 this.flush(); 32 }.bind(this)); 33 } 34 35 // Run through the JS files that are added at compile time. 36 if (SkottieKit._extraInitializations) { 37 SkottieKit._extraInitializations.forEach(function(init) { 38 init(); 39 }); 40 } 41}; // end SkottieKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic. 42 43// Construct a 4-float color. 44// Opaque if opacity is omitted. 45SkottieKit.Color4f = function(r, g, b, a) { 46 if (a === undefined) { 47 a = 1; 48 } 49 return Float32Array.of(r, g, b, a); 50} 51 52// Color constants use property getters to prevent other code from accidentally 53// changing them. 54Object.defineProperty(SkottieKit, "TRANSPARENT", { 55 get: function() { return SkottieKit.Color4f(0, 0, 0, 0); } 56}); 57Object.defineProperty(SkottieKit, "BLACK", { 58 get: function() { return SkottieKit.Color4f(0, 0, 0, 1); } 59}); 60Object.defineProperty(SkottieKit, "WHITE", { 61 get: function() { return SkottieKit.Color4f(1, 1, 1, 1); } 62}); 63// assets is a dictionary of named blobs: { key: ArrayBuffer, ... } 64// The keys should be well-behaved strings - they're turned into null-terminated 65// strings for the native side. 66SkottieKit.MakeManagedAnimation = function(json, assets) { 67 if (!SkottieKit.managed_skottie) { 68 throw 'Not compiled with MakeManagedAnimation'; 69 } 70 if (!assets) { 71 return SkottieKit._MakeManagedAnimation(json, 0, nullptr, nullptr, nullptr); 72 } 73 var assetNamePtrs = []; 74 var assetDataPtrs = []; 75 var assetSizes = []; 76 77 var assetKeys = Object.keys(assets || {}); 78 for (var i = 0; i < assetKeys.length; i++) { 79 var key = assetKeys[i]; 80 var buffer = assets[key]; 81 var data = new Uint8Array(buffer); 82 83 var iptr = SkottieKit._malloc(data.byteLength); 84 SkottieKit.HEAPU8.set(data, iptr); 85 assetDataPtrs.push(iptr); 86 assetSizes.push(data.byteLength); 87 88 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten 89 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8 90 // Add 1 for null terminator 91 var strLen = lengthBytesUTF8(key) + 1; 92 var strPtr = SkottieKit._malloc(strLen); 93 94 stringToUTF8(key, strPtr, strLen); 95 assetNamePtrs.push(strPtr); 96 } 97 98 // Not entirely sure if it matters, but the uintptr_t are 32 bits 99 // we want to copy our array of uintptr_t into the right size memory. 100 var namesPtr = copy1dArray(assetNamePtrs, SkottieKit.HEAPU32); 101 var assetsPtr = copy1dArray(assetDataPtrs, SkottieKit.HEAPU32); 102 var assetSizesPtr = copy1dArray(assetSizes, SkottieKit.HEAPU32); 103 104 var anim = SkottieKit._MakeManagedAnimation(json, assetKeys.length, namesPtr, 105 assetsPtr, assetSizesPtr); 106 107 // The C++ code has made copies of the asset and string data, so free our copies. 108 SkottieKit._free(namesPtr); 109 SkottieKit._free(assetsPtr); 110 SkottieKit._free(assetSizesPtr); 111 112 return anim; 113}; 114