how to set character frame of spritesheet in phaser js? - phaser-framework

how to set frame of character?
here is my code
this.load.spritesheet(`dude-1`, `../src/assets/Character/ch-1.png`, {
frameWidth: 72,
frameHeight: 92,
});
main logic code is here
this.player = this.physics.add.sprite(100, -500, this.charSelect).setOrigin(1, 1);
this.player.body.setGravityY(1700);
this.player.setBounce(0.1);
this.physics.add.existing(this.player).body.setSize(15, 90, ).setOffset(20, 0)
this.anims.create({
key: "left",
frames: this.anims.generateFrameNumbers(this.charSelect, { start: 6, end: 10 }),
frameRate: 10,
repeat: -1,
});
this.anims.create({
key: "turn",
frames: [{ key: this.charSelect, frame: 5 }],
frameRate: 10,
});
this.anims.create({
key: "right",
frames: this.anims.generateFrameNumbers(this.charSelect, { start: 0, end: 4 }),
frameRate: 10,
repeat: -1,
});
cursors = this.input.keyboard.createCursorKeys();
this.physics.add.collider(this.player, this.platformGroup);
this.player.body.setCollideWorldBounds(true);
this.physics.world.setBounds(0, 0, (config.width) - 10, config.height, true, true, false, true);
here is output and issue. how to set the frame of character?

The problem is, that the phaser-image / frame size is too big (or the actual image is to small). Check the frame size (frameWidth and frameHeight) and the actual image size, that could solve your problem.
Here a short demo:
It loads the same spritesheet, once with the correct frameWidth / frameHeight size and once with a too big size.
document.body.style = `padding:0;margin:0;`;
class Example extends Phaser.Scene {
constructor() {
super();
}
preload() {
this.load.spritesheet('mummyCorrectSize', 'https://labs.phaser.io/assets/animations/mummy37x45.png', { frameWidth: 37, frameHeight: 45 });
this.load.spritesheet('mummyWrongSize', 'https://labs.phaser.io/assets/animations/mummy37x45.png', { frameWidth: 50, frameHeight: 50 });
}
create(){
// the "const" variables show the the wrong and right sprite creation
const wrongSizeSprite= this.add.sprite(50, 40, 'mummyCorrectSize').setOrigin(0);
const rightSizeSprite = this.add.sprite(50, 120, 'mummyWrongSize').setOrigin(0);
this.add.text(50, 10, 'Right size: single sprite/frame shown');
this.add.text(50, 100, 'Wrong size: the whole image is shown');
}
}
const config = {
type: Phaser.AUTO,
width: 500,
height: 180,
scene: [ Example ]
};
new Phaser.Game(config);
<script src="//cdn.jsdelivr.net/npm/phaser#3.55.2/dist/phaser.js"></script>

Related

I want to create a Weibull probability Plot in Angular

This is the chart i am trying to create in Angular. This comes from the reliability package in python.
https://github.com/MatthewReid854/reliability/tree/master/reliability
I am using Highchart - Area-Range + scatter. I am able to fix the x-axis as it is just logarithmic scale. But I am unable to fix the y -axis
For Weibull analysis, formula for Y-Axis Scale is as: ln(-ln(1-p))
where p=(i-0.3)/(n+0.4) and i is the rank of the observation. This scale is chosen in order to linearize the resulting plot for Weibull data.
weibullHighcharts(weibullresp:any,statDataresp:any){
const chart = Highcharts.chart('chart-gauge', {
chart: {
marginTop: 5,
height: 380,
inverted: true,
zoomType: 'xy',
credits: false,
events: {
load() {
let chart = this;
},
},
},
title: {
credits: false,
text: '',
},
xAxis: {
gridLineWidth: 1,
labels: {
format: '{value}%'
},
type: 'logarithmic',
reversed: false,
min: 0.1,
max:90,
title: {
enabled: true,
text: 'Fraction Failure'
},
startOnTick: true,
endOnTick: true,
showLastLabel: true
},
yAxis: {
gridLineWidth: 1,
max: 999,
min: 10,
type: 'logarithmic',
title: {
text: 'Days to Failure'
}
},
legend: {
credits: false,
layout: 'vertical',
align: 'left',
verticalAlign: 'top',
floating: true,
borderWidth: 1
},
series: [
{
color:'#0000FF',
showInLegend:false,
type: 'line',
data: weibullresp.averages,
zIndex: 1,
marker: {
lineWidth: 2,
}},
{
showInLegend:false,
data: weibullresp.ranges,
type: 'arearange',
lineWidth: 0,
linkedTo: ':previous',
fillOpacity: 0.3,
zIndex: 0,
marker: {
enabled: false
}},
{
showInLegend:false,
type:'scatter',
data: weibullresp.scatterData,
color:'#343434',
marker: {
radius: 2,
symbol: 'circle'
}
}
],
credits: {
enabled: false,
},
} as any);
}
Is there a package for python where Weibull plot can be created?
How to fix the y-axis in Highchart?
Kindly help me fix the y-axis in Highchart or Let me know how can i achieve the the python image shared in front end -Angular. I need exactly same image in Python
Try to change tickInterval, minorTickInterval on axis.
On logarithmic axes, the unit is the power of the value. For example, setting the minorTickInterval to 1 puts one tick on each of 0.1, 1, 10, 100 etc. Setting the minorTickInterval to 0.1 produces 9 ticks between 1 and 10, 10 and 100 etc.
Highcharts.chart('container', {
title: {
text: 'Logarithmic axis demo'
},
xAxis: {
tickInterval: 1,
type: 'logarithmic',
},
yAxis: {
type: 'logarithmic',
minorTickInterval: 0.1,
},
tooltip: {
headerFormat: '<b>{series.name}</b><br />',
pointFormat: 'x = {point.x}, y = {point.y}'
},
series: [{
data: [1, 2, 4, 8, 16, 32, 64, 128, 256, 512],
pointStart: 1
}]
});
Demo: https://jsfiddle.net/BlackLabel/x21uc5sd/

Using Datalabels and ChartJS with Chartjs-node-canvas

I use NodeJS with ChartJS to render graph directly to a file and it works fine.
I do it by ChartjsNodeCanvas npm module installed.
In order to display labels on the graph I use official ChartJS plugin ChartJS-plugin-datalabels
But it seems like Canvas module has a different scheme of activating ChartJS plugins and most probably I activate datalabels plugin incorrectly. So I can’t get the labels displayed on the graph.
const Chart = require('chart.js');
const datalabels = require('chartjs-plugin-datalabels')
const { CanvasRenderService } = require('chartjs-node-canvas');
const width = 600;
const height = 400;
const chartCallback = (ChartJS) => {
ChartJS.defaults.global.elements.rectangle.borderWidth = 2;
ChartJS.plugins.register({
datalabels
});
};
const canvasRenderService = new CanvasRenderService(width, height, chartCallback);
(async () => {
const configuration = {
type: 'bar',
data: {
datasets: [
{
type:"bar",
barThickness: 24,
label: 'My dataset',
data: ydata['data'],
backgroundColor: 'rgba(75,192, 192, 0.2)',
borderColor: 'rgba(54, 162, 135, 0.2)',
borderWidth: 1,
datalabels: {
align: 'start',
anchor: 'start'
}
},
labels: xdata,
}
options: {
scales: {
yAxes: [
{
id: 'left-y-axis',
position: 'left',
stacked: false,
ticks: {
beginAtZero: true,
callback: (value) => value + "R"
},
},
{
id: 'right-y-axis',
position: 'right'
}
],
xAxes: [{
stacked: true,
}]
},
plugins: {
datalabels: {
backgroundColor: function(context) {
return context.dataset.backgroundColor;
},
borderRadius: 4,
color: 'red',
font: {
weight: 'bold'
},
formatter: Math.round
}
}
}
};
const image = await canvasRenderService.renderToBuffer(configuration);
fs.writeFileSync(tgMsgPath+"test.png", image)
})();
The chart is shown but no labels on the graph.
This is how to activate plugins with ChartJs-niode-canvas:
const chartJsFactory = () => {
const Chart = require('chart.js');
require('chartjs-plugin-datalabels');
delete require.cache[require.resolve('chart.js')];
delete require.cache[require.resolve('chartjs-plugin-datalabels')];
return Chart;
}
// ...
const canvasRenderService = new CanvasRenderService(
chartWidth,
chartHeight,
chartCallback,
undefined,
chartJsFactory
);

Add tween to scene but do not play it inmediately

I'm adding tweens to scene using scene.add.tween({config}) and the added tween plays automatically. How can I prevent this?
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 200 }
}
},
scene: {
preload: preload,
create: create
}
};
var game = new Phaser.Game(config);
function preload ()
{
this.load.setBaseURL('http://labs.phaser.io');
this.load.image('logo', 'assets/sprites/phaser3-logo.png');
}
function create ()
{
var logo = this.add.image(config.width / 2, config.height / 2, 'logo');
var tween_logo = this.add.tween({
targets: logo,
duration: 1000,
scale: 2,
ease: 'Linear',
yoyo: true,
repeat: -1
});
}
<script src="https://cdn.jsdelivr.net/gh/photonstorm/phaser#3.18.1/dist/phaser.min.js"></script>
In your tween config add the paused property and set it to true.
So, your tween config would be:
var tween_logo = this.add.tween({
targets: logo,
duration: 1000,
paused: true,
scale: 2,
ease: 'Linear',
yoyo: true,
repeat: -1
});
Here is a link to the docs for the config to see other properties:

Determine The Area Of A Point based on Background Color in Chart JS

As shown in the pictures below, I have a chart created with 'Chart JS'. This chart contains linear lines with various curves. I painted the areas between these lines with 'background color' in various colors.
My question is, when I create a random point on this chart, I just want to create an if statement based on colors.
For example,
There is the point called A and the variable called B.
If A point is in the yellow area, then B is 1.
If A point is in the light gray area, then B is 2.
Something like this.
Here is Picture:
Here is sample code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js#2.8.0"></script>
<script type="text/javascript">
var x_A = new Array(2, 2, 2.5, 2.5, 2);
var y_A = new Array(2.5 , 3 , 3.5, 2.5 , 2.5);
var c_A = [];
for (var i = 0; i < x_A.length; i++) {
var obj = { x: x_A[i], y: y_A[i] };
c_A.push(obj);
}
var x_B = new Array(0, 1.5, 1.5, 0);
var y_B = new Array(0, 2, 0.5, 0);
var c_B = [];
for (var i = 0; i < x_B.length; i++) {
var obj = { x: x_B[i], y: y_B[i] };
c_B.push(obj);
}
var x_C = new Array(1.5, 1.5, 2, 2, 1.5);
var y_C = new Array(0.5, 2, 3, 2.5, 0.5);
var c_C = [];
for (var i = 0; i < x_C.length; i++) {
var obj = { x: x_C[i], y: y_C[i] };
c_C.push(obj);
}
var r_A = 'rgba(220, 20, 60,1)';
var r_B = 'rgba(255, 255, 56,1)';
var r_C = 'rgba(34, 139, 34,1)';
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [
{
label: 'X',
showLine: true,
fill: true,
pointRadius: 7,
data: [{
x: 1,
y: 0.9
}],
borderWidth: 1,
pointBackgroundColor: "#000000",
lineTension: 0,
},
{
label:'Zone A',
showLine: true,
fill: true,
backgroundColor: r_A,
pointRadius: 0,
data: c_A,
borderWidth: 1,
borderColor: "#000000",
lineTension: 0,
},
{
label: 'Zone B',
showLine: true,
fill: true,
backgroundColor: r_B,
pointRadius: 0,
data: c_B,
borderWidth: 1,
borderColor: "#000000",
lineTension: 0,
},
{
label: 'Zone C',
showLine: true,
fill: true,
backgroundColor: r_C,
pointRadius: 0,
data: c_C,
borderWidth: 1,
borderColor: "#000000",
lineTension: 0,
},
]
},
options: {
title: {
display: false,
},
tooltips: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
type: 'linear',
position: 'bottom',
gridLines:
{
display: false
},
scaleLabel: {
display: true
}
}],
yAxes: [{
gridLines:
{
display: false
},
scaleLabel: {
display: true,
}
}]
}
}
});
</script>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<html>
<body>
<div class="w3-row">
<div class="w3-col s3"><p></p></div>
<div class="w3-col s6" align="center"><canvas id="myChart" height="220"></canvas></div>
</div>
<div class="w3-row" align="center"><h2>Color Zone where the point X is located: </h2></div>
</body>
</html>
Here is CodePen:
My Sample Example: CodePen

gridline/ticklines in dashed in canvas in flot.js

How can I create dashed lines in both x-axis and y-axis in flot.js? I don't know where to change in library. This is my code Of generated graph on canvas using flot.js. But I want the Dashed Lines on x-axis and y-axis on grid background in canvas. I try markingsStyle: 'dashed' but that did not work.
plot = $.plot("#placeholder", [{
dashes: { show: true },
data: data,
color: "rgb(255, 255, 255)",
label: $("#hidsymbolid").val(),
}, {
data: dataBaseLine,
color: "rgb(255, 255, 255)",
lineStyle: "dashed"
}
], {
series: {
lines: {
lineWidth: 1,
show: true,
lineJoin: "round",
lineCap: "round"
},
points: {
show: false
}
},
grid: {
hoverable: true,
backgroundColor: {
colors: ["#3B3B3B", "#3B3B3B"]
},
markingsStyle: 'dashed'
},
xaxis: {
show: true,
tickSize: parseFloat((maxvalue - minvalue) / 6),
min: minvalue,
max: maxvalue,
dashes: { show: true },
timeformat: "%H:%M:%S",
tickColor: "rgb(255, 255, 255)",
},
yaxis: {
show: true,
dashes: { show: true },
color: "rgb(255, 255, 255)",
alignTicksWithAxis: 2
}
});
Please try this.
setLineDash([5])
Example:-
var context = document.querySelector("canvas");
var cxt = context..getContext('2d');
cxt.beginPath();
cxt.setLineDash([9]);
cxt.rect(30,15,100,100);
cxt.stroke();

Resources