pixi.js
Create renderer
Create an area that can play animation, equivalent to (canvas
).
//v4.4.2之前的旧写法 //创建 var renderer = PIXI.autoDetectRenderer(w, h, { backgroundColor: 0x1099bb, transparent: true //背景是否设为透明 }); document.body.appendChild(renderer.view); //舞台添加显示对象sprite及每次渲染的监听函数 var stage = new PIXI.Container(); stage.addChild(sprite); animate(); function animate() { renderer.render(stage); requestAnimationFrame(animate); } //v4.4.2之后的新写法 //创建 var app = new PIXI.Application(w, h, { backgroundColor: 0x1099bb, transparent: false //背景是否设为透明 }); //添加显示对象sprite及每次渲染的监听函数 app.stage.addChild(sprite); app.ticker.add(function(delta) {}); document.body.appendChild(app.view);
In addition to the autoDetectRenderer interface, there are also CanvasRenderer and WebGLRenderer interfaces.
autoDetectRenderer can automatically create WebGL or Canvas renderer based on the client's support for WebGL.
Create stage
The stage is equivalent to a container (Container
). After adding elements, the renderer (renderer
) renders the stage. Equivalent to a top-level container.
There is a Container()
class in pixi.js
. This class is a container.
var stage = new PIXI.Container(); 添加舞台之后可以由渲染器(renderer)渲染。 renderer.render(stage); // 舞台(stage)搭建完成后渲染出来。。 ***最后
Creating a material set
The most important element in animation is a picture (material). This type of special picture object is called a sprite (in pixi.js
sprite
),
By controlling the size, position and some other attributes of sprite
, the animation effect can be achieved.
There is a sprite
class in pixi
, which can create a sprite
based on external pictures (materials) that can be used in pixi
##sprite
There are three ways to create:
- Create from a single image
From the entire Material image creation, intercept certain parts according to different positions and sizes on the material to create
sprite-
Create from the material set
The material set is a json file defines the position and size of the image in a certain material image, etc. The advantage of this is that you don’t have to define the position and size every time you create a sprite. On the other hand, you don’t need to modify the code when you modify the material image. .
Load images according to the material set
There is a loader
class in pixi
to manage images Load, and call the callback function after the loading is completed.
PIXI.loader .add("images/treasureHunter.json") .load(setup);
treasureHunter.json is the configuration file of the material set, and setup is the callback function called after the image is loaded.
PIXI.loader After loading is complete, you can obtain the loaded image through PIXI.loader.resources.
Callback function
After completing the image loading, PIXI.loader
will automatically call the setup
function for the next step of processing. Let's first define
a test method to see if it is as expected.
function setup() { console.log("加载完成."); } // 测试可以的话就可以,删除setup里面的东西,然后完善舞台。
Create scene (gameScene)
Games generally create two scenes, one is used to display the normal game screen (gameScene), and the other is used to display the game results (gameOverScene).
var gameScene; function setup() { gameScene = new PIXI.Container(); }
To add all the materials in the container and create the corresponding sprite
, how to add them? The loaded materials can be accessed through PIXI.loader.resources
.
var gameScene; function setup() { gameScene = new PIXI.Container(); }
Note: pixi needs to run on a server. It is recommended to use the http-server local server when debugging.
Game start interface scene
Game end interface scene (one appears and one disappears)
Use pixi to draw graphics
Draw line graphics
First you need to create a graphics class
var graphics = new PIXI.Graphics();
graphics.beginFill(0xFF3300 );
//Graphic fill color##graphics.lineStyle(4, 0xffd900,1);
//Graphic border width, color, transparency
- Drawing according to line point coordinates
graphics.moveTo(50,50); //图形绘制起点 graphics.lineTo(250, 50); //连线到下一个点 graphics.lineTo(100, 100); graphics.lineTo(50, 50); graphics.endFill(); // 图形结束标志Drawing squares and circles
Drawing Block
graphics.drawRect(50, 250, 120, 120);//The parameters are the x point and y point coordinates respectively. Square length, square width
Draw a rounded square
graphics.drawRoundedRect(150, 450, 300, 100, 15);// The first four parameters are the same as drawing a square, and the last corner radius
Drawing a circle
graphics.drawCircle (470, 90,60);//The parameters are x point coordinate, y point coordinate, circle radius 60
- First you need to create a text class
var basicText = new PIXI.Text('Basic text in pixi');
- You can then set the x and y coordinates
basicText.x = 30;
- ##Complex styled class
var style = new PIXI.TextStyle({ fontFamily: 'Arial', //字体 fontSize: 36, //字体大小 fontStyle: 'italic', //字体类型(斜体) fontWeight: 'bold', //加粗 fill: ['#ffffff', '#00ff99'], //由上到下的过渡颜色 stroke: '#4a1850', //文字边框颜色 strokeThickness: 5, //文字边框粗细 dropShadow: true, //阴影 dropShadowColor: '#000000', //阴影颜色 dropShadowBlur: 4, //阴影模糊程度 dropShadowAngle: Math.PI / 6, //阴影角度 dropShadowDistance: 6, //阴影距离 wordWrap: true, //自动换行 wordWrapWidth: 440 }); var richText = new PIXI.Text('Rich text with a lot of options', style); richText.x = 30; richText.y = 180;
The above is the detailed content of A preliminary study on pixi framework in Javascript. For more information, please follow other related articles on the PHP Chinese website!

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.

How to send task notifications in Quartz In advance When using the Quartz timer to schedule a task, the execution time of the task is set by the cron expression. Now...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools