Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly resize images to retain quality in Phaser 3

I am making a game in Phaser 3. I have downloaded high quality (1100x1000px) images from the internet to use. But whenever I shrink them (to roughly 80x70px) they lose quality (either become pixelated or have a weird appearance). How do I correctly re-size the images to retain quality?

I re-sized the image using image.setDisplaySize() but the image looked weird. I am not sure why but the image contrasting has changed. Exact Code-

gameState.team_player_icon = gameState.screen_team_group.create(100, 130, (gameState.all_player_team[0].skin))

gameState.scale_size = gameState.team_player_icon.displayHeight/70
gameState.team_player_icon.setDisplaySize(gameState.team_player_icon.displayWidth/gameState.scale_size, 70)

I have already resized using canvas in paint editor 3 and the image looked fine but I want to know how to do it in Phaser 3 to save having to re-edit all the images for my game. Here it is below-

Phaser 3 image here-

enter image description here

Canvas Edited here-

enter image description here

In case it is not clear I want the image to look like the bottom image but using phaser 3. Thanks for any help in advance.

like image 838
Lachlan Dunn Avatar asked May 20 '19 11:05

Lachlan Dunn


People also ask

How do I resize an image without losing quality?

One way to do this is to use a program like Photoshop. With Photoshop, you can resize an image without losing quality by using the "Image Size" dialog box. In the "Image Size" dialog box, you can change the width and height of the image. You can also change the resolution.

Does resizing an image reduce quality?

Frequently asked questions: Does resizing an image affect its quality? It definitely can! Typically, making an image smaller will not impact the quality, but an image can suffer quality loss when scaled beyond its original size.

Which method will preserve aspect ratio when you resize a picture?

The Constrain Proportions option lets you maintain the aspect ratio (the ratio of image width to image height). If you select this option and change the image size and resolution, the image does not stretch or shrink.


2 Answers

var config = {
  type: Phaser.AUTO,
  width: 800,
  height: 600,
  loader: {
    baseURL: 'https://raw.githubusercontent.com/nazimboudeffa/assets/master/',
    crossOrigin: 'anonymous'
  },
  pixelArt: true,//here
  //antialias: false,
  scene: {
    preload: preload,
    create: create
  }
};


var game = new Phaser.Game(config);

function preload ()
{
    this.load.image('alien', 'sprites/phaser-alien.png');
}

function create ()
{
    sprite = this.add.image(50, 50, 'alien');

    sprite.setScale(2);

}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
like image 165
nazimboudeffa Avatar answered Oct 20 '22 08:10

nazimboudeffa


Use the scale attribute of the image:

const config = {
  type: Phaser.AUTO,
  width: 640,
  height: 480,
  loader: {
    baseURL: 'https://i.ibb.co/t8p496v/',
    crossOrigin: 'anonymous'
  },
  scene: {
    preload: preload,
    create: create
  }
};

const game = new Phaser.Game(config);

function preload() {
  this.load.image('mario', 'mario.png');
}

function create() {
  const mario = this.add.image(320, 240, 'mario');
  mario.scale = 0.5; // Resize the image
}
* { padding: 0; margin: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.24.1/phaser.min.js"></script>

The image that appears in the center of the screen is half of its original size.

If you don't want that Phaser.js console message to block the view in the example, don't worry! You can find a JSFiddle version here.

like image 45
mrwolferinc Avatar answered Oct 20 '22 10:10

mrwolferinc