Skip to content

Commit

Permalink
move resizeObserver out of redraw calls
Browse files Browse the repository at this point in the history
  • Loading branch information
joewdavies committed Nov 21, 2024
1 parent bdb4576 commit b0e9448
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 64 deletions.
57 changes: 25 additions & 32 deletions dist/gridviz.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test/rendering-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
const style = new gridviz.ShapeColorSizeStyle({ color: colorFunction })

//add layer to map
map.layers = [new gridviz.GridLayer(dataset, [style], { minPixelsPerCell: 2 })]
map.layers = [new gridviz.GridLayer(dataset, [style], { minPixelsPerCell: 1 })]
</script>
</body>
</html>
51 changes: 20 additions & 31 deletions src/core/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Map {
}

//https://css-tricks.com/absolute-positioning-inside-relative-positioning/
this.container.style.position = "relative"; // container element must have relative positioning
this.container.style.position = 'relative' // container element must have relative positioning

//set dimensions
/** @type {number} */
Expand All @@ -54,14 +54,15 @@ export class Map {
* @type {GeoCanvas}
* @private */
this.geoCanvas = new GeoCanvas(this._canvas, opts.x, opts.y, opts.z, opts)
this.geoCanvas.redraw = () => { this.redraw() }
this.geoCanvas.redraw = () => {
this.redraw()
}

// legend div
this.legendDivId = opts.legendDivId || 'gvizLegend'
this.legend = select('#' + this.legendDivId)
if (this.legend.empty()) this.initialiseLegend()


//tooltip

// set App container as default parent element for tooltip
Expand All @@ -81,6 +82,9 @@ export class Map {
this.geoCanvas.canvas.addEventListener('mousemove', this.mouseMoveHandler)
this.geoCanvas.canvas.addEventListener('mouseout', this.mouseOutHandler)

// listen for resize events on the App's container and handle them
this.defineResizeObserver(this.container, this._canvas)

// add extra logic to onZoomStartFun
this.geoCanvas.onZoomStartFun = (e) => {
if (opts.onZoomStartFun) opts.onZoomStartFun(e)
Expand All @@ -104,10 +108,10 @@ export class Map {
opts.defaultGlobalCompositeOperation || this.geoCanvas.ctx.globalCompositeOperation
}

/**
* @protected
/**
* @protected
* @returns {HTMLCanvasElement}
*/
*/
initialiseCanvas() {
const canvas = document.createElement('canvas')
canvas.setAttribute('width', '' + this.w)
Expand All @@ -116,7 +120,6 @@ export class Map {
return canvas
}


initialiseLegend() {
this.legend = select(this.container.id && this.container.id != '' ? '#' + this.container.id : 'body')
.append('div')
Expand All @@ -138,8 +141,8 @@ export class Map {

/**
* Set/get layer stack.
*
* @param {undefined|import("./Layer.js").Layer|import("./Layer.js").Layer[]} layers
*
* @param {undefined|import("./Layer.js").Layer|import("./Layer.js").Layer[]} layers
* @returns { this | import("./Layer.js").Layer[] }
*/
layers_(layers) {
Expand All @@ -165,14 +168,12 @@ export class Map {

//go through the layers
for (const layer of this.layers) {

//check if layer is visible
if (layer.visible && !layer.visible(z)) continue

//set layer alpha and blend mode
this.geoCanvas.ctx.globalAlpha = layer.alpha ? layer.alpha(z) : 1.0
if (layer.blendOperation)
this.geoCanvas.ctx.globalCompositeOperation = layer.blendOperation(z)
if (layer.blendOperation) this.geoCanvas.ctx.globalCompositeOperation = layer.blendOperation(z)

//set affin transform to draw with geographical coordinates
this.geoCanvas.setCanvasTransform()
Expand All @@ -181,24 +182,17 @@ export class Map {
layer.draw(this.geoCanvas, this.legend)

//draw layer filter
if (layer.filterColor)
layer.drawFilter(this.geoCanvas)
if (layer.filterColor) layer.drawFilter(this.geoCanvas)

//restore default alpha and blend operation
this.geoCanvas.ctx.globalAlpha = 1.0
this.geoCanvas.ctx.globalCompositeOperation = this.defaultGlobalCompositeOperation
}

//
this.canvasSave = null

// listen for resize events on the App's container and handle them
this.defineResizeObserver(this.container, this._canvas)

return this
}


/**
* @param {number} marginPx
* @returns {import('./GeoCanvas.js').Envelope}
Expand All @@ -208,8 +202,6 @@ export class Map {
return this.geoCanvas.updateExtentGeo(marginPx)
}



/** @param {MouseEvent} e */
focusCell(e) {
//compute mouse geo position
Expand Down Expand Up @@ -274,7 +266,6 @@ export class Map {
}
}


/**
* Return the cell HTML info at a given geo position.
* This is usefull for user interactions, to show this info where the user clicks for example.
Expand Down Expand Up @@ -316,8 +307,7 @@ export class Map {
}
}


/**
/**
* @param {number} x
* @param {number} y
* @param {number|undefined} z
Expand All @@ -330,7 +320,9 @@ export class Map {
}

/** @returns {import('./GeoCanvas.js').View} */
getView() { return this.geoCanvas.view }
getView() {
return this.geoCanvas.view
}

/** @returns {number} */
getZoom() {
Expand All @@ -342,7 +334,6 @@ export class Map {
return this
}


/** @returns {Array.<number|undefined>} */
getCenterExtent() {
return this.geoCanvas.getCenterExtent()
Expand All @@ -363,8 +354,6 @@ export class Map {
return this
}



/** @returns {string} */
getBackgroundColor() {
return this.geoCanvas.backgroundColor
Expand Down Expand Up @@ -395,7 +384,7 @@ export class Map {
x: opts?.x,
y: opts?.y,
onZoom: opts?.onZoom,
delta: opts?.delta || 0.2
delta: opts?.delta || 0.2,
})

return this
Expand All @@ -418,7 +407,7 @@ export class Map {
id: opts?.id || 'gridviz-fullscreen-button',
class: opts?.class,
x: opts?.x,
y: opts?.y
y: opts?.y,
})

return this
Expand Down

0 comments on commit b0e9448

Please sign in to comment.