From e1d36ffb1458f734a92237e7132f8c881a94fdc7 Mon Sep 17 00:00:00 2001 From: Ian Gilman Date: Wed, 18 Mar 2015 10:03:44 -0700 Subject: [PATCH] First version of clip feature --- src/drawer.js | 28 ++++++++++++++++++++++++++++ src/tiledimage.js | 23 +++++++++++++++++++++++ src/viewer.js | 1 + test/demo/m2/js/main.js | 9 ++++++--- test/demo/m2/js/page.js | 7 +++++++ 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/drawer.js b/src/drawer.js index 77f078ad..52044375 100644 --- a/src/drawer.js +++ b/src/drawer.js @@ -258,6 +258,34 @@ $.Drawer.prototype = /** @lends OpenSeadragon.Drawer.prototype */{ } }, + // private + saveContext: function() { + if (!this.useCanvas) { + return; + } + + this.context.save(); + }, + + // private + restoreContext: function() { + if (!this.useCanvas) { + return; + } + + this.context.restore(); + }, + + // private + setClip: function(rect) { + if (!this.useCanvas) { + return; + } + + this.context.rect(rect.x, rect.y, rect.width, rect.height); + this.context.clip(); + }, + // private drawDebugInfo: function( tile, count, i ){ if ( this.useCanvas ) { diff --git a/src/tiledimage.js b/src/tiledimage.js index 9431502a..41c06e21 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -72,6 +72,8 @@ $.TiledImage = function( options ) { $.console.assert( options.viewer, "[TiledImage] options.viewer is required" ); $.console.assert( options.imageLoader, "[TiledImage] options.imageLoader is required" ); $.console.assert( options.source, "[TiledImage] options.source is required" ); + $.console.assert(!options.clip || options.clip instanceof $.Rect, + "[TiledImage] options.clip must be an OpenSeadragon.Rect if present"); $.EventSource.call( this ); @@ -84,6 +86,9 @@ $.TiledImage = function( options ) { this._imageLoader = options.imageLoader; delete options.imageLoader; + this._clip = options.clip; + delete options.clip; + var x = options.x || 0; delete options.x; var y = options.y || 0; @@ -1116,6 +1121,20 @@ function drawTiles( tiledImage, lastDrawn ){ position, tileSource; + var usedClip = false; + if (tiledImage._clip) { + tiledImage._drawer.saveContext(); + var box = tiledImage.imageToViewportRectangle(tiledImage._clip, true); + var topLeft = tiledImage.viewport.pixelFromPoint(box.getTopLeft(), true); + var size = tiledImage.viewport.deltaPixelsFromPoints(box.getSize(), true); + box = new OpenSeadragon.Rect(Math.round(topLeft.x * $.pixelDensityRatio), + Math.round(topLeft.y * $.pixelDensityRatio), + Math.round(size.x * $.pixelDensityRatio), + Math.round(size.y * $.pixelDensityRatio)); + tiledImage._drawer.setClip(box); + usedClip = true; + } + for ( i = lastDrawn.length - 1; i >= 0; i-- ) { tile = lastDrawn[ i ]; tiledImage._drawer.drawTile( tile, tiledImage._drawingHandler ); @@ -1147,6 +1166,10 @@ function drawTiles( tiledImage, lastDrawn ){ }); } } + + if (usedClip) { + tiledImage._drawer.restoreContext(); + } } }( OpenSeadragon )); diff --git a/src/viewer.js b/src/viewer.js index 155174d4..a585b936 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -1280,6 +1280,7 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, y: queueItem.options.y, width: queueItem.options.width, height: queueItem.options.height, + clip: queueItem.options.clip, springStiffness: _this.springStiffness, animationTime: _this.animationTime, minZoomImageRatio: _this.minZoomImageRatio, diff --git a/test/demo/m2/js/main.js b/test/demo/m2/js/main.js index 75337fdb..9f15857a 100644 --- a/test/demo/m2/js/main.js +++ b/test/demo/m2/js/main.js @@ -22,7 +22,10 @@ this.pages = this.createPages(); var tileSources = $.map(this.pages, function(v, i) { - return v.starter.tileSource; + return { + tileSource: v.starter.tileSource, + clip: v.starter.clip + }; }); this.viewer = OpenSeadragon({ @@ -543,11 +546,11 @@ } } + this.panBounds = null; + box = new OpenSeadragon.Rect(x, y, width, height); this.viewer.viewport.fitBounds(box, config.immediately); - this.panBounds = null; - var setPanBounds = function() { if (self.mode === 'page' || self.mode === 'book') { self.panBounds = box; diff --git a/test/demo/m2/js/page.js b/test/demo/m2/js/page.js index 736cb2f1..1294d3f5 100644 --- a/test/demo/m2/js/page.js +++ b/test/demo/m2/js/page.js @@ -19,6 +19,11 @@ tileSource: config.tileSource }; + if (config.clip) { + this.starter.clip = new OpenSeadragon.Rect(config.clip.x, config.clip.y, + config.clip.width, config.clip.height); + } + this.main = {}; this.alternateIndex = -1; @@ -56,6 +61,7 @@ App.viewer.world.removeItem(this.main.tiledImage); App.viewer.addTiledImage({ tileSource: itemInfo.tileSource, + clip: itemInfo.clip, index: this.pageIndex, success: function(event) { self.setDetail(self.main, itemInfo, event.item); @@ -81,6 +87,7 @@ App.viewer.addTiledImage({ tileSource: v.tileSource, + clip: v.clip, success: function(event) { v.tiledImage = event.item; self.placeDetail(v, true);