Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance fix for sluggish Pan & Zoom #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions openseadragon-fabricjs-overlay.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* OpenSeadragon canvas Overlay plugin based on svg overlay plugin and fabric.js
* @version 0.0.2
* @version 0.6.0
*/
(function () {

Expand Down Expand Up @@ -109,26 +109,20 @@
});

/**
* Update viewport
* Update Pan & Zoom accordingly on viewport-change
*/
this._viewer.addHandler('update-viewport', function () {
self.resize();
self.resizeCanvas();
self.render();

});
this._viewer.addHandler('viewport-change', self.OnViewportChange.bind(self));

/**
* Resize the fabric.js overlay when the viewer or window changes size
*/
this._viewer.addHandler('open', function () {
self.resize();
self.resizeCanvas();
});
window.addEventListener('resize', function () {
let doResizeAll = function (){
self.resize();
self.resizeCanvas();
});
};
this._viewer.addHandler('open', doResizeAll);
this._viewer.addHandler('resize', doResizeAll);
window.addEventListener('resize', doResizeAll);

};

Expand Down Expand Up @@ -189,6 +183,21 @@

this._fabricCanvas.absolutePan(new fabric.Point(canvasOffset.left - x + pageScroll.x, canvasOffset.top - y + pageScroll.y));

},
OnViewportChange: function(e) {
// Update view bounds this way to avoid reszing the canvas, since it's expensive to constantly resize the canvas...
// Currently no support for rotation since fabric does not support viewport rotation...

// get Zoom
let cSize = this._viewer.viewport._containerInnerSize;
let zoom = cSize.x * this._viewer.viewport.getZoom(true) / this._scale;

// get X,Y position
let b = this._viewer.viewport.getBounds(true);
let wPt = this._viewer.viewport.pixelFromPoint(new OpenSeadragon.Point(b), true);

// update the canvas viewport
this._fabricCanvas.setViewportTransform([zoom, 0, 0, zoom, wPt.x, wPt.y]);
}

};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openseadragon-fabricjs-overlay",
"version": "0.2.0",
"version": "0.6.0",
"description": "An OpenSeadragon plugin that adds fabricjs canvas overlay capability.",
"main": "openseadragon-fabricjs-overlay.js",
"dependencies": {
Expand Down