# canvas **Repository Path**: happy0232/canvas ## Basic Information - **Project Name**: canvas - **Description**: canvas学习、demo - **Primary Language**: JavaScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-08-17 - **Last Updated**: 2025-06-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Canvas Game Engine A modern 2D game engine built with TypeScript and Canvas. This engine provides a robust foundation for creating 2D games with features like: - Component-based architecture - Scene and layer management - Event system - Physics and collision detection - Sprite animation - Input handling - Resource management ## Installation ```bash npm install canvas-game-engine ``` ## Quick Start ```typescript import { Engine, GameObject, Layer, SpriteComponent } from 'canvas-game-engine'; // Create a canvas element const canvas = document.createElement('canvas'); document.body.appendChild(canvas); // Initialize the engine const engine = new Engine(canvas, { width: 800, height: 600, backgroundColor: '#000000', targetFPS: 60 }); // Create a layer const gameLayer = new Layer({ name: 'GameLayer' }); engine.addLayer(gameLayer); // Create a game object const player = new GameObject({ name: 'Player', x: 400, y: 300 }); // Add a sprite component const sprite = player.addComponent(new SpriteComponent('player.png')); sprite.play('idle'); // Add the player to the layer gameLayer.addObject(player); // Start the game loop engine.start(); ``` ## Core Concepts ### Game Objects Game objects are the basic building blocks of your game. They can have components attached to them that provide functionality like rendering, physics, input handling, etc. ```typescript const object = new GameObject({ name: 'MyObject', x: 100, y: 100, rotation: Math.PI / 4 }); // Add components object.addComponent(new SpriteComponent('sprite.png')); object.addComponent(new RigidBodyComponent()); // Add to a layer layer.addObject(object); ``` ### Components Components provide reusable functionality to game objects. The engine comes with several built-in components: - TransformComponent: Handles position, rotation, and scale - SpriteComponent: Renders sprites and handles animations - RigidBodyComponent: Provides physics simulation - ColliderComponent: Handles collision detection - InputComponent: Processes user input You can also create your own components: ```typescript class HealthComponent extends Component { private health: number; constructor(gameObject: GameObject, initialHealth: number) { super(gameObject); this.health = initialHealth; } public takeDamage(amount: number): void { this.health -= amount; if (this.health <= 0) { this.gameObject.destroy(); } } } ``` ### Layers Layers help organize your game objects and control rendering order. Each layer can have its own set of objects and can be enabled/disabled independently. ```typescript // Create layers const backgroundLayer = new Layer({ name: 'Background', zIndex: 0 }); const gameLayer = new Layer({ name: 'Game', zIndex: 1 }); const uiLayer = new Layer({ name: 'UI', zIndex: 2 }); // Add layers to engine engine.addLayer(backgroundLayer); engine.addLayer(gameLayer); engine.addLayer(uiLayer); // Add objects to layers backgroundLayer.addObject(new GameObject({ /* ... */ })); gameLayer.addObject(new GameObject({ /* ... */ })); uiLayer.addObject(new GameObject({ /* ... */ })); ``` ### Events The engine includes a robust event system that allows objects to communicate with each other: ```typescript // Listen for events object.on('collision', (other: GameObject) => { console.log(`Collided with ${other.name}`); }); // Emit events object.emit('collision', otherObject); ``` ## Advanced Features ### Physics The engine includes a simple physics system: ```typescript const rigidBody = object.addComponent(new RigidBodyComponent({ mass: 1, velocity: new Vector2(0, 0), acceleration: new Vector2(0, 9.8) })); // Apply forces rigidBody.applyForce(new Vector2(10, 0)); ``` ### Animation The sprite system supports sprite sheets and animations: ```typescript const sprite = object.addComponent(new SpriteComponent('character.png', { frameWidth: 32, frameHeight: 32, animations: { idle: { frames: [0, 1, 2, 3], frameRate: 8 }, walk: { frames: [4, 5, 6, 7], frameRate: 12 }, jump: { frames: [8, 9, 10], frameRate: 10 } } })); // Play animations sprite.play('idle'); sprite.play('walk'); ``` ### Input Handling The engine provides easy input handling: ```typescript const input = object.addComponent(new InputComponent()); input.onKeyDown('ArrowUp', () => { object.transform.translate(new Vector2(0, -5)); }); input.onMouseMove((position: Vector2) => { object.transform.lookAt(position); }); ``` ## Contributing Contributions are welcome! Please feel free to submit a Pull Request. ## License This project is licensed under the MIT License - see the LICENSE file for details. #### 介绍 canvas学习、demo #### 软件架构 软件架构说明 #### 安装教程 1. xxxx 2. xxxx 3. xxxx #### 使用说明 1. xxxx 2. xxxx 3. xxxx #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)