Skip to content

hacxy/l2d-widget

Repository files navigation

l2d-widget

Drop a Live2D character onto any web page. One function call, zero framework dependencies.

npm version downloads bundle size

Documentation | 中文

Important

This project is a full rewrite of oh-my-live2d based on the l2d library. The original oh-my-live2d has been renamed to l2d-widget. If you are migrating from oh-my-live2d, please refer to the documentation for the updated API.

Features

  • Single-call integrationcreateWidget() handles canvas creation, WebGL context initialization, model loading, and interaction bindng in one call
  • Cubism 2 & 6 runtime — built on l2d, auto-detects model version and loads the correct Cubism runtime
  • Lip-sync via parameter driving — character-by-character typing animation drives mouth open/close parameters (PARAM_MOUTH_OPEN_Y) in real time with configurable value ranges
  • Full lifecycle controlswitchModel() runs exit transition → WebGL teardown → re-init → entrance transition as one atomic async operation; destroy() guarantees resource cleanup
  • ~500 lines, zero runtime deps — pure DOM + CSS Animation, no framework. Outputs ESM and IIFE, tree-shakeable

Installation

npm install l2d-widget

Or use a CDN:

<script src="https://unpkg.com/l2d-widget/dist/index.min.js"></script>

Quick Start

import { createWidget } from 'l2d-widget';

const widget = createWidget({
  model: {
    path: 'https://model.hacxy.cn/cat-black/model.json',
  },
});

A Live2D model appears in the bottom-left corner of the page with a hover menu and tip bubbles. The returned widget object gives you programmatic control (switch models, sleep, destroy).

Examples

Multi-Model Switching

createWidget({
  model: [
    { path: '/models/cat-black/model.json' },
    { path: '/models/cat-white/model.json' },
  ],
});

A shuffle button appears automatically in the menu when multiple models are provided.

Typing Animation with Lip-Sync

createWidget({
  model: {
    path: '/models/cat-black/model.json',
    tips: {
      typing: {
        param: 'PARAM_MOUTH_OPEN_Y',
        speed: 200,
      },
      welcomeMessage: ['Hello!', 'Nice to meet you!'],
      messages: ['Take a break~', 'Stay hydrated!'],
      duration: 4000,
      interval: 6000,
    },
  },
});

Widget Instance

createWidget() returns a Widget object:

Method / Property Description
l2d Underlying l2d instance
switchModel(index) Switch to a model by index
sleep() Hide model; click the status bar to wake
destroy() Tear down widget and release resources

For the full options reference (WidgetOptions, ModelOptions, MenusOptions, TipsOptions), see the documentation.

Development

Command Purpose
pnpm dev Watch build with sourcemaps
pnpm demo Demo server at localhost:3000
pnpm build Production build
pnpm lint Lint

License

MIT


中文文档请见 README.zh.md

About

Drop a Live2D character onto any web page. One function call, zero framework dependencies.

Topics

Resources

License

Stars

Watchers

Forks

Contributors