Jump to content

How to draw gradient color


fengFanYong
 Share

Recommended Posts

I am doing it by canvas, may be there is a better solution

    var canvas = document.createElement('canvas');
    canvas.width  = 200;
    canvas.height = 60;
    var ctx = canvas.getContext('2d');
    var gradient = ctx.createLinearGradient(0, 0, 0, 50);
    gradient.addColorStop(0, "#D3872A");
    gradient.addColorStop(1, "#CFB732");
    ctx.fillStyle = gradient;
    var sprite = new PIXI.Sprite(PIXI.Texture.fromCanvas(canvas));
    sprite.x = 341;
    sprite.y = 5;
    this.addChild(sprite);

 

Link to comment
Share on other sites

  • 11 months later...
  • 3 weeks later...
  • 9 months later...
On 2/8/2017 at 4:05 AM, PainKKKiller said:

I am doing it by canvas, may be there is a better solution


    var canvas = document.createElement('canvas');
    canvas.width  = 200;
    canvas.height = 60;
    var ctx = canvas.getContext('2d');
    var gradient = ctx.createLinearGradient(0, 0, 0, 50);
    gradient.addColorStop(0, "#D3872A");
    gradient.addColorStop(1, "#CFB732");
    ctx.fillStyle = gradient;
    var sprite = new PIXI.Sprite(PIXI.Texture.fromCanvas(canvas));
    sprite.x = 341;
    sprite.y = 5;
    this.addChild(sprite);

 

Note to future people looking at this thread, you do have to make a call to ctx.fillRect(). Without the call, nothing will be rendered in the sprite. 

ctx.fillStyle = gradient;
ctx.fillRect(x, y, width, height);

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createLinearGradient

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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