1717 * License-Filename: LICENSE
1818 */
1919
20- import { Texture } from './Texture' ;
2120import { LinearGradient } from '@here/xyz-maps-core' ;
22-
23- type LinearGradientStops = LinearGradient [ 'stops' ] ;
24-
25- export class GradientTexture extends Texture {
26- ref : number = 0 ;
27- gradient : any ;
28- factory : GradientFactory ;
29-
30- destroy ( ) {
31- this . factory . dropTexture ( this ) ;
32- super . destroy ( ) ;
33- }
34- }
35-
36-
37- // let gradientTextCache = new Map();
38- // window._gradientTextCache = gradientTextCache;
39- // const defaultGradientConfig = {0: 'rgba(0, 0, 255, 0)', 0.1: 'royalblue', 0.3: 'cyan', 0.5: 'lime', 0.7: 'yellow', 1.0: 'red'};
40-
21+ import { FillTexture } from './TextureManager' ;
22+ export type LinearGradientStops = LinearGradient [ 'stops' ] ;
4123
4224export class GradientFactory {
4325 static canvas : HTMLCanvasElement = document . createElement ( 'canvas' ) ;
@@ -46,7 +28,6 @@ export class GradientFactory {
4628 private width : number ;
4729 private height : number ;
4830 private gl : WebGLRenderingContext ;
49- private cache : Map < LinearGradientStops , GradientTexture > = new Map ( ) ;
5031
5132 constructor ( gl : WebGLRenderingContext , width : number = 256 , height : number = 1 ) {
5233 this . gl = gl ;
@@ -58,47 +39,32 @@ export class GradientFactory {
5839 return typeof gradient ?. type == 'string' && typeof gradient . stops == 'object' ;
5940 }
6041
61- getTexture ( linearGradient : LinearGradientStops | LinearGradient , preprocessor ?: ( stops : LinearGradientStops ) => LinearGradientStops ) : GradientTexture {
42+ createTexture ( linearGradient : LinearGradientStops | LinearGradient , preprocessor ?: ( stops : LinearGradientStops ) => LinearGradientStops ) : FillTexture {
6243 const gradientStops : LinearGradientStops = this . isGradient ( linearGradient )
6344 ? ( linearGradient as LinearGradient ) . stops
6445 : linearGradient as LinearGradientStops ;
6546
66- let texture = this . cache . get ( gradientStops ) ;
67-
68- if ( ! texture ) {
69- const canvas = GradientFactory . canvas ;
70- const ctx = GradientFactory . ctx ;
71- const { width, height} = this ;
72- canvas . width = width ;
73- canvas . height = height ;
74-
75- const gradient = ctx . createLinearGradient ( 0 , 0 , width , height ) ;
47+ const canvas = GradientFactory . canvas ;
48+ const ctx = GradientFactory . ctx ;
49+ const { width, height} = this ;
50+ canvas . width = width ;
51+ canvas . height = height ;
7652
77- const stops = preprocessor ?. ( gradientStops ) || gradientStops ;
53+ const gradient = ctx . createLinearGradient ( 0 , 0 , width , height ) ;
7854
79- for ( var key in stops ) {
80- gradient . addColorStop ( Number ( key ) , stops [ key ] ) ;
81- }
82- // ctx.clearRect(0, 0, width, height);
83- ctx . fillStyle = gradient ;
84- ctx . fillRect ( 0 , 0 , width , height ) ;
85- // return paletteCtx.getImageData(0, 0, 256, 1).data;
55+ const stops = preprocessor ?.( gradientStops ) || gradientStops ;
8656
87- const data = ctx . getImageData ( 0 , 0 , width , height ) . data ;
88-
89- texture = new GradientTexture ( this . gl , { width, height, data} , { premultiplyAlpha : true } ) ;
90- texture . gradient = gradientStops ;
91- texture . factory = this ;
92-
93- this . cache . set ( gradientStops , texture ) ;
57+ for ( var key in stops ) {
58+ gradient . addColorStop ( Number ( key ) , stops [ key ] ) ;
9459 }
95- texture . ref ++ ;
60+ // ctx.clearRect(0, 0, width, height);
61+ ctx . fillStyle = gradient ;
62+ ctx . fillRect ( 0 , 0 , width , height ) ;
63+ // return paletteCtx.getImageData(0, 0, 256, 1).data;
9664
97- return texture ;
98- }
65+ const data = ctx . getImageData ( 0 , 0 , width , height ) . data ;
9966
100- dropTexture ( texture : GradientTexture ) {
101- this . cache . delete ( texture . gradient ) ;
67+ return new FillTexture ( this . gl , { width, height, data} , { premultiplyAlpha : true } ) ;
10268 }
10369}
10470
0 commit comments