1 /* 2 * Copyright 2019 Google LLC 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "include/core/SkCanvas.h" 9 #include "include/core/SkTypes.h" 10 #include "include/utils/SkRandom.h" 11 #include "modules/particles/include/SkParticleDrawable.h" 12 #include "modules/particles/include/SkParticleEffect.h" 13 #include "modules/particles/include/SkParticleSerialization.h" 14 15 #include <string> 16 17 #include <emscripten.h> 18 #include <emscripten/bind.h> 19 20 using namespace emscripten; 21 EMSCRIPTEN_BINDINGS(Particles)22EMSCRIPTEN_BINDINGS(Particles) { 23 class_<SkParticleEffect>("SkParticleEffect") 24 .smart_ptr<sk_sp<SkParticleEffect>>("sk_sp<SkParticleEffect>") 25 .function("draw", &SkParticleEffect::draw, allow_raw_pointers()) 26 .function("start", select_overload<void (double, bool)>(&SkParticleEffect::start)) 27 .function("update", select_overload<void (double)>(&SkParticleEffect::update)); 28 29 function("MakeParticles", optional_override([](std::string json)->sk_sp<SkParticleEffect> { 30 static bool didInit = false; 31 if (!didInit) { 32 REGISTER_REFLECTED(SkReflected); 33 SkParticleBinding::RegisterBindingTypes(); 34 SkParticleDrawable::RegisterDrawableTypes(); 35 didInit = true; 36 } 37 SkRandom r; 38 sk_sp<SkParticleEffectParams> params(new SkParticleEffectParams()); 39 skjson::DOM dom(json.c_str(), json.length()); 40 SkFromJsonVisitor fromJson(dom.root()); 41 params->visitFields(&fromJson); 42 return sk_sp<SkParticleEffect>(new SkParticleEffect(std::move(params), r)); 43 })); 44 constant("particles", true); 45 46 } 47