2023-03-06 00:08:32 +03:00
|
|
|
/*
|
|
|
|
* OpenSeadragon - DrawerBase
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 CodePlex Foundation
|
2023-12-15 03:14:05 +03:00
|
|
|
* Copyright (C) 2010-2024 OpenSeadragon contributors
|
2023-03-06 00:08:32 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* - Neither the name of CodePlex Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function( $ ){
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class DrawerBase
|
|
|
|
* @memberof OpenSeadragon
|
|
|
|
* @classdesc Base class for Drawers that handle rendering of tiles for an {@link OpenSeadragon.Viewer}.
|
|
|
|
* @param {Object} options - Options for this Drawer.
|
|
|
|
* @param {OpenSeadragon.Viewer} options.viewer - The Viewer that owns this Drawer.
|
|
|
|
* @param {OpenSeadragon.Viewport} options.viewport - Reference to Viewer viewport.
|
|
|
|
* @param {Element} options.element - Parent element.
|
|
|
|
*/
|
|
|
|
|
2023-06-14 17:51:50 +03:00
|
|
|
$.DrawerBase = class DrawerBase{
|
2023-06-07 22:42:16 +03:00
|
|
|
constructor(options){
|
|
|
|
$.console.assert( options.viewer, "[Drawer] options.viewer is required" );
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-06-07 22:42:16 +03:00
|
|
|
//backward compatibility for positional args while preferring more
|
|
|
|
//idiomatic javascript options object as the only argument
|
|
|
|
var args = arguments;
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-06-07 22:42:16 +03:00
|
|
|
if( !$.isPlainObject( options ) ){
|
|
|
|
options = {
|
|
|
|
source: args[ 0 ], // Reference to Viewer tile source.
|
|
|
|
viewport: args[ 1 ], // Reference to Viewer viewport.
|
|
|
|
element: args[ 2 ] // Parent element.
|
|
|
|
};
|
|
|
|
}
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-06-07 22:42:16 +03:00
|
|
|
$.console.assert( options.viewport, "[Drawer] options.viewport is required" );
|
|
|
|
$.console.assert( options.element, "[Drawer] options.element is required" );
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-06-07 22:42:16 +03:00
|
|
|
if ( options.source ) {
|
|
|
|
$.console.error( "[Drawer] options.source is no longer accepted; use TiledImage instead" );
|
|
|
|
}
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-06-07 22:42:16 +03:00
|
|
|
this.viewer = options.viewer;
|
|
|
|
this.viewport = options.viewport;
|
|
|
|
this.debugGridColor = typeof options.debugGridColor === 'string' ? [options.debugGridColor] : options.debugGridColor || $.DEFAULT_SETTINGS.debugGridColor;
|
2023-06-19 02:08:33 +03:00
|
|
|
this.options = options.options || {};
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-06-07 22:42:16 +03:00
|
|
|
/**
|
|
|
|
* The parent element of this Drawer instance, passed in when the Drawer was created.
|
|
|
|
* The parent of {@link OpenSeadragon.DrawerBase#canvas}.
|
|
|
|
* @member {Element} container
|
|
|
|
* @memberof OpenSeadragon.DrawerBase#
|
|
|
|
*/
|
|
|
|
this.container = $.getElement( options.element );
|
|
|
|
|
2023-12-15 03:14:05 +03:00
|
|
|
this._renderingTarget = this._createDrawingElement();
|
|
|
|
|
2023-06-07 22:42:16 +03:00
|
|
|
|
|
|
|
this.canvas.style.width = "100%";
|
|
|
|
this.canvas.style.height = "100%";
|
|
|
|
this.canvas.style.position = "absolute";
|
2023-12-15 03:14:05 +03:00
|
|
|
this.canvas.style.left = "0";
|
|
|
|
$.setElementOpacity( this.canvas, this.viewer.opacity, true );
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-06-07 22:42:16 +03:00
|
|
|
// Allow pointer events to pass through the canvas element so implicit
|
|
|
|
// pointer capture works on touch devices
|
|
|
|
$.setElementPointerEventsNone( this.canvas );
|
|
|
|
$.setElementTouchActionNone( this.canvas );
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-06-07 22:42:16 +03:00
|
|
|
// explicit left-align
|
|
|
|
this.container.style.textAlign = "left";
|
|
|
|
this.container.appendChild( this.canvas );
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-06-07 22:42:16 +03:00
|
|
|
this._checkForAPIOverrides();
|
|
|
|
}
|
2023-07-27 00:55:46 +03:00
|
|
|
|
2023-12-15 03:14:05 +03:00
|
|
|
// protect the canvas member with a getter
|
2023-06-19 02:08:33 +03:00
|
|
|
get canvas(){
|
|
|
|
return this._renderingTarget;
|
|
|
|
}
|
|
|
|
get element(){
|
|
|
|
$.console.error('Drawer.element is deprecated. Use Drawer.container instead.');
|
|
|
|
return this.container;
|
|
|
|
}
|
|
|
|
|
2023-06-30 23:38:07 +03:00
|
|
|
/**
|
2023-12-15 03:14:05 +03:00
|
|
|
* @returns {String|undefined} What type of drawer this is.
|
2023-06-30 23:38:07 +03:00
|
|
|
*/
|
2023-07-09 21:10:20 +03:00
|
|
|
getType(){
|
2023-12-15 03:14:05 +03:00
|
|
|
$.console.error('Drawer.getType must be implemented by child class');
|
2023-06-30 23:38:07 +03:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2023-06-19 02:08:33 +03:00
|
|
|
/**
|
|
|
|
* @returns {Boolean} whether the drawer implementation is supported by the browser
|
|
|
|
*/
|
2023-06-30 04:55:59 +03:00
|
|
|
static isSupported() {
|
2023-06-19 02:08:33 +03:00
|
|
|
$.console.error('Drawer.isSupported must be implemented by child class');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create the HTML element (e.g. canvas, div) that the image will be drawn into
|
|
|
|
* @returns {Element} the element to draw into
|
|
|
|
*/
|
2023-12-13 05:45:24 +03:00
|
|
|
_createDrawingElement() {
|
|
|
|
$.console.error('Drawer._createDrawingElement must be implemented by child class');
|
2023-06-19 02:08:33 +03:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-14 17:51:50 +03:00
|
|
|
|
2023-03-06 00:08:32 +03:00
|
|
|
/**
|
2023-06-30 23:38:07 +03:00
|
|
|
* @param {Array} tiledImages - An array of TiledImages that are ready to be drawn
|
2023-03-06 00:08:32 +03:00
|
|
|
*/
|
2023-06-30 23:38:07 +03:00
|
|
|
draw(tiledImages) {
|
2023-03-06 00:08:32 +03:00
|
|
|
$.console.error('Drawer.draw must be implemented by child class');
|
2023-06-07 22:42:16 +03:00
|
|
|
}
|
2023-03-06 00:08:32 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Boolean} True if rotation is supported.
|
|
|
|
*/
|
2023-06-07 22:42:16 +03:00
|
|
|
canRotate() {
|
2023-03-06 00:08:32 +03:00
|
|
|
$.console.error('Drawer.canRotate must be implemented by child class');
|
2023-06-07 22:42:16 +03:00
|
|
|
}
|
2023-03-06 00:08:32 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the drawer (unload current loaded tiles)
|
|
|
|
*/
|
2023-06-07 22:42:16 +03:00
|
|
|
destroy() {
|
2023-03-06 00:08:32 +03:00
|
|
|
$.console.error('Drawer.destroy must be implemented by child class');
|
2023-06-07 22:42:16 +03:00
|
|
|
}
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-06-30 23:38:07 +03:00
|
|
|
/**
|
|
|
|
* @returns {Boolean} Whether this drawer requires enforcing minimum tile overlap to avoid showing seams.
|
|
|
|
*/
|
|
|
|
minimumOverlapRequired() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-06 00:08:32 +03:00
|
|
|
/**
|
|
|
|
* Turns image smoothing on or off for this viewer. Note: Ignored in some (especially older) browsers that do not support this property.
|
|
|
|
*
|
|
|
|
* @function
|
|
|
|
* @param {Boolean} [imageSmoothingEnabled] - Whether or not the image is
|
|
|
|
* drawn smoothly on the canvas; see imageSmoothingEnabled in
|
|
|
|
* {@link OpenSeadragon.Options} for more explanation.
|
|
|
|
*/
|
2023-06-07 22:42:16 +03:00
|
|
|
setImageSmoothingEnabled(imageSmoothingEnabled){
|
2023-03-06 00:08:32 +03:00
|
|
|
$.console.error('Drawer.setImageSmoothingEnabled must be implemented by child class');
|
2023-06-07 22:42:16 +03:00
|
|
|
}
|
2023-03-06 00:08:32 +03:00
|
|
|
|
|
|
|
/**
|
2023-03-11 19:38:21 +03:00
|
|
|
* Optional public API to draw a rectangle (e.g. for debugging purposes)
|
|
|
|
* Child classes can override this method if they wish to support this
|
|
|
|
* @param {OpenSeadragon.Rect} rect
|
|
|
|
*/
|
2023-06-07 22:42:16 +03:00
|
|
|
drawDebuggingRect(rect) {
|
2023-03-11 19:38:21 +03:00
|
|
|
$.console.warn('[drawer].drawDebuggingRect is not implemented by this drawer');
|
2023-06-07 22:42:16 +03:00
|
|
|
}
|
2023-03-11 19:38:21 +03:00
|
|
|
|
|
|
|
// Deprecated functions
|
2023-06-07 22:42:16 +03:00
|
|
|
clear(){
|
2023-03-11 19:38:21 +03:00
|
|
|
$.console.warn('[drawer].clear() is deprecated. The drawer is responsible for clearing itself as needed before drawing tiles.');
|
2023-06-07 22:42:16 +03:00
|
|
|
}
|
2023-03-11 19:38:21 +03:00
|
|
|
|
|
|
|
// Private functions
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Ensures that child classes have provided implementations for public API methods
|
|
|
|
* draw, canRotate, destroy, and setImageSmoothinEnabled. Throws an exception if the original
|
2023-03-06 00:08:32 +03:00
|
|
|
* placeholder methods are still in place.
|
|
|
|
*/
|
2023-06-07 22:42:16 +03:00
|
|
|
_checkForAPIOverrides(){
|
2023-12-13 05:45:24 +03:00
|
|
|
if(this._createDrawingElement === $.DrawerBase.prototype._createDrawingElement){
|
2023-12-15 03:14:05 +03:00
|
|
|
throw(new Error("[drawer]._createDrawingElement must be implemented by child class"));
|
2023-06-27 04:29:08 +03:00
|
|
|
}
|
2023-03-06 00:08:32 +03:00
|
|
|
if(this.draw === $.DrawerBase.prototype.draw){
|
2023-12-15 03:14:05 +03:00
|
|
|
throw(new Error("[drawer].draw must be implemented by child class"));
|
2023-03-06 00:08:32 +03:00
|
|
|
}
|
|
|
|
if(this.canRotate === $.DrawerBase.prototype.canRotate){
|
2023-12-15 03:14:05 +03:00
|
|
|
throw(new Error("[drawer].canRotate must be implemented by child class"));
|
2023-03-06 00:08:32 +03:00
|
|
|
}
|
|
|
|
if(this.destroy === $.DrawerBase.prototype.destroy){
|
2023-12-15 03:14:05 +03:00
|
|
|
throw(new Error("[drawer].destroy must be implemented by child class"));
|
2023-03-06 00:08:32 +03:00
|
|
|
}
|
|
|
|
if(this.setImageSmoothingEnabled === $.DrawerBase.prototype.setImageSmoothingEnabled){
|
2023-12-15 03:14:05 +03:00
|
|
|
throw(new Error("[drawer].setImageSmoothingEnabled must be implemented by child class"));
|
2023-03-06 00:08:32 +03:00
|
|
|
}
|
2023-06-07 22:42:16 +03:00
|
|
|
}
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-03-11 19:38:21 +03:00
|
|
|
|
2023-06-08 22:10:55 +03:00
|
|
|
// Utility functions
|
2023-03-11 19:38:21 +03:00
|
|
|
|
2023-03-06 00:08:32 +03:00
|
|
|
/**
|
|
|
|
* Scale from OpenSeadragon viewer rectangle to drawer rectangle
|
|
|
|
* (ignoring rotation)
|
|
|
|
* @param {OpenSeadragon.Rect} rectangle - The rectangle in viewport coordinate system.
|
|
|
|
* @returns {OpenSeadragon.Rect} Rectangle in drawer coordinate system.
|
|
|
|
*/
|
2023-06-08 22:10:55 +03:00
|
|
|
viewportToDrawerRectangle(rectangle) {
|
2023-03-06 00:08:32 +03:00
|
|
|
var topLeft = this.viewport.pixelFromPointNoRotate(rectangle.getTopLeft(), true);
|
|
|
|
var size = this.viewport.deltaPixelsFromPointsNoRotate(rectangle.getSize(), true);
|
|
|
|
|
|
|
|
return new $.Rect(
|
|
|
|
topLeft.x * $.pixelDensityRatio,
|
|
|
|
topLeft.y * $.pixelDensityRatio,
|
|
|
|
size.x * $.pixelDensityRatio,
|
|
|
|
size.y * $.pixelDensityRatio
|
|
|
|
);
|
2023-06-07 22:42:16 +03:00
|
|
|
}
|
2023-03-06 00:08:32 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function converts the given point from to the drawer coordinate by
|
|
|
|
* multiplying it with the pixel density.
|
|
|
|
* This function does not take rotation into account, thus assuming provided
|
|
|
|
* point is at 0 degree.
|
|
|
|
* @param {OpenSeadragon.Point} point - the pixel point to convert
|
|
|
|
* @returns {OpenSeadragon.Point} Point in drawer coordinate system.
|
|
|
|
*/
|
2023-06-08 22:10:55 +03:00
|
|
|
viewportCoordToDrawerCoord(point) {
|
2023-03-06 00:08:32 +03:00
|
|
|
var vpPoint = this.viewport.pixelFromPointNoRotate(point, true);
|
|
|
|
return new $.Point(
|
|
|
|
vpPoint.x * $.pixelDensityRatio,
|
|
|
|
vpPoint.y * $.pixelDensityRatio
|
|
|
|
);
|
2023-06-07 22:42:16 +03:00
|
|
|
}
|
2023-03-06 00:08:32 +03:00
|
|
|
|
2023-06-08 22:10:55 +03:00
|
|
|
|
|
|
|
// Internal utility functions
|
|
|
|
|
2023-03-11 19:38:21 +03:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Calculate width and height of the canvas based on viewport dimensions
|
|
|
|
* and pixelDensityRatio
|
2023-12-15 03:14:05 +03:00
|
|
|
* @returns {OpenSeadragon.Point} {x, y} size of the canvas
|
2023-03-11 19:38:21 +03:00
|
|
|
*/
|
2023-06-07 22:42:16 +03:00
|
|
|
_calculateCanvasSize() {
|
2023-03-06 00:08:32 +03:00
|
|
|
var pixelDensityRatio = $.pixelDensityRatio;
|
|
|
|
var viewportSize = this.viewport.getContainerSize();
|
2023-12-15 03:14:05 +03:00
|
|
|
return new OpenSeadragon.Point( Math.round(viewportSize.x * pixelDensityRatio), Math.round(viewportSize.y * pixelDensityRatio));
|
2023-03-06 00:08:32 +03:00
|
|
|
}
|
2023-06-07 22:42:16 +03:00
|
|
|
|
2023-06-14 17:51:50 +03:00
|
|
|
};
|
2023-03-06 00:08:32 +03:00
|
|
|
|
|
|
|
}( OpenSeadragon ));
|