Ionic 整合 pixi.js

來源:互聯網
上載者:User

標籤:win   渲染   one   view   logs   idt   rect   append   types   

最近做了個app,上線google play不大順利,說是有假冒行為,然後改了下icon和名字以及描述,但是沒啥資訊去上,於是暫時放下搞點別的。

 

因為近期看到個比較有趣的繪圖創意,

於是想通過ionic整合pixi來實現,

整合途徑來自這裡:

Integrate pixijs - typescript in Ionic 2 with npm

為啥是pixi呢?因為pixi目前來講是地球上效率最好的js 2d渲染架構。

基於pixi的遊戲架構phaser更是霸氣十足。

 

以下是在ionic中使用pixi的步驟:

1. 建立ionic項目

2. 進入項目運行npm install --save pixi.js:添加pixi包

3. 運行npm install --save @types/pixi.js:添加typescript包

就這樣pixi就在你的項目中可以使用了。

 

到任意一個page裡面,加入一些代碼以確保pixi是否可以正常運行,ts檔案中:

import { Component,ViewChild,ElementRef } from ‘@angular/core‘;import { NavController } from ‘ionic-angular‘;import * as PIXI from ‘pixi.js‘;@Component({  selector: ‘page-home‘,  templateUrl: ‘home.html‘})export class HomePage {  @ViewChild(‘content‘) content:ElementRef;  constructor(public navCtrl: NavController) {    console.log(window.innerWidth,window.innerHeight);  }  ionViewDidLoad(){    var type = "WEBGL";    if(!PIXI.utils.isWebGLSupported){      type = "canvas";    }    PIXI.utils.sayHello(type);        var app = new PIXI.Application(window.innerWidth, window.innerHeight,{backgroundColor:0x1099bb});    this.content.nativeElement.appendChild(app.view);    let bg = new PIXI.Graphics();    bg.beginFill(0xcccccc,0.6);    bg.drawRect(0,0,window.innerWidth, window.innerHeight);    bg.endFill();    bg.interactive = true;    app.stage.addChild(bg);    bg.on(‘pointerdown‘,onClick);    bg.on(‘pointermove‘,onMove);    function onClick(evt){      console.log(‘on touched...‘,evt);    }    function onMove(evt){      var pos = evt.data.getLocalPosition(this.parent);      bg.beginFill(Math.random()*0xffffff,1);      bg.drawCircle(pos.x, pos.y,5);      bg.endFill();      console.log(‘on move...‘);    }  }}

模板檔案.html中修改為如下:

<ion-content>  <div #content style="width:100%" ></div></ion-content>

#content 可以讓它在ts中以ViewChild方式找到。

這裡是一個簡單的拖動繪製隨機顏色圓的demo。

如下:

 

Ionic 整合 pixi.js

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.