Skip to content

Commit

Permalink
enable rendering cache
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed May 15, 2020
1 parent 36dfeb0 commit 2463276
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions src/elements/geoFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,63 @@ export const GeoFeature = (Chart.elements.GeoFeature = Chart.Element.extend({
return this.getCenterPoint();
},

draw() {
if (!this.feature) {
_drawInCache(doc) {
const bounds = this.getBounds();
if (!Number.isFinite(bounds.x)) {
return;
}
const canvas = this.cache && this.cache.canvas ? this.cache.canvas : doc.createElement('canvas');
canvas.width = Math.max(Math.ceil(bounds.width), 1);
canvas.height = Math.max(Math.ceil(bounds.height), 1);

const vm = this._view;
const ctx = this._chart.ctx;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.translate(-bounds.x, -bounds.y);
this._drawImpl(ctx);
ctx.restore();

this.cache = Object.assign({}, this.cache || {}, {
canvas,
canvasKey: this._optionsToKey(),
});
},

_optionsToKey() {
const options = this._view;
return `${options.backgroundColor};${options.borderColor};${options.borderWidth}`;
},

_drawImpl(ctx) {
const options = this._view;
ctx.beginPath();
this._xScale.geoPath.context(ctx)(this.feature);
if (vm.backgroundColor) {
ctx.fillStyle = vm.backgroundColor;
if (options.backgroundColor) {
ctx.fillStyle = options.backgroundColor;
ctx.fill();
}
if (vm.borderColor) {
ctx.strokeStyle = vm.borderColor;
ctx.lineWidth = vm.borderWidth;
if (options.borderColor) {
ctx.strokeStyle = options.borderColor;
ctx.lineWidth = options.borderWidth;
ctx.stroke();
}
ctx.restore();
},

draw() {
if (!this.feature) {
return;
}
const ctx = this._chart.ctx;
if (!this.cache || this.cache.canvasKey !== this._optionsToKey()) {
this._drawInCache(ctx.canvas.ownerDocument);
}
const bounds = this.getBounds();
if (this.cache && this.cache.canvas) {
ctx.drawImage(this.cache.canvas, bounds.x, bounds.y, bounds.width, bounds.height);
} else if (Number.isFinite(bounds.x)) {
ctx.save();
this._drawImpl(ctx);
ctx.restore();
}
},
}));

0 comments on commit 2463276

Please sign in to comment.