This time I will bring you a summary of using Pixi.js. What are the precautions when using Pixi.js? The following is a practical case, let’s take a look.
Pixi.js is a 2D rendering engine written in JavaScript. It can be used to make "rich visual" applications such as interactive graphics, animations and games in the browser. It mainly supports hardware GPU-rendered WebGL API. If the browser does not support WebGL, Pixi will fall back to HTML5 Canvas for rendering. Pixi is mainly responsible for rendering images. Developers have to write many other functions themselves or use them with other libraries. For example, the Phaser framework used to develop web games uses Pixi for rendering. The following content is selected from the book Learning Pixi.js. You can give Pixi a try.
Note: The JavaScript in the example uses ES6, var becomes let, function(){} becomes () => {}.
Making sprites
The basic building module in Pixi is an object called sprite. A sprite is a graphic that can be controlled with code. You can control their position, size, and other properties to create interactive and animated graphics. Learning how to make and control sprites is indeed the most important thing when learning to use Pixi. If you know how to make and display sprites, you are only a small step away from starting to make games or any other interactive program.
In this chapter, you will learn the necessary knowledge required to display and position sprites in Pixi's Canvas, including the following content:
How to make a root container (root container), called a stage (stage) )
How to create a new renderer
Use the loader to load graphics into Pixi's texture cache (texture cache)
Use the loaded graphics including tilesets and texture atlases, making sprites
Before we start making sprites, let's create a rectangular-like screen for displaying them.
Create renderer and stage
Pixi has a renderer object (renderer) for you to create a display screen. It will automatically generate an HTML
//Create the renderer Create a renderer let renderer = PIXI.autoDectectRenderer(256, 256);//Add the canvas to the HTML document Add canvasdocunment.body.appendChild(renderer.view);//Create a container object called the ' stage' creates a container object called stage let stage = new PIXI.Container();//Tell the 'renderer' to 'render' the 'stage' tells the renderer to render a stage renderer.render(stage);
This is the most basic code you need to start using Pixi, it generates a black canvas element with a size of 256 pixels by 256 pixels and adds it to the HTML document
This The simple black square should bring pure joy to your little heart! That's because getting Pixi to start displaying content is both the first and most important step, so let's break down what this code does.
Rendering options
Pixi's autoDetectRenderer method will automatically choose whether to use the Canvas API or WebGL to render the image based on the available status.
let renderer = PIXI.autoDetectRenderer(256, 256);
The first and second parameters of the above code represent the width and height of the canvas respectively. However, you can also add a third optional parameter for you to set some additional values. This third parameter is an object literal. The following code is an example of setting the renderer's anti-aliasing, transparency, and resolution:
renderer = PIXI.autoDetectRenderer( 256, 256, { antialias: false, transparent: false, resolution: 1 } );
Whether to use the third parameter is optional. If you are satisfied with Pixi's default value, you don't need to worry about it. Generally speaking, there is no need to change it. But it’s best to understand what these settings are used for. Antialiasing can smooth the edges of fonts and graphics (WebGL’s anti-aliasing is not yet available for all platforms, so it is best to test this feature on your application platform. ). The transparency option makes the background of the canvas transparent. The resolution option makes it easier to match different resolutions and pixel densities. Generally, setting it to 1 can handle most projects. For more information on resolution matching, see Chapter 6.
Tips This renderer has another fourth option called preserveDrawingBuffer, and the default value is false. The only time you need to set it to true is when calling a Pixi special method dataToURL in a WebGL context. You may have to do this if you need to convert a Pixi canvas into an HTML image object.
Pixi的autoDetectRenderer将决定使用canvas绘图接口还是WebGL来显示图像。缺省是WebGL,这是好的,因为WebGL特别的快而且能使用很多引人入胜的特效,这些你都能在本书后面学到。但是如果你想强制使用canvas绘图接口而不是
WebGL,你可以这么写:
renderer = new PIXI.CanvasRenderer(256, 256);
上面的情况只需要头两个参数:宽和高。
你也可以强制使用WebGL渲染器,如下所示:
renderer = new PIXI.WebGLRenderer(256, 256);
现在我们再来看看如何增强渲染器的外表
定制canvas
renderer.view对象只是一个普通平常的
renderer.view.style.border = "1px dashed black";
如果新建canvas之后,你需要改变它的背景颜色,那就给渲染器对象的backgroundColor设置任何十六进制颜色值即可,下面的代码给了一个纯白色的背景:
renderer.backgroundColor = 0xFFFFFF;
提示 网上可以搜索到很多十六进制配色表,你可以很容易选择一个合适的背景色。
如果你想知道渲染器的宽和高,使用renderer.view.width和renderer.view.height即可。
要改变canvas的大小,使用渲染器的resize()方法,然后应用新的宽高值,如下所示:
renderer.resize(512, 512);
如果想让canvas充满整个窗口,你可以使用如下CSS样式:
renderer.view.style.position = "absolute"; renderer.view.style.width = window.innerWidth + "px"; renderer.view.style.height = window.innerHeight + "px"; renderer.viwe.sytle.display = "block";
注意,如果要这么做,你必须还得将所有HTML元素的padding和margin值都设为0, 如下所示:
(上面的*号是CSS里的通配选择器,就是指所有标签)如果没有上面这条CSS样式,你可能会发现在Pixi画布与浏览器边缘之间有几像素的空隙。
相信看了这些案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:
一个用Vue.js 2.0+做出的石墨文档样式的富文本编辑器
The above is the detailed content of Summary of using Pixi.js. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
