|
1 | 1 | import * as constants from "../core/constants";
|
| 2 | +import { Graphics } from "../core/p5.Graphics"; |
2 | 3 | import { Renderer } from './p5.Renderer';
|
3 | 4 | import GeometryBuilder from "../webgl/GeometryBuilder";
|
4 | 5 | import { Matrix } from "../math/p5.Matrix";
|
@@ -350,6 +351,80 @@ export class Renderer3D extends Renderer {
|
350 | 351 | };
|
351 | 352 | }
|
352 | 353 |
|
| 354 | + //This is helper function to reset the context anytime the attributes |
| 355 | + //are changed with setAttributes() |
| 356 | + |
| 357 | + async _resetContext(options, callback, ctor = Renderer3D) { |
| 358 | + const w = this.width; |
| 359 | + const h = this.height; |
| 360 | + const defaultId = this.canvas.id; |
| 361 | + const isPGraphics = this._pInst instanceof Graphics; |
| 362 | + |
| 363 | + // Preserve existing position and styles before recreation |
| 364 | + const prevStyle = { |
| 365 | + position: this.canvas.style.position, |
| 366 | + top: this.canvas.style.top, |
| 367 | + left: this.canvas.style.left, |
| 368 | + }; |
| 369 | + |
| 370 | + if (isPGraphics) { |
| 371 | + // Handle PGraphics: remove and recreate the canvas |
| 372 | + const pg = this._pInst; |
| 373 | + pg.canvas.parentNode.removeChild(pg.canvas); |
| 374 | + pg.canvas = document.createElement("canvas"); |
| 375 | + const node = pg._pInst._userNode || document.body; |
| 376 | + node.appendChild(pg.canvas); |
| 377 | + Element.call(pg, pg.canvas, pg._pInst); |
| 378 | + // Restore previous width and height |
| 379 | + pg.width = w; |
| 380 | + pg.height = h; |
| 381 | + } else { |
| 382 | + // Handle main canvas: remove and recreate it |
| 383 | + let c = this.canvas; |
| 384 | + if (c) { |
| 385 | + c.parentNode.removeChild(c); |
| 386 | + } |
| 387 | + c = document.createElement("canvas"); |
| 388 | + c.id = defaultId; |
| 389 | + // Attach the new canvas to the correct parent node |
| 390 | + if (this._pInst._userNode) { |
| 391 | + this._pInst._userNode.appendChild(c); |
| 392 | + } else { |
| 393 | + document.body.appendChild(c); |
| 394 | + } |
| 395 | + this._pInst.canvas = c; |
| 396 | + this.canvas = c; |
| 397 | + |
| 398 | + // Restore the saved position |
| 399 | + this.canvas.style.position = prevStyle.position; |
| 400 | + this.canvas.style.top = prevStyle.top; |
| 401 | + this.canvas.style.left = prevStyle.left; |
| 402 | + } |
| 403 | + |
| 404 | + const renderer = new ctor( |
| 405 | + this._pInst, |
| 406 | + w, |
| 407 | + h, |
| 408 | + !isPGraphics, |
| 409 | + this._pInst.canvas |
| 410 | + ); |
| 411 | + this._pInst._renderer = renderer; |
| 412 | + |
| 413 | + renderer._applyDefaults(); |
| 414 | + |
| 415 | + if (renderer.contextReady) { |
| 416 | + await renderer.contextReady |
| 417 | + } |
| 418 | + |
| 419 | + if (typeof callback === "function") { |
| 420 | + //setTimeout with 0 forces the task to the back of the queue, this ensures that |
| 421 | + //we finish switching out the renderer |
| 422 | + setTimeout(() => { |
| 423 | + callback.apply(window._renderer, options); |
| 424 | + }, 0); |
| 425 | + } |
| 426 | + } |
| 427 | + |
353 | 428 | remove() {
|
354 | 429 | this.wrappedElt.remove();
|
355 | 430 | this.wrappedElt = null;
|
|
0 commit comments