Skip to content

Commit

Permalink
Merge pull request #3229 from leaksignal/unstable
Browse files Browse the repository at this point in the history
use cy.window() instead of global window in canvas renderer (jsdom/node-canvas support)
  • Loading branch information
chrtannus authored May 1, 2024
2 parents 3ea4144 + 194b8de commit 2bfefff
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/extensions/renderer/base/coord-ele-math/labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import * as math from '../../../../math';
import * as is from '../../../../is';
import * as util from '../../../../util';

/* global document */

let BRp = {};

BRp.recalculateNodeLabelProjection = function( node ){
Expand Down Expand Up @@ -469,6 +467,10 @@ BRp.getLabelJustification = function(ele){
BRp.calculateLabelDimensions = function( ele, text ){
let r = this;

var containerWindow = r.cy.window();

var document = containerWindow.document;

let cacheKey = util.hashString( text, ele._private.labelDimsKey );

let cache = r.labelDimCache || (r.labelDimCache = []);
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/renderer/base/load-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as math from '../../../math';

var BRp = {};

/* global document, window, ResizeObserver, MutationObserver */
/* global document, ResizeObserver, MutationObserver */

BRp.registerBinding = function( target, event, handler, useCapture ){ // eslint-disable-line no-unused-vars
var args = Array.prototype.slice.apply( arguments, [1] ); // copy
Expand Down Expand Up @@ -1374,7 +1374,7 @@ BRp.load = function(){
}, false );

var touchmoveHandler;
r.registerBinding(window, 'touchmove', touchmoveHandler = function(e) { // eslint-disable-line no-undef
r.registerBinding(containerWindow, 'touchmove', touchmoveHandler = function(e) { // eslint-disable-line no-undef
var capture = r.touchData.capture;

if( !capture && !eventInContainer(e) ){ return; }
Expand Down
4 changes: 3 additions & 1 deletion src/extensions/renderer/canvas/drawing-redraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ CRp.getPixelRatio = function(){
return this.forcedPixelRatio;
}

var containerWindow = this.cy.window();

var backingStore = context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 1;

return (window.devicePixelRatio || 1) / backingStore; // eslint-disable-line no-undef
return (containerWindow.devicePixelRatio || 1) / backingStore; // eslint-disable-line no-undef
};

CRp.paintCache = function( context ){
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/renderer/canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ CRp.MOTIONBLUR_BUFFER_DRAG = 2;
function CanvasRenderer( options ){
var r = this;

var containerWindow = r.cy.window();
var document = containerWindow.document;

r.data = {
canvases: new Array( CRp.CANVAS_LAYERS ),
contexts: new Array( CRp.CANVAS_LAYERS ),
Expand Down Expand Up @@ -314,6 +317,8 @@ CRp.makeOffscreenCanvas = function(width, height){
if( typeof OffscreenCanvas !== typeof undefined ){
canvas = new OffscreenCanvas(width, height);
} else {
var containerWindow = this.cy.window();
var document = containerWindow.document;
canvas = document.createElement('canvas'); // eslint-disable-line no-undef
canvas.width = width;
canvas.height = height;
Expand Down

0 comments on commit 2bfefff

Please sign in to comment.