Skip to content

Instantly share code, notes, and snippets.

@Viroide
Created June 9, 2017 12:55
Show Gist options
  • Select an option

  • Save Viroide/b4ef0e53e40968e5787cc947fc08de9a to your computer and use it in GitHub Desktop.

Select an option

Save Viroide/b4ef0e53e40968e5787cc947fc08de9a to your computer and use it in GitHub Desktop.
import { PIXI } from "ingine/rendering";
export default class Gradient extends PIXI.Sprite {
constructor(resolution, color1, color2) {
super();
this.resolution = resolution;
this.createGradient(color1, color2);
}
createGradient(color1, color2) {
this.texture = this.createCanvasTexture(color1, color2);
this.anchor.set(0.5);
}
createCanvasTexture(color1, color2) {
let { canvasWidth, canvasHeight } = this.resolution.calculateSizes();
const multiplier = this.resolution.imageResolution;
canvasWidth = canvasWidth * multiplier;
canvasHeight = canvasHeight * multiplier;
let c=document.createElement("canvas");
c.width = canvasWidth ;
c.height = canvasHeight ;
let ctx=c.getContext("2d");
let grd=ctx.createLinearGradient(0, 0, canvasWidth, canvasHeight);
grd.addColorStop(0, color1);
grd.addColorStop(1, color2);
ctx.fillStyle=grd;
ctx.fillRect(0, 0, canvasWidth , canvasHeight );
return PIXI.Texture.fromCanvas(c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment