Quick start
Demo project created based on webpack with cdn: https://github.com/eva-engine/start-demo-with-cdn
Demo project build for multi platform(Web/Wechat minigame):
Install
在浏览器中以CDN的形式引入Eva.js,所有的对象将会绑定在 window.EVA 上,未来所用到的插件都会绑定在window.EVA.plugin上。详情参考:基于CDN开发
<style>
#canvas {
width: 100%;
height: auto;
}
</style>
<canvas id="canvas"></canvas>
Add resource
Before creating the game, we need to add resource files to the resource manager, here we add two image resources. Of course, you can add keel animation and spine animation resources. For more information, please see Resource Management.
import {resource, RESOURCE_TYPE} from '@eva/eva.js'
resource.addResource([
{
name:'image1',
type: RESOURCE_TYPE.IMAGE,
src: {
image: {
url:'https://gw.alicdn.com/tfs/TB1DNzoOvb2gK0jSZK9XXaEgFXa-658-1152.webp'
}
},
preload: true
},
{
name:'image2',
src: {
image: {
type:'png',
url:'https://gw.alicdn.com/tfs/TB15Upxqk9l0K4jSZFKXXXFjpXa-750-1624.jpg'
}
},
preload: true
}
])
The Eva.js kernel is a very lightweight runtime, and other functions are implemented through plug-ins. If you want to achieve the most basic rendering capabilities of the game, you need to install the rendering plug-in @eva/plugin-renderer
.
- With NPM
- In Browser
<!-- import PixiJS -->
<script src="//unpkg.com/pixi.js@4.8.9/dist/pixi.min.js"></script>
<!-- import RendererAdapter -->
<script src="//unpkg.com/@eva/renderer-adapter@1.2.x/dist/EVA.rendererAdapter.min.js"></script>
<script src="https://unpkg.com/@eva/plugin-renderer@1.2.x/dist/EVA.plugin.renderer.min.js"></script>
import {Game} from '@eva/eva.js'
import {RendererSystem} from '@eva/plugin-renderer'
// Create a rendering system
canvas: document.querySelector('#canvas'), // Optional, automatically generated canvas hanging on game.canvas
width: 750,
height: 1000,
resolution: window.devicePixelRatio / 2, // Optional, if it is 2 times the image design, it can be divided by 2
enableScroll: true, // Enable page scrolling
renderType: 0 // 0: automatic judgment, 1: WebGL, 2: Canvas, it is recommended to use Canvas below android6.1 ios9, business judgment is required.
})
// Create GameObject
const game = new Game({
frameRate: 60, // Optional, game frame rate, default 60
autoStart: true, // optional, start automatically
systems: [rendererSystem]
})
Of course, this only allows Eva.js to have basic rendering capabilities, but no elements have been displayed on the canvas. Next, we will add gameObject, which will be displayed on the canvas.
Add GameObject
npm i @eva/plugin-renderer @eva/plugin-renderer-img
Method 1: Keep components when creating
const img = gameObject.addComponent(
new Img({
resource:'image1'
})
)
// or
const img = new Img({ resource:'image1' })
gameObject.addComponent(img)
Method 2: Obtain from the GameObject after creation
const img = gameObject.getComponent(Img)
// or
const img = gameObject.getComponent('Img')
Are you ready?
Just introduced the simplest demo of Eva.js, let’s look at some 2D interactive games .