Skip to content

Commit

Permalink
chore: update WASM player references and types for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Feb 28, 2025
1 parent 2689ba8 commit 1a5b927
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 34 deletions.
30 changes: 29 additions & 1 deletion .size-limit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,35 @@ module.exports = [
import: '{ DotLottie }',
},
{
name: '@lottiefiles/dotlottie-web WASM',
name: '@lottiefiles/dotlottie-web/webgpu',
path: 'packages/web/dist/webgpu/index.js',
import: '{ DotLottie }',
},
{
name: '@lottiefiles/dotlottie-web/webgl',
path: 'packages/web/dist/webgl/index.js',
import: '{ DotLottie }',
},
{
name: '@lottiefiles/dotlottie-web WASM (WebGL)',
path: 'packages/web/dist/webgl/*.wasm',
modifyWebpackConfig: (config) => {
config.experiments = {
asyncWebAssembly: true,
};
},
},
{
name: '@lottiefiles/dotlottie-web WASM (WebGPU)',
path: 'packages/web/dist/webgpu/*.wasm',
modifyWebpackConfig: (config) => {
config.experiments = {
asyncWebAssembly: true,
};
},
},
{
name: '@lottiefiles/dotlottie-web WASM (Software)',
path: 'packages/web/dist/*.wasm',
modifyWebpackConfig: (config) => {
config.experiments = {
Expand Down
2 changes: 1 addition & 1 deletion apps/dotlottie-react-example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const animations = [
'./dragon.json',
];

setWasmUrl(new URL('../../../packages/web/src/core/dotlottie-player.wasm', import.meta.url).href);
setWasmUrl(new URL('../../../packages/web/src/software/wasm/dotlottie-player.wasm', import.meta.url).href);

function App() {
const [dotLottie, setDotLottie] = useState<DotLottieWorker | null>(null);
Expand Down
2 changes: 1 addition & 1 deletion apps/dotlottie-solid-example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type DotLottie, DotLottieSolid, setWasmUrl } from '@lottiefiles/dotlott
import type { Component } from 'solid-js';
import { createEffect, createSignal, onCleanup } from 'solid-js';

import wasmUrl from '../../../packages/web/src/core/dotlottie-player.wasm?url';
import wasmUrl from '../../../packages/web/src/software/wasm/dotlottie-player.wasm?url';

const animations = [
'https://lottie.host/b06d1336-2c08-4156-aa6f-96f08ff511e0/4itF1pXb1i.lottie',
Expand Down
2 changes: 1 addition & 1 deletion apps/dotlottie-viewer/src/components/players.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@lottiefiles/dotlottie-react';
import dotLottieWebPkg from '@lottiefiles/dotlottie-react/node_modules/@lottiefiles/dotlottie-web/package.json';
import { Range, getTrackBackground } from 'react-range';
import dotLottieWasmUrl from '../../../../packages/web/src/core/dotlottie-player.wasm?url';
import dotLottieWasmUrl from '../../../../packages/web/src/software/wasm/dotlottie-player.wasm?url';
import {
setActiveAnimationId,
setAnimations,
Expand Down
8 changes: 1 addition & 7 deletions apps/dotlottie-viewer/src/pages/perf.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isMobile } from 'react-device-detect';
import { setCanvasKit, Skottie as SkottiePlayer } from '../components/skottie';
import skottieWasmUrl from '../../node_modules/canvaskit-wasm/bin/full/canvaskit.wasm?url';
import InitCanvasKit from 'canvaskit-wasm/bin/full/canvaskit';
import dotLottieWasmUrl from '../../../../packages/web/src/core/dotlottie-player.wasm?url';
import dotLottieWasmUrl from '../../../../packages/web/src/software/wasm/dotlottie-player.wasm?url';

setDotLottieWasmUrl(window.location.origin + dotLottieWasmUrl);

Expand Down Expand Up @@ -325,9 +325,6 @@ export const Perf = (): JSX.Element => {
loop
autoplay
useFrameInterpolation={useFrameInterpolation}
renderConfig={{
renderer: 'wg',
}}
/>
)}
{player.id === 2 && (
Expand All @@ -337,9 +334,6 @@ export const Perf = (): JSX.Element => {
loop
autoplay
useFrameInterpolation={useFrameInterpolation}
renderConfig={{
renderer: 'gl',
}}
/>
)}
{player.id === 3 && (
Expand Down
2 changes: 1 addition & 1 deletion apps/dotlottie-vue-example/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { Ref, onMounted, ref } from 'vue';
import { type DotLottieVueInstance, DotLottieVue, setWasmUrl } from '@lottiefiles/dotlottie-vue';
import wasmUrl from '../../../packages/web/src/core/dotlottie-player.wasm?url';
import wasmUrl from '../../../packages/web/src/software/wasm/dotlottie-player.wasm?url';
setWasmUrl(wasmUrl);
Expand Down
2 changes: 1 addition & 1 deletion apps/dotlottie-wc-example/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { setWasmUrl } from '@lottiefiles/dotlottie-wc';
import type { DotLottieWC } from '@lottiefiles/dotlottie-wc';

// eslint-disable-next-line node/no-unsupported-features/node-builtins
setWasmUrl(new URL('../../../packages/web/src/core/dotlottie-player.wasm', import.meta.url).href);
setWasmUrl(new URL('../../../packages/web/src/software/wasm/dotlottie-player.wasm', import.meta.url).href);

const elementName = 'dotlottie-worker-wc';

Expand Down
4 changes: 0 additions & 4 deletions apps/dotlottie-web-example/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ allCanvas.forEach((canvas) => {
backgroundColor,
renderConfig: {
autoResize: true,
renderer: 'gl',
},
useFrameInterpolation: false,
});
Expand Down Expand Up @@ -206,9 +205,6 @@ fetch(
speed: 1,
backgroundColor: '#800080ff',
themeId: 'animated_light',
renderConfig: {
renderer: 'wg',
},
// useFrameInterpolation: false,
});

Expand Down
4 changes: 3 additions & 1 deletion apps/dotlottie-web-example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"extends": "../../tsconfig.dev.json",

// Compiler options
"compilerOptions": {},
"compilerOptions": {
"moduleResolution": "bundler"
},

"include": ["src"]
}
2 changes: 1 addition & 1 deletion packages/react/setup-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import 'vitest-browser-react';
import { setWasmUrl } from './src';

// eslint-disable-next-line node/no-unsupported-features/node-builtins
const wasmUrl = new URL('../web/src/core/dotlottie-player.wasm?url', import.meta.url).href;
const wasmUrl = new URL('../web/src/software/wasm/dotlottie-player.wasm?url', import.meta.url).href;

setWasmUrl(wasmUrl);
2 changes: 1 addition & 1 deletion packages/svelte/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { DotLottieSvelte, setWasmUrl } from '../lib/index.js';
import type { DotLottie } from '../lib/index.js';
import wasmUrl from "../../../web/src/core/dotlottie-player.wasm?url";
import wasmUrl from "../../../web/src/software/wasm/dotlottie-player.wasm?url";
setWasmUrl(wasmUrl);
Expand Down
2 changes: 1 addition & 1 deletion packages/wc/__tests__/dotlottie-wc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { DotLottieWC } from '../src/dotlottie-wc';
const src = new URL('./__fixtures__/test.lottie', import.meta.url).href;
const lottieSrc = new URL('./__fixtures__/test.json', import.meta.url).href;

setWasmUrl(new URL('../../web/src/core/dotlottie-player.wasm', import.meta.url).href);
setWasmUrl(new URL('../../web/src/software/wasm/dotlottie-player.wasm', import.meta.url).href);

const render = <T extends 'dotlottie-wc' | 'dotlottie-worker-wc'>(
elementName: T,
Expand Down
40 changes: 31 additions & 9 deletions packages/web/src/common/dotlottie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ const createCoreSegment = (segment: number[], module: MainModule): VectorFloat =
return coresegment;
};

// Helper function to safely check if an object is an HTMLCanvasElement
const isHTMLCanvasElement = (obj: unknown): obj is HTMLCanvasElement => {
return typeof HTMLCanvasElement !== 'undefined' && obj instanceof HTMLCanvasElement;
};

let canvasId = 0;

export class DotLottieCommon {
private readonly _canvas: HTMLCanvasElement | OffscreenCanvas;

Expand Down Expand Up @@ -135,9 +142,19 @@ export class DotLottieCommon {
engine = module.TvgEngine.TvgEngineSw;
}

// Get canvas selector for the specific rendering engine
// This converts the canvas to a format that the WASM module can accept
const canvasSelector = this._canvas instanceof HTMLCanvasElement ? this._canvas.id || '#canvas' : '#canvas';
let selector = '';

if (isHTMLCanvasElement(this._canvas)) {
if (this._canvas.id) {
selector = `#${this._canvas.id}`;
} else if (this._renderer !== 'sw') {
if (!this._canvas.getAttribute('data-dl-id')) {
this._canvas.setAttribute('data-dl-id', `${canvasId}`);
canvasId += 1;
}
selector = `[data-dl-id="${this._canvas.getAttribute('data-dl-id')}"]`;
}
}

this._dotLottieCore = new module.DotLottiePlayer(
{
Expand All @@ -159,7 +176,7 @@ export class DotLottieCommon {
},
engine,
0,
canvasSelector,
selector,
);

this._eventManager.dispatch({ type: 'ready' });
Expand All @@ -175,6 +192,7 @@ export class DotLottieCommon {
}
})
.catch((error: Error) => {
console.error('error', error);
this._eventManager.dispatch({
type: 'loadError',
error: new Error(`Failed to load wasm module: ${error}`),
Expand Down Expand Up @@ -666,7 +684,7 @@ export class DotLottieCommon {
public setBackgroundColor(color: string): void {
if (this._dotLottieCore === null) return;

if (IS_BROWSER && this._canvas instanceof HTMLCanvasElement) {
if (IS_BROWSER && isHTMLCanvasElement(this._canvas)) {
this._canvas.style.backgroundColor = color;
} else {
this._dotLottieCore.setConfig({
Expand Down Expand Up @@ -746,7 +764,7 @@ export class DotLottieCommon {
public resize(): void {
if (!this._dotLottieCore || !this.isLoaded) return;

if (IS_BROWSER && this._canvas instanceof HTMLCanvasElement) {
if (IS_BROWSER && isHTMLCanvasElement(this._canvas)) {
const dpr = this._renderConfig.devicePixelRatio || window.devicePixelRatio || 1;

const { height: clientHeight, width: clientWidth } = this._canvas.getBoundingClientRect();
Expand Down Expand Up @@ -944,7 +962,11 @@ export class DotLottieCommon {
}

private _getPointerPosition(event: PointerEvent): { x: number; y: number } {
const rect = (this._canvas as HTMLCanvasElement).getBoundingClientRect();
if (!isHTMLCanvasElement(this._canvas)) {
return { x: 0, y: 0 };
}

const rect = this._canvas.getBoundingClientRect();
const scaleX = this._canvas.width / rect.width;
const scaleY = this._canvas.height / rect.height;

Expand Down Expand Up @@ -1023,7 +1045,7 @@ export class DotLottieCommon {
}

private _setupStateMachineListeners(): void {
if (IS_BROWSER && this._canvas instanceof HTMLCanvasElement && this._dotLottieCore !== null && this.isLoaded) {
if (IS_BROWSER && isHTMLCanvasElement(this._canvas) && this._dotLottieCore !== null && this.isLoaded) {
const listeners = this.getStateMachineListeners();

if (listeners.includes('PointerUp')) {
Expand All @@ -1049,7 +1071,7 @@ export class DotLottieCommon {
}

private _cleanupStateMachineListeners(): void {
if (IS_BROWSER && this._canvas instanceof HTMLCanvasElement) {
if (IS_BROWSER && isHTMLCanvasElement(this._canvas)) {
this._canvas.removeEventListener('pointerup', this._pointerUpMethod);
this._canvas.removeEventListener('pointerdown', this._pointerDownMethod);
this._canvas.removeEventListener('pointermove', this._pointerMoveMethod);
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/software/wasm/dotlottie-player.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export interface DotLottiePlayer extends ClassHandle {

export interface Marker {
duration: number;
name: EmbindString;
name: string;
time: number;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/webgl/wasm/dotlottie-player.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export interface DotLottiePlayer extends ClassHandle {

export interface Marker {
duration: number;
name: EmbindString;
name: string;
time: number;
}

Expand Down
17 changes: 16 additions & 1 deletion packages/web/src/webgpu/wasm-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@ export class WasmLoader {
}

private static async _tryLoad(url: string): Promise<MainModule> {
const module = await createDotLottiePlayerModule({ locateFile: () => url });
if (typeof navigator === 'undefined' || typeof navigator.gpu === 'undefined') {
throw new Error('WebGPU not supported');
}

const adapter = await navigator.gpu.requestAdapter();

if (!adapter) {
throw new Error('WebGPU adapter not found');
}

const device = await adapter.requestDevice();

const module = await createDotLottiePlayerModule({
locateFile: () => url,
preinitializedWebGPUDevice: device,
});

return module;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/webgpu/wasm/dotlottie-player.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export interface DotLottiePlayer extends ClassHandle {

export interface Marker {
duration: number;
name: EmbindString;
name: string;
time: number;
}

Expand Down

0 comments on commit 1a5b927

Please sign in to comment.