1// Adds compile-time JS functions to augment the CanvasKit interface. 2// Specifically, anything that should only be on the Particle 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. 7CanvasKit.MakeParticles = function(json, assets) { 8 if (!CanvasKit._MakeParticles) { 9 throw 'Not compiled with MakeParticles'; 10 } 11 if (!assets) { 12 return CanvasKit._MakeParticles(json, 0, nullptr, nullptr, nullptr); 13 } 14 var assetNamePtrs = []; 15 var assetDataPtrs = []; 16 var assetSizes = []; 17 18 var assetKeys = Object.keys(assets || {}); 19 for (var i = 0; i < assetKeys.length; i++) { 20 var key = assetKeys[i]; 21 var buffer = assets[key]; 22 var data = new Uint8Array(buffer); 23 24 var iptr = CanvasKit._malloc(data.byteLength); 25 CanvasKit.HEAPU8.set(data, iptr); 26 assetDataPtrs.push(iptr); 27 assetSizes.push(data.byteLength); 28 29 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten 30 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8 31 // Add 1 for null terminator 32 var strLen = lengthBytesUTF8(key) + 1; 33 var strPtr = CanvasKit._malloc(strLen); 34 35 stringToUTF8(key, strPtr, strLen); 36 assetNamePtrs.push(strPtr); 37 } 38 39 // Not entirely sure if it matters, but the uintptr_t are 32 bits 40 // we want to copy our array of uintptr_t into the right size memory. 41 var namesPtr = copy1dArray(assetNamePtrs, "HEAPU32"); 42 var assetsPtr = copy1dArray(assetDataPtrs, "HEAPU32"); 43 var assetSizesPtr = copy1dArray(assetSizes, "HEAPU32"); 44 45 var particles = CanvasKit._MakeParticles(json, assetKeys.length, 46 namesPtr, assetsPtr, assetSizesPtr); 47 48 // The C++ code has made copies of the asset and string data, so free our copies. 49 CanvasKit._free(namesPtr); 50 CanvasKit._free(assetsPtr); 51 CanvasKit._free(assetSizesPtr); 52 53 return particles; 54}; 55 56CanvasKit._extraInitializations = CanvasKit._extraInitializations || []; 57CanvasKit._extraInitializations.push(function() { 58 59 CanvasKit.ParticleEffect.prototype.uniforms = function() { 60 var fptr = this._uniformPtr(); 61 var numFloats = this.getUniformFloatCount(); 62 if (!fptr || numFloats <= 0) { 63 return new Float32Array(); 64 } 65 return new Float32Array(CanvasKit.HEAPU8.buffer, fptr, numFloats); 66 }; 67 68 CanvasKit.ParticleEffect.prototype.setPosition = function(pos) { 69 this._setPosition(pos[0], pos[1]); 70 }; 71}); 72