- 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
import { CanvasRenderTarget } from '@pixi/utils';
import { Rectangle } from '@pixi/math';
import { ExtensionType, RenderTexture } from '@pixi/core';
import type { Renderer, IRendererPlugin, ExtensionMetadata } from '@pixi/core';
import type { DisplayObject } from '@pixi/display';
const TEMP_RECT = new Rectangle();
const BYTES_PER_PIXEL = 4;
/**
* this interface is used to extract only a single pixel of Render Texture or Display Object
* if you use this Interface all fields is required
* @deprecated
* @example
* test: PixelExtractOptions = { x: 15, y: 20, resolution: 4, width: 10, height: 10 }
*/
export interface PixelExtractOptions
{
x: number,
y: number,
height: number,
resolution: number,
width: number
}
/**
* This class provides renderer-specific plugins for exporting content from a renderer.
* For instance, these plugins can be used for saving an Image, Canvas element or for exporting the raw image data (pixels).
*
* Do not instantiate these plugins directly. It is available from the `renderer.plugins` property.
* See {@link PIXI.CanvasRenderer#plugins} or {@link PIXI.Renderer#plugins}.
* @example
* // Create a new app (will auto-add extract plugin to renderer)
* const app = new PIXI.Application();
*
* // Draw a red circle
* const graphics = new PIXI.Graphics()
* .beginFill(0xFF0000)
* .drawCircle(0, 0, 50);
*
* // Render the graphics as an HTMLImageElement
* const image = app.renderer.plugins.extract.image(graphics);
* document.body.appendChild(image);
* @memberof PIXI
*/
export class Extract implements IRendererPlugin
{
/** @ignore */
static extension: ExtensionMetadata = {
name: 'extract',
type: ExtensionType.RendererPlugin,
};
private renderer: Renderer;
/**
* @param renderer - A reference to the current renderer
*/
constructor(renderer: Renderer)
{
this.renderer = renderer;
}
/**
* Will return a HTML Image of the target
* @param target - A displayObject or renderTexture
* to convert. If left empty will use the main renderer
* @param format - Image format, e.g. "image/jpeg" or "image/webp".
* @param quality - JPEG or Webp compression from 0 to 1. Default is 0.92.
* @returns - HTML Image of the target
*/
public image(target: DisplayObject | RenderTexture, format?: string, quality?: number): HTMLImageElement
{
const image = new Image();
image.src = this.base64(target, format, quality);
return image;
}
/**
* Will return a base64 encoded string of this target. It works by calling
* `Extract.getCanvas` and then running toDataURL on that.
* @param target - A displayObject or renderTexture
* to convert. If left empty will use the main renderer
* @param format - Image format, e.g. "image/jpeg" or "image/webp".
* @param quality - JPEG or Webp compression from 0 to 1. Default is 0.92.
* @returns - A base64 encoded string of the texture.
*/
public base64(target: DisplayObject | RenderTexture, format?: string, quality?: number): string
{
return this.canvas(target).toDataURL(format, quality);
}
/**
* Creates a Canvas element, renders this target to it and then returns it.
* @param target - A displayObject or renderTexture
* to convert. If left empty will use the main renderer
* @param frame - The frame the extraction is restricted to.
* @returns - A Canvas element with the texture rendered on.
*/
public canvas(target: DisplayObject | RenderTexture, frame?: Rectangle): HTMLCanvasElement
{
const renderer = this.renderer;
let resolution;
let flipY = false;
let renderTexture;
let generated = false;
if (target)
{
if (target instanceof RenderTexture)
{
renderTexture = target;
}
else
{
renderTexture = this.renderer.generateTexture(target);
generated = true;
}
}
if (renderTexture)
{
resolution = renderTexture.baseTexture.resolution;
frame = frame ?? renderTexture.frame;
flipY = false;
renderer.renderTexture.bind(renderTexture);
}
else
{
resolution = renderer.resolution;
if (!frame)
{
frame = TEMP_RECT;
frame.width = renderer.width;
frame.height = renderer.height;
}
flipY = true;
renderer.renderTexture.bind(null);
}
const width = Math.round(frame.width * resolution);
const height = Math.round(frame.height * resolution);
let canvasBuffer = new CanvasRenderTarget(width, height, 1);
const webglPixels = new Uint8Array(BYTES_PER_PIXEL * width * height);
// read pixels to the array
const gl = renderer.gl;
gl.readPixels(
Math.round(frame.x * resolution),
Math.round(frame.y * resolution),
width,
height,
gl.RGBA,
gl.UNSIGNED_BYTE,
webglPixels
);
// add the pixels to the canvas
const canvasData = canvasBuffer.context.getImageData(0, 0, width, height);
Extract.arrayPostDivide(webglPixels, canvasData.data);
canvasBuffer.context.putImageData(canvasData, 0, 0);
// pulling pixels
if (flipY)
{
const target = new CanvasRenderTarget(canvasBuffer.width, canvasBuffer.height, 1);
target.context.scale(1, -1);
// we can't render to itself because we should be empty before render.
target.context.drawImage(canvasBuffer.canvas, 0, -height);
canvasBuffer.destroy();
canvasBuffer = target;
}
if (generated)
{
renderTexture.destroy(true);
}
// send the canvas back..
return canvasBuffer.canvas;
}
/**
* Will return a one-dimensional array containing the pixel data of the entire texture in RGBA
* order, with integer values between 0 and 255 (included).
* @param target - A displayObject or renderTexture
* to convert. If left empty will use the main renderer
* @param frame - The frame the extraction is restricted to.
* @returns - One-dimensional array containing the pixel data of the entire texture
*/
public pixels(target?: DisplayObject | RenderTexture, frame?: Rectangle | PixelExtractOptions): Uint8Array
{
const renderer = this.renderer;
let resolution;
let renderTexture;
let generated = false;
if (target)
{
if (target instanceof RenderTexture)
{
renderTexture = target;
}
else
{
renderTexture = this.renderer.generateTexture(target);
generated = true;
}
}
if (renderTexture)
{
resolution = renderTexture.baseTexture.resolution;
frame = frame ?? renderTexture.frame;
renderer.renderTexture.bind(renderTexture);
}
else
{
resolution = renderer.resolution;
if (!frame)
{
frame = TEMP_RECT;
frame.width = renderer.width;
frame.height = renderer.height;
}
renderer.renderTexture.bind(null);
}
const width = Math.round(frame.width * resolution);
const height = Math.round(frame.height * resolution);
const webglPixels = new Uint8Array(BYTES_PER_PIXEL * width * height);
// read pixels to the array
const gl = renderer.gl;
gl.readPixels(
Math.round(frame.x * resolution),
Math.round(frame.y * resolution),
width,
height,
gl.RGBA,
gl.UNSIGNED_BYTE,
webglPixels
);
if (generated)
{
renderTexture.destroy(true);
}
Extract.arrayPostDivide(webglPixels, webglPixels);
return webglPixels;
}
/** Destroys the extract. */
public destroy(): void
{
this.renderer = null;
}
/**
* Takes premultiplied pixel data and produces regular pixel data
* @private
* @param pixels - array of pixel data
* @param out - output array
*/
static arrayPostDivide(
pixels: number[] | Uint8Array | Uint8ClampedArray, out: number[] | Uint8Array | Uint8ClampedArray
): void
{
for (let i = 0; i < pixels.length; i += 4)
{
const alpha = out[i + 3] = pixels[i + 3];
if (alpha !== 0)
{
out[i] = Math.round(Math.min(pixels[i] * 255.0 / alpha, 255.0));
out[i + 1] = Math.round(Math.min(pixels[i + 1] * 255.0 / alpha, 255.0));
out[i + 2] = Math.round(Math.min(pixels[i + 2] * 255.0 / alpha, 255.0));
}
else
{
out[i] = pixels[i];
out[i + 1] = pixels[i + 1];
out[i + 2] = pixels[i + 2];
}
}
}
}