Jump to content

Gradient Rectangle in Phaser 3


JesusJoseph
 Share

Recommended Posts

I need to create a sky color background. 

I am planning to do it using a gradient rectangle using below code but I am getting below error at LINE 2

HomeScene.js:68 Uncaught TypeError: Cannot read property 'createLinearGradient' of undefined

var texture = this.textures.createCanvas('gradient', 16, 256);
var grd = texture.context.createLinearGradient(0, 0, 0, 256);    // ERROR LINE

grd.addColorStop(0, '#8ED6FF');
grd.addColorStop(1, '#004CB3');

texture.context.fillStyle = grd;
texture.context.fillRect(0, 0, 16, 256);

//  Call this if running under WebGL, or you'll see nothing change
texture.refresh();

Can someone please tell me how to create a gradient images.

Thanks for your help.

Link to comment
Share on other sites

var texture = this.textures.createCanvas('gradient', 16, 256);
var context = texture.getContext();
var grd = context.createLinearGradient(0, 0, 0, 256);    // ERROR LINE

grd.addColorStop(0, '#8ED6FF');
grd.addColorStop(1, '#004CB3');

context.fillStyle = grd;
context.fillRect(0, 0, 16, 256);

//  Call this if running under WebGL, or you'll see nothing change
texture.refresh();

 

Link to comment
Share on other sites

15 minutes ago, YuPi said:

var texture = this.textures.createCanvas('gradient', 16, 256);
var context = texture.getContext();
var grd = context.createLinearGradient(0, 0, 0, 256);    // ERROR LINE

grd.addColorStop(0, '#8ED6FF');
grd.addColorStop(1, '#004CB3');

context.fillStyle = grd;
context.fillRect(0, 0, 16, 256);

//  Call this if running under WebGL, or you'll see nothing change
texture.refresh();

 

Thanks for your reply

 

Now  I am getting another error from the second line (var context = texture.getContext();) as below

HomeScene.js:66 Uncaught TypeError: texture.getContext is not a function
    at HomeScene.create (HomeScene.js:66)
    at SceneManager.create (phaser.js:41315)
    at SceneManager.bootScene (phaser.js:41169)

I am using Phaser 3.5.1 

Link to comment
Share on other sites

  • 2 weeks later...

Thanks YuPi

After I changed to version 3.10.1 then there is no error

But still, I didn't see the gradient rectangle in my screen.

In create function I have only below codes. But my screen is all black. Any help will be appreciated. 

var texture = this.textures.createCanvas('gradient', 200, 256);
var context = texture.getContext();
var grd = context.createLinearGradient(0, 0, game.config.width, game.config.height);    // ERROR LINE

grd.addColorStop(0, '#FFFFFF');
grd.addColorStop(1, '#004CB3');

context.fillStyle = grd;
context.fillRect(0, 0, game.config.width, game.config.height);

//  Call this if running under WebGL, or you'll see nothing change
texture.refresh();

//Second one
//----------------------------------------------------------------------------
var c=this.textures.createCanvas('gradient1', 200, 256);
var ctx=c.getContext("2d");

// Create gradient
var grd=ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");

// Fill with gradient
ctx.fillStyle=grd;
ctx.fillRect(100,100,150,80);

 

Link to comment
Share on other sites

  • 4 weeks later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...