-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
canvasUtils.ts
312 lines (256 loc) · 9.3 KB
/
canvasUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { settings, utils } from '@pixi/core';
import { canUseNewCanvasBlendModes } from './utils/canUseNewCanvasBlendModes';
import type { ICanvas, Texture } from '@pixi/core';
/**
* Utility methods for Sprite/Texture tinting.
*
* Tinting with the CanvasRenderer involves creating a new canvas to use as a texture,
* so be aware of the performance implications.
* @namespace PIXI.canvasUtils
* @memberof PIXI
*/
export const canvasUtils = {
canvas: null as ICanvas,
/**
* Basically this method just needs a sprite and a color and tints the sprite with the given color.
* @memberof PIXI.canvasUtils
* @param {PIXI.Sprite} sprite - the sprite to tint
* @param sprite.texture
* @param {number} color - the color to use to tint the sprite with
* @returns {ICanvas|HTMLImageElement} The tinted canvas
*/
getTintedCanvas: (sprite: { texture: Texture }, color: number): ICanvas | HTMLImageElement =>
{
const texture = sprite.texture;
color = canvasUtils.roundColor(color);
const stringColor = `#${(`00000${(color | 0).toString(16)}`).slice(-6)}`;
texture.tintCache = texture.tintCache || {};
const cachedCanvas = texture.tintCache[stringColor];
let canvas: ICanvas;
if (cachedCanvas)
{
if (cachedCanvas.tintId === texture._updateID)
{
return texture.tintCache[stringColor];
}
canvas = texture.tintCache[stringColor] as ICanvas;
}
else
{
canvas = settings.ADAPTER.createCanvas();
}
canvasUtils.tintMethod(texture, color, canvas);
canvas.tintId = texture._updateID;
// Convert tint to image only if ICanvas.toDataURL exists (e.g. OffscreenCanvas don't have toDataURL)
if (canvasUtils.convertTintToImage && canvas.toDataURL !== undefined)
{
// is this better?
const tintImage = new Image();
tintImage.src = canvas.toDataURL();
texture.tintCache[stringColor] = tintImage;
}
else
{
texture.tintCache[stringColor] = canvas;
}
return canvas;
},
/**
* Basically this method just needs a sprite and a color and tints the sprite with the given color.
* @memberof PIXI.canvasUtils
* @param {PIXI.Texture} texture - the sprite to tint
* @param {number} color - the color to use to tint the sprite with
* @returns {CanvasPattern} The tinted canvas
*/
getTintedPattern: (texture: Texture, color: number): CanvasPattern =>
{
color = canvasUtils.roundColor(color);
const stringColor = `#${(`00000${(color | 0).toString(16)}`).slice(-6)}`;
texture.patternCache = texture.patternCache || {};
let pattern = texture.patternCache[stringColor];
if (pattern?.tintId === texture._updateID)
{
return pattern;
}
if (!canvasUtils.canvas)
{
canvasUtils.canvas = settings.ADAPTER.createCanvas();
}
canvasUtils.tintMethod(texture, color, canvasUtils.canvas);
pattern = canvasUtils.canvas.getContext('2d').createPattern(canvasUtils.canvas, 'repeat');
pattern.tintId = texture._updateID;
texture.patternCache[stringColor] = pattern;
return pattern;
},
/**
* Tint a texture using the 'multiply' operation.
* @memberof PIXI.canvasUtils
* @param {PIXI.Texture} texture - the texture to tint
* @param {number} color - the color to use to tint the sprite with
* @param {PIXI.ICanvas} canvas - the current canvas
*/
tintWithMultiply: (texture: Texture, color: number, canvas: ICanvas): void =>
{
const context = canvas.getContext('2d');
const crop = texture._frame.clone();
const resolution = texture.baseTexture.resolution;
crop.x *= resolution;
crop.y *= resolution;
crop.width *= resolution;
crop.height *= resolution;
canvas.width = Math.ceil(crop.width);
canvas.height = Math.ceil(crop.height);
context.save();
context.fillStyle = `#${(`00000${(color | 0).toString(16)}`).slice(-6)}`;
context.fillRect(0, 0, crop.width, crop.height);
context.globalCompositeOperation = 'multiply';
const source = texture.baseTexture.getDrawableSource();
context.drawImage(
source,
crop.x,
crop.y,
crop.width,
crop.height,
0,
0,
crop.width,
crop.height
);
context.globalCompositeOperation = 'destination-atop';
context.drawImage(
source,
crop.x,
crop.y,
crop.width,
crop.height,
0,
0,
crop.width,
crop.height
);
context.restore();
},
/**
* Tint a texture using the 'overlay' operation.
* @memberof PIXI.canvasUtils
* @param {PIXI.Texture} texture - the texture to tint
* @param {number} color - the color to use to tint the sprite with
* @param {PIXI.ICanvas} canvas - the current canvas
*/
tintWithOverlay: (texture: Texture, color: number, canvas: ICanvas): void =>
{
const context = canvas.getContext('2d');
const crop = texture._frame.clone();
const resolution = texture.baseTexture.resolution;
crop.x *= resolution;
crop.y *= resolution;
crop.width *= resolution;
crop.height *= resolution;
canvas.width = Math.ceil(crop.width);
canvas.height = Math.ceil(crop.height);
context.save();
context.globalCompositeOperation = 'copy';
context.fillStyle = `#${(`00000${(color | 0).toString(16)}`).slice(-6)}`;
context.fillRect(0, 0, crop.width, crop.height);
context.globalCompositeOperation = 'destination-atop';
context.drawImage(
texture.baseTexture.getDrawableSource(),
crop.x,
crop.y,
crop.width,
crop.height,
0,
0,
crop.width,
crop.height
);
// context.globalCompositeOperation = 'copy';
context.restore();
},
/**
* Tint a texture pixel per pixel.
* @memberof PIXI.canvasUtils
* @param {PIXI.Texture} texture - the texture to tint
* @param {number} color - the color to use to tint the sprite with
* @param {PIXI.ICanvas} canvas - the current canvas
*/
tintWithPerPixel: (texture: Texture, color: number, canvas: ICanvas): void =>
{
const context = canvas.getContext('2d');
const crop = texture._frame.clone();
const resolution = texture.baseTexture.resolution;
crop.x *= resolution;
crop.y *= resolution;
crop.width *= resolution;
crop.height *= resolution;
canvas.width = Math.ceil(crop.width);
canvas.height = Math.ceil(crop.height);
context.save();
context.globalCompositeOperation = 'copy';
context.drawImage(
texture.baseTexture.getDrawableSource(),
crop.x,
crop.y,
crop.width,
crop.height,
0,
0,
crop.width,
crop.height
);
context.restore();
const rgbValues = utils.hex2rgb(color);
const r = rgbValues[0];
const g = rgbValues[1];
const b = rgbValues[2];
const pixelData = context.getImageData(0, 0, crop.width, crop.height);
const pixels = pixelData.data;
for (let i = 0; i < pixels.length; i += 4)
{
pixels[i + 0] *= r;
pixels[i + 1] *= g;
pixels[i + 2] *= b;
}
context.putImageData(pixelData, 0, 0);
},
/**
* Rounds the specified color according to the canvasUtils.cacheStepsPerColorChannel.
* @memberof PIXI.canvasUtils
* @param {number} color - the color to round, should be a hex color
* @returns {number} The rounded color.
*/
roundColor: (color: number): number =>
{
const step = canvasUtils.cacheStepsPerColorChannel;
const rgbValues = utils.hex2rgb(color);
rgbValues[0] = Math.min(255, (rgbValues[0] / step) * step);
rgbValues[1] = Math.min(255, (rgbValues[1] / step) * step);
rgbValues[2] = Math.min(255, (rgbValues[2] / step) * step);
return utils.rgb2hex(rgbValues);
},
/**
* Number of steps which will be used as a cap when rounding colors.
* @memberof PIXI.canvasUtils
* @type {number}
*/
cacheStepsPerColorChannel: 8,
/**
* Tint cache boolean flag.
* @memberof PIXI.canvasUtils
* @type {boolean}
*/
convertTintToImage: false,
/**
* Whether or not the Canvas BlendModes are supported, consequently the ability to tint using the multiply method.
* @memberof PIXI.canvasUtils
* @type {boolean}
*/
canUseMultiply: canUseNewCanvasBlendModes(),
/**
* The tinting method that will be used.
* @memberof PIXI.canvasUtils
* @type {Function}
*/
tintMethod: null as (texture: Texture, color: number, canvas: ICanvas) => void,
};
canvasUtils.tintMethod = canvasUtils.canUseMultiply ? canvasUtils.tintWithMultiply : canvasUtils.tintWithPerPixel;