Skip to content

Commit

Permalink
fix: fix corner case for too small cache elem
Browse files Browse the repository at this point in the history
closes #171
  • Loading branch information
sgratzl committed Apr 29, 2023
1 parent cfbe693 commit adedfad
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/elements/GeoFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,13 @@ export class GeoFeature extends Element<IGeoFeatureProps, IGeoFeatureOptions> im
const x2 = Math.ceil(bounds.x + bounds.width);
const y2 = Math.ceil(bounds.y + bounds.height);
const pixelRatio = this.pixelRatio || 1;
canvas.width = Math.max(x2 - x1, 1) * pixelRatio;
canvas.height = Math.max(y2 - y1, 1) * pixelRatio;
const width = Math.ceil(Math.max(x2 - x1, 1) * pixelRatio);
const height = Math.ceil(Math.max(y2 - y1, 1) * pixelRatio);
if (width <= 0 || height <= 0) {
return;
}
canvas.width = width;
canvas.height = height;

const ctx = canvas.getContext('2d');
if (ctx) {
Expand Down Expand Up @@ -208,12 +213,16 @@ export class GeoFeature extends Element<IGeoFeatureProps, IGeoFeatureOptions> im
this._drawInCache(ctx.canvas.ownerDocument);
}
const bounds = this.getBounds();
if (this.cache && this.cache.canvas) {
if (this.cache && this.cache.canvas && this.cache.canvas.width > 0 && this.cache.canvas.height > 0) {
const x1 = Math.floor(bounds.x);
const y1 = Math.floor(bounds.y);
const x2 = Math.ceil(bounds.x + bounds.width);
const y2 = Math.ceil(bounds.y + bounds.height);
ctx.drawImage(this.cache.canvas, x1, y1, x2 - x1, y2 - y1);
const width = x2 - x1;
const height = y2 - y1;
if (width > 0 && height > 0) {
ctx.drawImage(this.cache.canvas, x1, y1, x2 - x1, y2 - y1);
}
} else if (Number.isFinite(bounds.x)) {
ctx.save();
this._drawImpl(ctx);
Expand Down

0 comments on commit adedfad

Please sign in to comment.