import * as PIXI from 'pixi.js';
class GradientResource extends PIXI.Resource
{
constructor()
{
super(256, 1);
}
upload(renderer, baseTexture, glTexture)
{
const { width } = this;
const { height } = this;
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
const grd = ctx.createLinearGradient(0, 0, width, 0);
grd.addColorStop(0, 'rgba(255, 255, 255, 0.0)');
grd.addColorStop(0.3, 'cyan');
grd.addColorStop(0.7, 'red');
grd.addColorStop(1, 'green');
ctx.fillStyle = grd;
ctx.fillRect(0, 0, width, height);
glTexture.width = width;
glTexture.height = height;
const { gl } = renderer;
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, baseTexture.premultiplyAlpha);
gl.texImage2D(baseTexture.target, 0, baseTexture.format, baseTexture.format, baseTexture.type, canvas);
return true;
}
}
const app = new PIXI.Application({ antialias: true, resizeTo: window });
document.body.appendChild(app.view);
const gradBaseTexture = new PIXI.BaseTexture(new GradientResource());
gradBaseTexture.setSize(500, 50);
const gradTexture = new PIXI.Texture(gradBaseTexture);
const sprite = new PIXI.Sprite(gradTexture);
sprite.position.set(100, 100);
sprite.rotation = Math.PI / 8;
app.stage.addChild(sprite);