Mercurial > hg > digilib-old
comparison client/digitallibrary/jquery/jquery.digilib.js @ 746:2e9a48dc7a0c jquery
move zoom-drag background along with birdview indicator
author | hertzhaft |
---|---|
date | Fri, 04 Feb 2011 00:02:06 +0100 |
parents | ad483c008159 |
children | fd4842f0afc7 |
comparison
equal
deleted
inserted
replaced
745:ad483c008159 | 746:2e9a48dc7a0c |
---|---|
1287 // bird's eye view zoom area click and drag handler | 1287 // bird's eye view zoom area click and drag handler |
1288 var setupBirdDrag = function(data) { | 1288 var setupBirdDrag = function(data) { |
1289 var $birdImg = data.$birdImg; | 1289 var $birdImg = data.$birdImg; |
1290 var $birdZoom = data.$birdZoom; | 1290 var $birdZoom = data.$birdZoom; |
1291 var $document = $(document); | 1291 var $document = $(document); |
1292 var startPos, newRect, birdImgRect, birdZoomRect; | 1292 var startPos, newRect, birdImgRect, birdZoomRect, fullRect; |
1293 | 1293 |
1294 // mousedown handler: start dragging bird zoom to a new position | 1294 // mousedown handler: start dragging bird zoom to a new position |
1295 var birdZoomStartDrag = function(evt) { | 1295 var birdZoomStartDrag = function(evt) { |
1296 startPos = geom.position(evt); | 1296 startPos = geom.position(evt); |
1297 // position may have changed | 1297 // position may have changed |
1298 data.birdTrafo = getImgTrafo($birdImg, FULL_AREA); | 1298 data.birdTrafo = getImgTrafo($birdImg, FULL_AREA); |
1299 birdImgRect = geom.rectangle($birdImg); | 1299 birdImgRect = geom.rectangle($birdImg); |
1300 birdZoomRect = geom.rectangle($birdZoom); | 1300 birdZoomRect = geom.rectangle($birdZoom); |
1301 newRect = null; | 1301 newRect = null; |
1302 fullRect = setZoomBG(data); // setup zoom background image | |
1302 $document.bind("mousemove.dlBirdMove", birdZoomMove); | 1303 $document.bind("mousemove.dlBirdMove", birdZoomMove); |
1303 $document.bind("mouseup.dlBirdMove", birdZoomEndDrag); | 1304 $document.bind("mouseup.dlBirdMove", birdZoomEndDrag); |
1304 $birdZoom.bind("mousemove.dlBirdMove", birdZoomMove); | 1305 // $birdZoom.bind("mousemove.dlBirdMove", birdZoomMove); |
1305 $birdZoom.bind("mouseup.dlBirdMove", birdZoomEndDrag); | 1306 // $birdZoom.bind("mouseup.dlBirdMove", birdZoomEndDrag); |
1306 return false; | 1307 return false; |
1307 }; | 1308 }; |
1308 | 1309 |
1309 // mousemove handler: drag | 1310 // mousemove handler: drag |
1310 var birdZoomMove = function(evt) { | 1311 var birdZoomMove = function(evt) { |
1314 newRect = birdZoomRect.copy(); | 1315 newRect = birdZoomRect.copy(); |
1315 newRect.addPosition(delta); | 1316 newRect.addPosition(delta); |
1316 // stay within birdimage | 1317 // stay within birdimage |
1317 newRect.stayInside(birdImgRect); | 1318 newRect.stayInside(birdImgRect); |
1318 newRect.adjustDiv($birdZoom); | 1319 newRect.adjustDiv($birdZoom); |
1320 // reflect birdview zoom position in scaler image | |
1321 // TODO: account for scaler position in embedded mode? | |
1322 var area = data.birdTrafo.invtransform(newRect); | |
1323 var imgArea = data.imgTrafo.transform(area); | |
1324 var offset = imgArea.getPosition().neg(); | |
1325 if (fullRect) { | |
1326 var bgPos = fullRect.getPosition().add(offset); | |
1327 } else { | |
1328 var bgPos = offset; | |
1329 } | |
1330 // move the background image to the new position | |
1331 data.$scaler.css({ | |
1332 'background-position' : bgPos.x + "px " + bgPos.y + "px" | |
1333 }); | |
1319 return false; | 1334 return false; |
1320 }; | 1335 }; |
1321 | 1336 |
1322 // mouseup handler: reload page | 1337 // mouseup handler: reload page |
1323 var birdZoomEndDrag = function(evt) { | 1338 var birdZoomEndDrag = function(evt) { |
1324 var settings = data.settings; | 1339 var settings = data.settings; |
1325 $document.unbind("mousemove.dlBirdMove", birdZoomMove); | 1340 $document.unbind("mousemove.dlBirdMove", birdZoomMove); |
1326 $document.unbind("mouseup.dlBirdMove", birdZoomEndDrag); | 1341 $document.unbind("mouseup.dlBirdMove", birdZoomEndDrag); |
1327 $birdZoom.unbind("mousemove.dlBirdMove", birdZoomMove); | 1342 // $birdZoom.unbind("mousemove.dlBirdMove", birdZoomMove); |
1328 $birdZoom.unbind("mouseup.dlBirdMove", birdZoomEndDrag); | 1343 // $birdZoom.unbind("mouseup.dlBirdMove", birdZoomEndDrag); |
1329 if (newRect == null) { | 1344 if (newRect == null) { |
1330 // no movement happened - set center to click position | 1345 // no movement happened - set center to click position |
1331 startPos = birdZoomRect.getCenter(); | 1346 startPos = birdZoomRect.getCenter(); |
1332 birdZoomMove(evt); | 1347 birdZoomMove(evt); |
1333 } | 1348 } |
1346 $birdImg.bind("mousedown.dlBirdMove", birdZoomStartDrag); | 1361 $birdImg.bind("mousedown.dlBirdMove", birdZoomStartDrag); |
1347 $birdZoom.bind("mousedown.dlBirdMove", birdZoomStartDrag); | 1362 $birdZoom.bind("mousedown.dlBirdMove", birdZoomStartDrag); |
1348 } | 1363 } |
1349 }; | 1364 }; |
1350 | 1365 |
1351 // set bird zoom indicator to img rect | 1366 // move bird zoom indicator to reflect zoomed detail area |
1352 var setBirdZoom = function(data, rect) { | 1367 var setBirdZoom = function(data, rect) { |
1353 var part = data.imgTrafo.invtransform(rect); | 1368 var part = data.imgTrafo.invtransform(rect); |
1354 // area = FULL_AREA.fit(part); | 1369 // area = FULL_AREA.fit(part); // we want to see where we transcend the borders |
1355 birdTrafo = getImgTrafo(data.$birdImg, FULL_AREA); | 1370 birdTrafo = getImgTrafo(data.$birdImg, FULL_AREA); |
1356 var birdRect = birdTrafo.transform(part); | 1371 var birdRect = birdTrafo.transform(part); |
1357 // acount for border width | 1372 // acount for border width |
1358 birdRect.addPosition({x : -2, y : -2}); | 1373 birdRect.addPosition({x : -2, y : -2}); |
1359 birdRect.adjustDiv(data.$birdZoom); | 1374 birdRect.adjustDiv(data.$birdZoom); |
1360 }; | 1375 }; |
1361 | 1376 |
1377 // set zoom background | |
1378 var setZoomBG = function(data) { | |
1379 var $scaler = data.$scaler; | |
1380 var $img = data.$img; | |
1381 var fullRect = null; | |
1382 // hide the scaler img, show background of div instead | |
1383 $img.css('visibility', 'hidden'); | |
1384 var scalerCss = { | |
1385 'background-image' : 'url(' + $img.attr('src') + ')', | |
1386 'background-repeat' : 'no-repeat', | |
1387 'background-position' : 'left top', | |
1388 'opacity' : '0.5', | |
1389 'cursor' : 'move' | |
1390 }; | |
1391 if (data.hasBgSize) { | |
1392 // full-size background using CSS3-background-size | |
1393 fullRect = data.imgTrafo.transform(FULL_AREA); | |
1394 if (fullRect.height < data.settings.maxBgSize && fullRect.width < data.settings.maxBgSize) { | |
1395 // correct offset because background is relative | |
1396 var scalePos = geom.position($scaler); | |
1397 fullRect.addPosition(scalePos.neg()); | |
1398 var url = getBirdImgUrl(data, ['rot', 'mo']); | |
1399 scalerCss['background-image'] = 'url(' + url + ')'; | |
1400 scalerCss[data.bgSizeName] = fullRect.width + 'px ' + fullRect.height + 'px'; | |
1401 scalerCss['background-position'] = fullRect.x + 'px '+ fullRect.y + 'px'; | |
1402 } else { | |
1403 // too big | |
1404 fullRect = null; | |
1405 } | |
1406 } | |
1407 $scaler.css(scalerCss); | |
1408 // isBgReady = true; | |
1409 return fullRect; | |
1410 }; | |
1411 | |
1362 // setup handlers for dragging the zoomed image | 1412 // setup handlers for dragging the zoomed image |
1363 var setupZoomDrag = function(data) { | 1413 var setupZoomDrag = function(data) { |
1364 var startPos, delta, fullRect, isBgReady; | 1414 var startPos, delta, fullRect; |
1365 var $document = $(document); | 1415 var $document = $(document); |
1366 var $elem = data.$elem; | 1416 var $elem = data.$elem; |
1367 var $scaler = data.$scaler; | 1417 var $scaler = data.$scaler; |
1368 var $img = data.$img; | 1418 var $img = data.$img; |
1369 | 1419 |
1371 var dragStart = function (evt) { | 1421 var dragStart = function (evt) { |
1372 console.debug("dragstart at=",evt); | 1422 console.debug("dragstart at=",evt); |
1373 // don't start dragging if not zoomed | 1423 // don't start dragging if not zoomed |
1374 if (isFullArea(data.zoomArea)) return false; | 1424 if (isFullArea(data.zoomArea)) return false; |
1375 startPos = geom.position(evt); | 1425 startPos = geom.position(evt); |
1376 fullRect = null; | |
1377 delta = null; | 1426 delta = null; |
1378 isBgReady = false; | 1427 // set low res background immediately on mousedown |
1428 fullRect = setZoomBG(data); | |
1379 $document.bind("mousemove.dlZoomDrag", dragMove); | 1429 $document.bind("mousemove.dlZoomDrag", dragMove); |
1380 $document.bind("mouseup.dlZoomDrag", dragEnd); | 1430 $document.bind("mouseup.dlZoomDrag", dragEnd); |
1381 return false; | 1431 return false; |
1382 }; | 1432 }; |
1383 | 1433 |
1384 // mousemove handler: drag zoomed image | 1434 // mousemove handler: drag zoomed image |
1385 var dragMove = function (evt) { | 1435 var dragMove = function (evt) { |
1386 var pos = geom.position(evt); | 1436 var pos = geom.position(evt); |
1387 delta = startPos.delta(pos); | 1437 delta = startPos.delta(pos); |
1388 if (!isBgReady) { | |
1389 // hide the scaler img, show background of div instead | |
1390 $img.css('visibility', 'hidden'); | |
1391 var scalerCss = { | |
1392 'background-image' : 'url(' + $img.attr('src') + ')', | |
1393 'background-repeat' : 'no-repeat', | |
1394 'background-position' : 'left top', | |
1395 'opacity' : '0.5', | |
1396 'cursor' : 'move' | |
1397 }; | |
1398 if (data.hasBgSize) { | |
1399 // full-size background using CSS3-background-size | |
1400 fullRect = data.imgTrafo.transform(FULL_AREA); | |
1401 if (fullRect.height < data.settings.maxBgSize && fullRect.width < data.settings.maxBgSize) { | |
1402 // correct offset because background is relative | |
1403 var scalePos = geom.position($scaler); | |
1404 fullRect.addPosition(scalePos.neg()); | |
1405 var url = getBirdImgUrl(data, ['rot', 'mo']); | |
1406 scalerCss['background-image'] = 'url(' + url + ')'; | |
1407 scalerCss[data.bgSizeName] = fullRect.width + 'px ' + fullRect.height + 'px'; | |
1408 scalerCss['background-position'] = fullRect.x + 'px '+ fullRect.y + 'px'; | |
1409 } else { | |
1410 // too big | |
1411 fullRect = null; | |
1412 } | |
1413 } | |
1414 $scaler.css(scalerCss); | |
1415 isBgReady = true; | |
1416 } | |
1417 if (fullRect) { | 1438 if (fullRect) { |
1418 var bgPos = fullRect.getPosition().add(delta); | 1439 var bgPos = fullRect.getPosition().add(delta); |
1419 } else { | 1440 } else { |
1420 var bgPos = delta; | 1441 var bgPos = delta; |
1421 } | 1442 } |
1422 // move the background image to the new position | 1443 // move the background image to the new position |
1423 $scaler.css({ | 1444 $scaler.css({ |
1424 'background-position' : bgPos.x + "px " + bgPos.y + "px" | 1445 'background-position' : bgPos.x + "px " + bgPos.y + "px" |
1425 }); | 1446 }); |
1426 // get old zoom area (screen coordinates) | 1447 // set birdview indicator to reflect new zoom position |
1448 // TODO: get rid of indicator wobble | |
1427 var za = geom.rectangle($img); | 1449 var za = geom.rectangle($img); |
1428 // move | |
1429 za.addPosition(delta.neg()); | 1450 za.addPosition(delta.neg()); |
1430 setBirdZoom(data, za); | 1451 setBirdZoom(data, za); |
1431 return false; | 1452 return false; |
1432 }; | 1453 }; |
1433 | 1454 |