標籤: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