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

Add thumbnailViewer demo #155

Merged
merged 4 commits into from
Oct 16, 2015
Merged
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
75 changes: 75 additions & 0 deletions demo/thumbnailViewer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
<style tyle="text/css">
html,body{
width: 100%;
height: 100%;
}

#mainViewContainer {
width: 95%;
height: 95%;
border: 1px solid black;
margin: 10px;
padding: 3px;
overflow: hidden;
}

#mainView {
width: 100%;
height: 100%;
min-height: 100%;
display: inline;
}

.thumbViewClass {
border: 1px solid black;
position: absolute;
bottom: 5px;
left: 5px;
width: 20%;
height: 30%;
margin: 3px
padding: 3px;
overflow: hidden;
}

#thumbView {
z-index: 110;
background: white;
}

#scopeContainer {
z-index: 120;
}
</style>
</head>

<body>

<div id="mainViewContainer">
<embed id="mainView" type="image/svg+xml" src="tiger.svg"/>
</div>

<div id="thumbViewContainer">
<svg id="scopeContainer" class="thumbViewClass">
<g>
<rect id="scope" fill="red" fill-opacity="0.1" stroke="red" stroke-width="2px" x="0" y="0" width="0" height="0"/>
<line id="line1" stroke="red" stroke-width="2px" x1="0" y1="0" x2="0" y2="0"/>
<line id="line2" stroke="red" stroke-width="2px" x1="0" y1="0" x2="0" y2="0"/>
</g>
</svg>
<embed id="thumbView" type="image/svg+xml" src="tiger.svg" class="thumbViewClass"/>
</div>

<script src="../dist/svg-pan-zoom.js" type="text/javascript" ></script>
<script src="./thumbnailViewer.js" type="text/javascript" ></script>

<script type="text/javascript">
thumbnailViewer({mainViewId: 'mainView',thumbViewId: 'thumbView'});
</script>
</body>

</html>
187 changes: 187 additions & 0 deletions demo/thumbnailViewer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
var thumbnailViewer = function(options){

var getSVGDocument = function(objectElem){
var svgDoc = objectElem.contentDocument;
if(! svgDoc){
svgDoc = objectElem.getSVGDocument();
}
return svgDoc;
}

var bindThumbnail = function(main, thumb){

if(! window.main && main){
window.main = main;
}
if(! window.thumb && thumb){
window.thumb = thumb;
}
if(! window.main || ! window.thumb){
return;
}

var resizeTimer;
var interval = 300; //msec
window.addEventListener('resize', function(event){
if (resizeTimer !== false) {
clearTimeout(resizeTimer);
}
resizeTimer = setTimeout(function () {
window.main.resize();
window.thumb.resize();
}, interval);
});

window.main.setOnZoom(function(level){
window.thumb.updateThumbScope();
if(options.onZoom){
options.onZoom(window.main, window.thumb, level);
}
});

window.main.setOnPan(function(point){
window.thumb.updateThumbScope();
if(options.onPan){
options.onPan(window.main, window.thumb, point);
}
});

var _updateThumbScope = function (main, thumb, scope, line1, line2){
var mainPanX = main.getPan().x
, mainPanY = main.getPan().y
, mainWidth = main.getSizes().width
, mainHeight = main.getSizes().height
, mainZoom = main.getSizes().realZoom
, thumbPanX = thumb.getPan().x
, thumbPanY = thumb.getPan().y
, thumbZoom = thumb.getSizes().realZoom;

var thumByMainZoomRatio = thumbZoom / mainZoom;

var scopeX = thumbPanX - mainPanX * thumByMainZoomRatio;
var scopeY = thumbPanY - mainPanY * thumByMainZoomRatio;
var scopeWidth = mainWidth * thumByMainZoomRatio;
var scopeHeight = mainHeight * thumByMainZoomRatio;

scope.setAttribute("x", scopeX + 1);
scope.setAttribute("y", scopeY + 1);
scope.setAttribute("width", scopeWidth - 2);
scope.setAttribute("height", scopeHeight - 2);
/*
line1.setAttribute("x1", scopeX + 1);
line1.setAttribute("y1", scopeY + 1);
line1.setAttribute("x2", scopeX + 1 + scopeWidth - 2);
line1.setAttribute("y2", scopeY + 1 + scopeHeight - 2);
line2.setAttribute("x1", scopeX + 1);
line2.setAttribute("y1", scopeY + 1 + scopeHeight - 2);
line2.setAttribute("x2", scopeX + 1 + scopeWidth - 2);
line2.setAttribute("y2", scopeY + 1);
*/
};

window.thumb.updateThumbScope = function(){
var scope = document.getElementById('scope');
var line1 = document.getElementById('line1');
var line2 = document.getElementById('line2');
_updateThumbScope(window.main, window.thumb, scope, line1, line2);
}
window.thumb.updateThumbScope();

var _updateMainViewPan = function(clientX, clientY, scopeContainer, main, thumb){
var dim = scopeContainer.getBoundingClientRect()
, mainWidth = main.getSizes().width
, mainHeight = main.getSizes().height
, mainZoom = main.getSizes().realZoom
, thumbWidth = thumb.getSizes().width
, thumbHeight = thumb.getSizes().height
, thumbZoom = thumb.getSizes().realZoom;

var thumbPanX = clientX - dim.left - thumbWidth / 2;
var thumbPanY = clientY - dim.top - thumbHeight / 2;
var mainPanX = - thumbPanX * mainZoom / thumbZoom;
var mainPanY = - thumbPanY * mainZoom / thumbZoom;
main.pan({x:mainPanX, y:mainPanY});
};

var updateMainViewPan = function(evt){
if(evt.which == 0 && evt.button == 0){
return false;
}
var scopeContainer = document.getElementById('scopeContainer');
_updateMainViewPan(evt.clientX, evt.clientY, scopeContainer, window.main, window.thumb);
}

var scopeContainer = document.getElementById('scopeContainer');
scopeContainer.addEventListener('click', function(evt){
updateMainViewPan(evt);
});

scopeContainer.addEventListener('mousemove', function(evt){
updateMainViewPan(evt);
});
};

var mainViewObjectElem = document.getElementById(options.mainViewId);
mainViewObjectElem.addEventListener("load", function(){

var mainViewSVGDoc = getSVGDocument(mainViewObjectElem);
if(options.onMainViewSVGLoaded){
options.onMainViewSVGLoaded(mainViewSVGDoc);
}

var beforePan = function(oldPan, newPan){
var stopHorizontal = false
, stopVertical = false
, gutterWidth = 100
, gutterHeight = 100
// Computed variables
, sizes = this.getSizes()
, leftLimit = -((sizes.viewBox.x + sizes.viewBox.width) * sizes.realZoom) + gutterWidth
, rightLimit = sizes.width - gutterWidth - (sizes.viewBox.x * sizes.realZoom)
, topLimit = -((sizes.viewBox.y + sizes.viewBox.height) * sizes.realZoom) + gutterHeight
, bottomLimit = sizes.height - gutterHeight - (sizes.viewBox.y * sizes.realZoom);
customPan = {};
customPan.x = Math.max(leftLimit, Math.min(rightLimit, newPan.x));
customPan.y = Math.max(topLimit, Math.min(bottomLimit, newPan.y));
return customPan;
};

var main = svgPanZoom('#'+options.mainViewId, {
zoomEnabled: true,
controlIconsEnabled: true,
fit: true,
center: true,
beforePan: beforePan
});

bindThumbnail(main, undefined);
if(options.onMainViewShown){
options.onMainViewShown(mainViewSVGDoc, main);
}

}, false);

var thumbViewObjectElem = document.getElementById(options.thumbViewId);
thumbViewObjectElem.addEventListener("load", function(){

var thumbViewSVGDoc = getSVGDocument(thumbViewObjectElem);
if(options.onThumbnailSVGLoaded){
options.onThumbnailSVGLoaded(thumbViewSVGDoc);
}

var thumb = svgPanZoom('#'+options.thumbViewId, {
zoomEnabled: false,
panEnabled: false,
controlIconsEnabled: false,
dblClickZoomEnabled: false,
preventMouseEventsDefault: true,
});

bindThumbnail(undefined, thumb);
if(options.onThumbnailShown){
options.onThumbnailShown(thumbViewSVGDoc, thumb);
}

}, false);
};