Skip to content

Commit

Permalink
For WebGL1 support, use power of 2 sized canvas for drawing dash lines
Browse files Browse the repository at this point in the history
(closes #1588)
  • Loading branch information
bhousel committed Oct 21, 2024
1 parent 55ad198 commit e3c2e7a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions modules/pixi/lib/DashLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,23 @@ export class DashLine {
return _dashTextureCache[key];
}

// For WebGL1 support, this canvas should have power of 2 dimensions.
const canvas = document.createElement('canvas');
canvas.width = dashSize;
canvas.height = Math.ceil(options.width);
const drawWidth = dashSize;
const drawHeight = Math.ceil(options.width);
canvas.width = PIXI.nextPow2(drawWidth);
canvas.height = PIXI.nextPow2(drawHeight);
const ctx = canvas.getContext('2d');
if (!ctx) {
console.warn('Did not get context from canvas'); // eslint-disable-line no-console
return null;
}

// scale up to fill canvas
const scaleX = canvas.width / drawWidth;
const scaleY = canvas.height / drawHeight;
ctx.scale(scaleX, scaleY);

ctx.strokeStyle = 'white';
ctx.globalAlpha = options.alpha;
ctx.lineWidth = options.width;
Expand All @@ -422,6 +430,7 @@ export class DashLine {
}
}
ctx.stroke();

const texture = (_dashTextureCache[key] = PIXI.Texture.from(canvas));
texture.source.scaleMode = 'nearest';

Expand Down

0 comments on commit e3c2e7a

Please sign in to comment.