PixiJS教程(一):快速搭建环境启动项目

文章目录

一、快速搭建环境,创建项目

1、快速搭建 PixiJS + Vite 环境

快速创建项目:

bash 复制代码
# 创建项目(选择 Vanilla 和 JavaScript)
npm create vite@latest pixi-master

安装pixi.js:

bash 复制代码
npm install pixi.js

2、创建入门项目

在main.js中添加如下代码:

js 复制代码
import { Application, Assets, Sprite } from 'pixi.js';

// 1. 创建应用实例
const app = new Application();

async function setup() {
    // 2. 初始化 Application (v8 必须异步初始化)
    await app.init({ 
        width: 800, 
        height: 400,
        backgroundColor: 0x1099bb,
        antialias: true, // 开启抗锯齿
        resolution: window.devicePixelRatio || 1, // 适配高分屏
    });

    // 将生成的 canvas 添加到页面
    document.body.appendChild(app.canvas);

    // 3. 加载资源 (Assets 管理器)
    const texture = await Assets.load('https://pixijs.com/assets/bunny.png');

    // 4. 创建精灵并加入舞台
    const bunny = new Sprite(texture);
    bunny.anchor.set(0.5); // 设置中心点
    bunny.x = app.screen.width / 2;
    bunny.y = app.screen.height / 2;

    app.stage.addChild(bunny);

    // 5. 动画循环
    app.ticker.add((time) => {
        // time.deltaTime 是平滑帧率参数
        bunny.rotation += 0.1 * time.deltaTime;
    });
}

setup();

然后启动项目:

bash 复制代码
npm run dev

运行效果:

相关推荐
#麻辣小龙虾#12 分钟前
网页布局css中flex+margin的组合布局技巧
开发语言·前端·javascript
叼烟扛炮16 分钟前
Linux 学习笔记(四十五):Web 安全基石——Cookie、Session 与 HTTPS 原理解析
linux·前端·网络·笔记·学习·tcp/ip·安全
环境栈笔记22 分钟前
指纹浏览器安卓怎么用?从运行位置到功能验收,先把三种方案分清
前端·人工智能·后端·自动化
计算机魔术师40 分钟前
Anthropic IPO 招股文件披露近 9000 亿美元隐性债务
前端
chi11301 小时前
【无标题】
前端·javascript·python
IT_陈寒2 小时前
JavaScript实战技巧总结:深挖数组遍历的性能陷阱与实战解法
前端·人工智能·后端
步行cgn2 小时前
为什么既要 web.xml 又要 springmvc.xml?能不能只配一个?
xml·前端·hive
风骏时光牛马2 小时前
大模型综合能力评测:多维度性能与对齐效果对比
前端
winfredzhang2 小时前
从零手写一个毛玻璃风格的页面搭建台:单文件 HTML 实现组件放置、面板磁吸与图层管理
前端·html
葡萄城技术团队10 小时前
1000 人协同压测的环境瓶颈排查:CPU 争用与 Wi\-Fi
前端