简体   繁体   English

如何在 Phaser 游戏中使用 ChartJs

[英]How to use ChartJs in Phaser Game

I want to show stats in Phaser with some charts, and I'm using chart.js. I have a problem and one question我想在 Phaser 中用一些图表显示统计数据,我正在使用 chart.js。我有一个问题和一个问题

  1. problem: I'm trying to load a generated Chart.js base64Image in phaser, but it is not showing.问题:我试图在移相器中加载生成的 Chart.js base64Image,但它没有显示。 Am I missing something?我错过了什么吗?

  2. question: Is it somehow possible to draw the chart.js on the phaser canvas?问题:是否有可能在移相器 canvas 上绘制 chart.js? Because I would like to have the chart animation in the game, if possible.因为如果可能的话,我想在游戏中有图表 animation。

Here is a mini Demo, showing what I tried:这是一个迷你演示,展示了我的尝试:
(with a demo chart generation) (带有演示图表生成)

 document.body.style = 'margin:0;'; // Chart Generation Helper function for DEMO function generateChart(callback){ let data = { // labels: ['Highscore'], datasets: [{ data: [{y: 100, x: '01.01'}, {y: 80, x: '04.01'}, {y: 130, x: '05.01'}, {y: 110, x: '06.01'}, {y: 199, x: '08.01'},], backgroundColor: ['#6BFF6B'], borderColor: ['#6BFF6B'], }], }; const chartConfig = { type: 'line', data: data, options: { maintainAspectRatio: false, plugins: { legend: { display:false } }, scales: { x: { title: {display: false}, grid: { color: '#6BFF6B' }, ticks: { color: '#6BFF6B' }, }, y: { title: {display: false}, grid: { color: '#6BFF6B' }, ticks: { color: '#6BFF6B' }, } }, animation: { onComplete: () => { callback(chart.toBase64Image()); } } } }; let chart = new Chart( document.getElementById('chart'), chartConfig ); } var config = { type: Phaser.AUTO, width: 536, height: 190, scene: { create } }; function create () { generateChart(base64Image => { this.textures.addBase64('chart', base64Image); this.add.image(10, 10, 'chart').setOrigin(0); }); } new Phaser.Game(config);
 <script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script> <script src="//cdn.jsdelivr.net/npm/chart.js"></script> <div id="chart-wrapper" class="chart" style="height:170px; width:350px;"> <canvas id="chart" ></canvas> </div> <div id="phaser-example"></div>

Thanks to @DerekLawrence comments, the best solution with animation is to use phaser Phaser.GameObjects.DOMElement ( link to documentation ) .感谢@DerekLawrence的评论,animation 的最佳解决方案是使用移相器Phaser.GameObjects.DOMElement 文档链接

here the demo working:这里的演示工作:

 document.body.style = 'margin:0;'; // Chart Generation Helper function for DEMO function generateChart(callback){ let data = { // labels: ['Highscore'], datasets: [{ data: [{y: 100, x: '01.01'}, {y: 80, x: '04.01'}, {y: 130, x: '05.01'}, {y: 110, x: '06.01'}, {y: 199, x: '08.01'},], backgroundColor: ['#6BFF6B'], borderColor: ['#6BFF6B'], }], }; const chartConfig = { type: 'line', data: data, options: { maintainAspectRatio: false, plugins: { legend: { display:false } }, scales: { x: { title: {display: false}, grid: { color: '#6BFF6B' }, ticks: { color: '#6BFF6B' }, }, y: { title: {display: false}, grid: { color: '#6BFF6B' }, ticks: { color: '#6BFF6B' }, } }, animation: { onComplete: () => { callback(chart.toBase64Image()); } } } }; let chart = new Chart( document.getElementById('chart'), chartConfig ); } var config = { type: Phaser.AUTO, width: 536, height: 190, scene: { preload, create } }; function preload(){ generateChart(); } function create () { let chart = this.add.dom(10, 10, '#chart-wrapper').setOrigin(0); // chart.setInteractive().on('pointerdown', function (){console.info(arguments)}) } new Phaser.Game(config);
 <script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script> <script src="//cdn.jsdelivr.net/npm/chart.js"></script> <div id="chart-wrapper" class="chart" style="height:170px; width:350px;"> <canvas id="chart" ></canvas> </div> <div id="phaser-example"></div>

btw.: I found the reason why the base64 image was not showing up, I failed to wait for the image to load (the event addTexture ) .顺便说一句:我找到了 base64 图像没有显示的原因,我未能等待图像加载(事件addTexture

function create(){
    ...

    generateChart(base64Image => {
        this.textures.addBase64('chart', base64Image);
    });
    
    // event triggered after texture is loaded
    this.textures.once('addtexture', function () {
        this.add.image(10, 10, 'chart').setOrigin(0);
    }, this);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM