- 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
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
import {
AbstractRenderer,
CanvasResource,
RenderTexture,
BaseRenderTexture,
extensions,
ExtensionType } from '@pixi/core';
import { CanvasRenderTarget, sayHello, rgb2hex, hex2string, deprecation } from '@pixi/utils';
import { CanvasMaskManager } from './utils/CanvasMaskManager';
import { mapCanvasBlendModesToPixi } from './utils/mapCanvasBlendModesToPixi';
import { RENDERER_TYPE, SCALE_MODES, BLEND_MODES } from '@pixi/constants';
import { settings } from '@pixi/settings';
import { Matrix } from '@pixi/math';
import type { DisplayObject } from '@pixi/display';
import type {
IRendererOptions,
IRendererPlugin,
IRendererPlugins,
IRendererRenderOptions
} from '@pixi/core';
const tempMatrix = new Matrix();
export interface ICanvasRendererPluginConstructor
{
new (renderer: CanvasRenderer, options?: any): IRendererPlugin;
}
export interface ICanvasRendererPlugins
{
[key: string]: any;
}
/*
* Different browsers support different smoothing property names
* this is the list of all platform props.
*/
type SmoothingEnabledProperties =
'imageSmoothingEnabled' |
'webkitImageSmoothingEnabled' |
'mozImageSmoothingEnabled' |
'oImageSmoothingEnabled' |
'msImageSmoothingEnabled';
/**
* Rendering context for all browsers. This includes platform-specific
* properties that are not included in the spec for CanvasRenderingContext2D
* @private
*/
export interface CrossPlatformCanvasRenderingContext2D extends CanvasRenderingContext2D
{
webkitImageSmoothingEnabled: boolean;
mozImageSmoothingEnabled: boolean;
oImageSmoothingEnabled: boolean;
msImageSmoothingEnabled: boolean;
}
/**
* The CanvasRenderer draws the scene and all its content onto a 2d canvas.
*
* This renderer should be used for browsers that do not support WebGL.
* Don't forget to add the CanvasRenderer.view to your DOM or you will not see anything!
* @class
* @memberof PIXI
* @extends PIXI.AbstractRenderer
*/
export class CanvasRenderer extends AbstractRenderer
{
/**
* Fired after rendering finishes.
* @event PIXI.CanvasRenderer#postrender
*/
/**
* Fired before rendering starts.
* @event PIXI.CanvasRenderer#prerender
*/
/** The root canvas 2d context that everything is drawn with. */
public readonly rootContext: CrossPlatformCanvasRenderingContext2D;
/** The currently active canvas 2d context (could change with renderTextures) */
public context: CrossPlatformCanvasRenderingContext2D;
/** Boolean flag controlling canvas refresh. */
public refresh = true;
/**
* Instance of a CanvasMaskManager, handles masking when using the canvas renderer.
* @member {PIXI.CanvasMaskManager}
*/
public maskManager: CanvasMaskManager = new CanvasMaskManager(this);
/** The canvas property used to set the canvas smoothing property. */
public smoothProperty: SmoothingEnabledProperties = 'imageSmoothingEnabled';
/** Tracks the blend modes useful for this renderer. */
public readonly blendModes: string[] = mapCanvasBlendModesToPixi();
public renderingToScreen = false;
private _activeBlendMode: BLEND_MODES = null;
/** Projection transform, passed in render() stored here */
private _projTransform: Matrix = null;
/** @private */
_outerBlend = false;
/**
* @param options - The optional renderer parameters
* @param {number} [options.width=800] - the width of the screen
* @param {number} [options.height=600] - the height of the screen
* @param {HTMLCanvasElement} [options.view] - the canvas to use as a view, optional
* @param {boolean} [options.useContextAlpha=true] - Pass-through value for canvas' context `alpha` property.
* If you want to set transparency, please use `backgroundAlpha`. This option is for cases where the
* canvas needs to be opaque, possibly for performance reasons on some older devices.
* @param {boolean} [options.autoDensity=false] - Resizes renderer view in CSS pixels to allow for
* resolutions other than 1
* @param {boolean} [options.antialias=false] - sets antialias
* @param {number} [options.resolution=PIXI.settings.RESOLUTION] - The resolution / device pixel ratio of the renderer.
* @param {boolean} [options.preserveDrawingBuffer=false] - enables drawing buffer preservation,
* enable this if you need to call toDataUrl on the webgl context.
* @param {boolean} [options.clearBeforeRender=true] - This sets if the renderer will clear the canvas or
* not before the new render pass.
* @param {number} [options.backgroundColor=0x000000] - The background color of the rendered area
* (shown if not transparent).
* @param {number} [options.backgroundAlpha=1] - Value from 0 (fully transparent) to 1 (fully opaque).
*/
constructor(options?: IRendererOptions)
{
super(RENDERER_TYPE.CANVAS, options);
this.rootContext = this.view.getContext('2d', { alpha: this.useContextAlpha }) as
CrossPlatformCanvasRenderingContext2D;
this.context = this.rootContext;
if (!this.rootContext.imageSmoothingEnabled)
{
const rc = this.rootContext;
if (rc.webkitImageSmoothingEnabled)
{
this.smoothProperty = 'webkitImageSmoothingEnabled';
}
else if (rc.mozImageSmoothingEnabled)
{
this.smoothProperty = 'mozImageSmoothingEnabled';
}
else if (rc.oImageSmoothingEnabled)
{
this.smoothProperty = 'oImageSmoothingEnabled';
}
else if (rc.msImageSmoothingEnabled)
{
this.smoothProperty = 'msImageSmoothingEnabled';
}
}
this.initPlugins(CanvasRenderer.__plugins);
sayHello('Canvas');
this.resize(this.options.width, this.options.height);
}
/** Adds a new system to the renderer. It does nothing in the CanvasRenderer. */
addSystem(): this
{
return this;
}
/**
* Renders the object to its WebGL view.
* @param displayObject - The object to be rendered.
* @param options - Object to use for render options.
* @param {PIXI.RenderTexture} [options.renderTexture] - The render texture to render to.
* @param {boolean} [options.clear=true] - Should the canvas be cleared before the new render.
* @param {PIXI.Matrix} [options.transform] - A transform to apply to the render texture before rendering.
* @param {boolean} [options.skipUpdateTransform=false] - Should we skip the update transform pass?
*/
render(displayObject: DisplayObject, options?: IRendererRenderOptions): void;
/**
* Please use the `option` render arguments instead.
* @deprecated Since 6.0.0
* @param displayObject - The object to be rendered.
* @param renderTexture - The render texture to render to.
* @param clear - Should the canvas be cleared before the new render.
* @param transform - A transform to apply to the render texture before rendering.
* @param skipUpdateTransform - Should we skip the update transform pass?
*/
render(displayObject: DisplayObject, renderTexture?: RenderTexture | BaseRenderTexture,
clear?: boolean, transform?: Matrix, skipUpdateTransform?: boolean): void;
/** @ignore */
public render(displayObject: DisplayObject, options?: IRendererRenderOptions | RenderTexture | BaseRenderTexture): void
{
if (!this.view)
{
return;
}
let renderTexture: BaseRenderTexture | RenderTexture;
let clear: boolean;
let transform: Matrix;
let skipUpdateTransform: boolean;
if (options)
{
if (options instanceof RenderTexture || options instanceof BaseRenderTexture)
{
// #if _DEBUG
deprecation('6.0.0', 'CanvasRenderer#render arguments changed, use options instead.');
// #endif
/* eslint-disable prefer-rest-params */
renderTexture = options;
clear = arguments[2];
transform = arguments[3];
skipUpdateTransform = arguments[4];
/* eslint-enable prefer-rest-params */
}
else
{
renderTexture = options.renderTexture;
clear = options.clear;
transform = options.transform;
skipUpdateTransform = options.skipUpdateTransform;
}
}
// can be handy to know!
this.renderingToScreen = !renderTexture;
this.emit('prerender');
const rootResolution = this.resolution;
if (renderTexture)
{
renderTexture = renderTexture.castToBaseTexture() as BaseRenderTexture;
if (!renderTexture._canvasRenderTarget)
{
renderTexture._canvasRenderTarget = new CanvasRenderTarget(
renderTexture.width,
renderTexture.height,
renderTexture.resolution
);
renderTexture.resource = new CanvasResource(renderTexture._canvasRenderTarget.canvas);
renderTexture.valid = true;
}
this.context = renderTexture._canvasRenderTarget.context as CrossPlatformCanvasRenderingContext2D;
this.resolution = renderTexture._canvasRenderTarget.resolution;
}
else
{
this.context = this.rootContext;
}
const context = this.context;
this._projTransform = transform || null;
if (!renderTexture)
{
this._lastObjectRendered = displayObject;
}
if (!skipUpdateTransform)
{
// update the scene graph
const cacheParent = displayObject.enableTempParent();
displayObject.updateTransform();
displayObject.disableTempParent(cacheParent);
}
context.save();
context.setTransform(1, 0, 0, 1, 0, 0);
context.globalAlpha = 1;
this._activeBlendMode = BLEND_MODES.NORMAL;
this._outerBlend = false;
context.globalCompositeOperation = this.blendModes[BLEND_MODES.NORMAL];
if (clear !== undefined ? clear : this.clearBeforeRender)
{
if (this.renderingToScreen)
{
context.clearRect(0, 0, this.width, this.height);
if (this.backgroundAlpha > 0)
{
context.globalAlpha = this.useContextAlpha ? this.backgroundAlpha : 1;
context.fillStyle = this._backgroundColorString;
context.fillRect(0, 0, this.width, this.height);
context.globalAlpha = 1;
}
}
else
{
renderTexture = (renderTexture as BaseRenderTexture);
renderTexture._canvasRenderTarget.clear();
const clearColor = renderTexture.clearColor;
if (clearColor[3] > 0)
{
context.globalAlpha = this.useContextAlpha ? clearColor[3] : 1;
context.fillStyle = hex2string(rgb2hex(clearColor));
context.fillRect(0, 0, renderTexture.realWidth, renderTexture.realHeight);
context.globalAlpha = 1;
}
}
}
// TODO RENDER TARGET STUFF HERE..
const tempContext = this.context;
this.context = context;
displayObject.renderCanvas(this);
this.context = tempContext;
context.restore();
this.resolution = rootResolution;
this._projTransform = null;
this.emit('postrender');
}
/**
* Sets matrix of context.
* called only from render() methods
* takes care about resolution
* @param transform - world matrix of current element
* @param roundPixels - whether to round (tx,ty) coords
* @param localResolution - If specified, used instead of `renderer.resolution` for local scaling
*/
setContextTransform(transform: Matrix, roundPixels?: boolean, localResolution?: number): void
{
let mat = transform;
const proj = this._projTransform;
const resolution = this.resolution;
localResolution = localResolution || resolution;
if (proj)
{
mat = tempMatrix;
mat.copyFrom(transform);
mat.prepend(proj);
}
if (roundPixels)
{
this.context.setTransform(
mat.a * localResolution,
mat.b * localResolution,
mat.c * localResolution,
mat.d * localResolution,
(mat.tx * resolution) | 0,
(mat.ty * resolution) | 0
);
}
else
{
this.context.setTransform(
mat.a * localResolution,
mat.b * localResolution,
mat.c * localResolution,
mat.d * localResolution,
mat.tx * resolution,
mat.ty * resolution
);
}
}
/**
* Clear the canvas of renderer.
* @param {string} [clearColor] - Clear the canvas with this color, except the canvas is transparent.
* @param {number} [alpha] - Alpha to apply to the background fill color.
*/
public clear(clearColor: string = this._backgroundColorString, alpha: number = this.backgroundAlpha): void
{
const { context } = this;
context.clearRect(0, 0, this.width, this.height);
if (clearColor)
{
context.globalAlpha = this.useContextAlpha ? alpha : 1;
context.fillStyle = clearColor;
context.fillRect(0, 0, this.width, this.height);
context.globalAlpha = 1;
}
}
/**
* Sets the blend mode of the renderer.
* @param {number} blendMode - See {@link PIXI.BLEND_MODES} for valid values.
* @param {boolean} [readyForOuterBlend=false] - Some blendModes are dangerous, they affect outer space of sprite.
* Pass `true` only if you are ready to use them.
*/
setBlendMode(blendMode: BLEND_MODES, readyForOuterBlend?: boolean): void
{
const outerBlend = blendMode === BLEND_MODES.SRC_IN
|| blendMode === BLEND_MODES.SRC_OUT
|| blendMode === BLEND_MODES.DST_IN
|| blendMode === BLEND_MODES.DST_ATOP;
if (!readyForOuterBlend && outerBlend)
{
blendMode = BLEND_MODES.NORMAL;
}
if (this._activeBlendMode === blendMode)
{
return;
}
this._activeBlendMode = blendMode;
this._outerBlend = outerBlend;
this.context.globalCompositeOperation = this.blendModes[blendMode];
}
/**
* Removes everything from the renderer and optionally removes the Canvas DOM element.
* @param {boolean} [removeView=false] - Removes the Canvas element from the DOM.
*/
public destroy(removeView?: boolean): void
{
// call the base destroy
super.destroy(removeView);
this.context = null;
this.refresh = true;
this.maskManager.destroy();
this.maskManager = null;
this.smoothProperty = null;
}
/**
* Resizes the canvas view to the specified width and height.
* @extends PIXI.AbstractRenderer#resize
* @param desiredScreenWidth - the desired width of the screen
* @param desiredScreenHeight - the desired height of the screen
*/
public resize(desiredScreenWidth: number, desiredScreenHeight: number): void
{
super.resize(desiredScreenWidth, desiredScreenHeight);
// reset the scale mode.. oddly this seems to be reset when the canvas is resized.
// surely a browser bug?? Let PixiJS fix that for you..
if (this.smoothProperty)
{
this.rootContext[this.smoothProperty] = (settings.SCALE_MODE === SCALE_MODES.LINEAR);
}
}
/** Checks if blend mode has changed. */
invalidateBlendMode(): void
{
this._activeBlendMode = this.blendModes.indexOf(this.context.globalCompositeOperation);
}
static __plugins: IRendererPlugins = {};
/**
* Collection of installed plugins. These are included by default in PIXI, but can be excluded
* by creating a custom build. Consult the README for more information about creating custom
* builds and excluding plugins.
* @member {object} plugins
* @readonly
* @property {PIXI.AccessibilityManager} accessibility Support tabbing interactive elements.
* @property {PIXI.CanvasExtract} extract Extract image data from renderer.
* @property {PIXI.InteractionManager} interaction Handles mouse, touch and pointer events.
* @property {PIXI.CanvasPrepare} prepare Pre-render display objects.
*/
/**
* Use the {@link PIXI.extensions.add} API to register plugins.
* @deprecated since 6.5.0
* @param pluginName - The name of the plugin.
* @param ctor - The constructor function or class for the plugin.
*/
static registerPlugin(pluginName: string, ctor: ICanvasRendererPluginConstructor): void
{
// #if _DEBUG
deprecation('6.5.0', 'CanvasRenderer.registerPlugin() has been deprecated, please use extensions.add() instead.');
// #endif
extensions.add({
name: pluginName,
type: ExtensionType.CanvasRendererPlugin,
ref: ctor,
});
}
}
extensions.handleByMap(ExtensionType.CanvasRendererPlugin, CanvasRenderer.__plugins);