1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 * The overlay displaying the image.
10 * @param {HTMLElement} container The container element.
11 * @param {Viewport} viewport The viewport.
12 * @param {MetadataCache} metadataCache The metadataCache.
15 function ImageView(container, viewport, metadataCache) {
16 this.container_ = container;
17 this.viewport_ = viewport;
18 this.document_ = container.ownerDocument;
19 this.contentGeneration_ = 0;
20 this.displayedContentGeneration_ = 0;
21 this.displayedViewportGeneration_ = 0;
23 this.imageLoader_ = new ImageUtil.ImageLoader(this.document_, metadataCache);
24 // We have a separate image loader for prefetch which does not get cancelled
25 // when the selection changes.
26 this.prefetchLoader_ = new ImageUtil.ImageLoader(
27 this.document_, metadataCache);
29 // The content cache is used for prefetching the next image when going
30 // through the images sequentially. The real life photos can be large
31 // (18Mpix = 72Mb pixel array) so we want only the minimum amount of caching.
32 this.contentCache_ = new ImageView.Cache(2);
34 // We reuse previously generated screen-scale images so that going back to
35 // a recently loaded image looks instant even if the image is not in
36 // the content cache any more. Screen-scale images are small (~1Mpix)
37 // so we can afford to cache more of them.
38 this.screenCache_ = new ImageView.Cache(5);
39 this.contentCallbacks_ = [];
42 * The element displaying the current content.
44 * @type {HTMLCanvasElement|HTMLVideoElement}
47 this.screenImage_ = null;
49 this.localImageTransformFetcher_ = function(url, callback) {
50 metadataCache.get(url, 'fetchedMedia', function(fetchedMedia) {
51 callback(fetchedMedia.imageTransform);
57 * Duration of transition between modes in ms.
59 ImageView.MODE_TRANSITION_DURATION = 350;
62 * If the user flips though images faster than this interval we do not apply
63 * the slide-in/slide-out transition.
65 ImageView.FAST_SCROLL_INTERVAL = 300;
68 * Image load type: full resolution image loaded from cache.
70 ImageView.LOAD_TYPE_CACHED_FULL = 0;
73 * Image load type: screen resolution preview loaded from cache.
75 ImageView.LOAD_TYPE_CACHED_SCREEN = 1;
78 * Image load type: image read from file.
80 ImageView.LOAD_TYPE_IMAGE_FILE = 2;
83 * Image load type: video loaded.
85 ImageView.LOAD_TYPE_VIDEO_FILE = 3;
88 * Image load type: error occurred.
90 ImageView.LOAD_TYPE_ERROR = 4;
93 * Image load type: the file contents is not available offline.
95 ImageView.LOAD_TYPE_OFFLINE = 5;
98 * The total number of load types.
100 ImageView.LOAD_TYPE_TOTAL = 6;
102 ImageView.prototype = {__proto__: ImageBuffer.Overlay.prototype};
105 * Draw below overlays with the default zIndex.
106 * @return {number} Z-index.
108 ImageView.prototype.getZIndex = function() { return -1 };
111 * Draw the image on screen.
113 ImageView.prototype.draw = function() {
114 if (!this.contentCanvas_) // Do nothing if the image content is not set.
117 var forceRepaint = false;
119 if (this.displayedViewportGeneration_ !=
120 this.viewport_.getCacheGeneration()) {
121 this.displayedViewportGeneration_ = this.viewport_.getCacheGeneration();
123 this.setupDeviceBuffer(this.screenImage_);
129 this.displayedContentGeneration_ != this.contentGeneration_) {
130 this.displayedContentGeneration_ = this.contentGeneration_;
132 ImageUtil.trace.resetTimer('paint');
133 this.paintDeviceRect(this.viewport_.getDeviceClipped(),
134 this.contentCanvas_, this.viewport_.getImageClipped());
135 ImageUtil.trace.reportTimer('paint');
140 * @param {number} x X pointer position.
141 * @param {number} y Y pointer position.
142 * @param {boolean} mouseDown True if mouse is down.
143 * @return {string} CSS cursor style.
145 ImageView.prototype.getCursorStyle = function(x, y, mouseDown) {
146 // Indicate that the image is draggable.
147 if (this.viewport_.isClipped() &&
148 this.viewport_.getScreenClipped().inside(x, y))
155 * @param {number} x X pointer position.
156 * @param {number} y Y pointer position.
157 * @return {function} The closure to call on drag.
159 ImageView.prototype.getDragHandler = function(x, y) {
160 var cursor = this.getCursorStyle(x, y);
161 if (cursor == 'move') {
162 // Return the handler that drags the entire image.
163 return this.viewport_.createOffsetSetter(x, y);
170 * @return {number} The cache generation.
172 ImageView.prototype.getCacheGeneration = function() {
173 return this.contentGeneration_;
177 * Invalidate the caches to force redrawing the screen canvas.
179 ImageView.prototype.invalidateCaches = function() {
180 this.contentGeneration_++;
184 * @return {HTMLCanvasElement} The content canvas element.
186 ImageView.prototype.getCanvas = function() { return this.contentCanvas_ };
189 * @return {boolean} True if the a valid image is currently loaded.
191 ImageView.prototype.hasValidImage = function() {
192 return !this.preview_ && this.contentCanvas_ && this.contentCanvas_.width;
196 * @return {HTMLVideoElement} The video element.
198 ImageView.prototype.getVideo = function() { return this.videoElement_ };
201 * @return {HTMLCanvasElement} The cached thumbnail image.
203 ImageView.prototype.getThumbnail = function() { return this.thumbnailCanvas_ };
206 * @return {number} The content revision number.
208 ImageView.prototype.getContentRevision = function() {
209 return this.contentRevision_;
213 * Copy an image fragment from a full resolution canvas to a device resolution
216 * @param {Rect} deviceRect Rectangle in the device coordinates.
217 * @param {HTMLCanvasElement} canvas Full resolution canvas.
218 * @param {Rect} imageRect Rectangle in the full resolution canvas.
220 ImageView.prototype.paintDeviceRect = function(deviceRect, canvas, imageRect) {
221 // Map screen canvas (0,0) to (deviceBounds.left, deviceBounds.top)
222 var deviceBounds = this.viewport_.getDeviceClipped();
223 deviceRect = deviceRect.shift(-deviceBounds.left, -deviceBounds.top);
225 // The source canvas may have different physical size than the image size
226 // set at the viewport. Adjust imageRect accordingly.
227 var bounds = this.viewport_.getImageBounds();
228 var scaleX = canvas.width / bounds.width;
229 var scaleY = canvas.height / bounds.height;
230 imageRect = new Rect(imageRect.left * scaleX, imageRect.top * scaleY,
231 imageRect.width * scaleX, imageRect.height * scaleY);
233 this.screenImage_.getContext('2d'), canvas, deviceRect, imageRect);
237 * Create an overlay canvas with properties similar to the screen canvas.
238 * Useful for showing quick feedback when editing.
240 * @return {HTMLCanvasElement} Overlay canvas.
242 ImageView.prototype.createOverlayCanvas = function() {
243 var canvas = this.document_.createElement('canvas');
244 canvas.className = 'image';
245 this.container_.appendChild(canvas);
250 * Sets up the canvas as a buffer in the device resolution.
252 * @param {HTMLCanvasElement} canvas The buffer canvas.
254 ImageView.prototype.setupDeviceBuffer = function(canvas) {
255 var deviceRect = this.viewport_.getDeviceClipped();
257 // Set the canvas position and size in device pixels.
258 if (canvas.width != deviceRect.width)
259 canvas.width = deviceRect.width;
261 if (canvas.height != deviceRect.height)
262 canvas.height = deviceRect.height;
264 canvas.style.left = deviceRect.left + 'px';
265 canvas.style.top = deviceRect.top + 'px';
267 // Scale the canvas down to screen pixels.
268 this.setTransform(canvas);
272 * @return {ImageData} A new ImageData object with a copy of the content.
274 ImageView.prototype.copyScreenImageData = function() {
275 return this.screenImage_.getContext('2d').getImageData(
276 0, 0, this.screenImage_.width, this.screenImage_.height);
280 * @return {boolean} True if the image is currently being loaded.
282 ImageView.prototype.isLoading = function() {
283 return this.imageLoader_.isBusy();
287 * Cancel the current image loading operation. The callbacks will be ignored.
289 ImageView.prototype.cancelLoad = function() {
290 this.imageLoader_.cancel();
294 * Load and display a new image.
296 * Loads the thumbnail first, then replaces it with the main image.
297 * Takes into account the image orientation encoded in the metadata.
299 * @param {string} url Image url.
300 * @param {Object} metadata Metadata.
301 * @param {Object} effect Transition effect object.
302 * @param {function(number} displayCallback Called when the image is displayed
303 * (possibly as a prevew).
304 * @param {function(number} loadCallback Called when the image is fully loaded.
305 * The parameter is the load type.
307 ImageView.prototype.load = function(url, metadata, effect,
308 displayCallback, loadCallback) {
310 // Skip effects when reloading repeatedly very quickly.
311 var time = Date.now();
312 if (this.lastLoadTime_ &&
313 (time - this.lastLoadTime_) < ImageView.FAST_SCROLL_INTERVAL) {
316 this.lastLoadTime_ = time;
319 metadata = metadata || {};
321 ImageUtil.metrics.startInterval(ImageUtil.getMetricName('DisplayTime'));
325 this.contentID_ = url;
326 this.contentRevision_ = -1;
328 var loadingVideo = FileType.getMediaType(url) == 'video';
330 var video = this.document_.createElement('video');
331 var videoPreview = !!(metadata.thumbnail && metadata.thumbnail.url);
333 var thumbnailLoader = new ThumbnailLoader(
334 metadata.thumbnail.url,
335 ThumbnailLoader.LoaderType.CANVAS,
337 thumbnailLoader.loadDetachedImage(function(success) {
339 var canvas = thumbnailLoader.getImage();
340 video.setAttribute('poster', canvas.toDataURL('image/jpeg'));
341 this.replace(video, effect); // Show the poster immediately.
342 if (displayCallback) displayCallback();
347 var onVideoLoad = function(error) {
348 video.removeEventListener('loadedmetadata', onVideoLoadSuccess);
349 video.removeEventListener('error', onVideoLoadError);
350 displayMainImage(ImageView.LOAD_TYPE_VIDEO_FILE, videoPreview, video,
353 var onVideoLoadError = onVideoLoad.bind(this, 'VIDEO_ERROR');
354 var onVideoLoadSuccess = onVideoLoad.bind(this, null);
356 video.addEventListener('loadedmetadata', onVideoLoadSuccess);
357 video.addEventListener('error', onVideoLoadError);
364 // Cache has to be evicted in advance, so the returned cached image is not
365 // evicted later by the prefetched image.
366 this.contentCache_.evictLRU();
368 var cached = this.contentCache_.getItem(this.contentID_);
370 displayMainImage(ImageView.LOAD_TYPE_CACHED_FULL,
371 false /* no preview */, cached);
373 var cachedScreen = this.screenCache_.getItem(this.contentID_);
374 var imageWidth = metadata.media && metadata.media.width ||
375 metadata.drive && metadata.drive.imageWidth;
376 var imageHeight = metadata.media && metadata.media.height ||
377 metadata.drive && metadata.drive.imageHeight;
379 // We have a cached screen-scale canvas, use it instead of a thumbnail.
380 displayThumbnail(ImageView.LOAD_TYPE_CACHED_SCREEN, cachedScreen);
381 // As far as the user can tell the image is loaded. We still need to load
382 // the full res image to make editing possible, but we can report now.
383 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('DisplayTime'));
384 } else if ((!effect || (effect.constructor.name == 'Slide')) &&
385 metadata.thumbnail && metadata.thumbnail.url &&
386 !(imageWidth && imageHeight &&
387 ImageUtil.ImageLoader.isTooLarge(imageWidth, imageHeight))) {
388 // Only show thumbnails if there is no effect or the effect is Slide.
389 // Also no thumbnail if the image is too large to be loaded.
390 var thumbnailLoader = new ThumbnailLoader(
391 metadata.thumbnail.url,
392 ThumbnailLoader.LoaderType.CANVAS,
394 thumbnailLoader.loadDetachedImage(function(success) {
395 displayThumbnail(ImageView.LOAD_TYPE_IMAGE_FILE,
396 success ? thumbnailLoader.getImage() : null);
399 loadMainImage(ImageView.LOAD_TYPE_IMAGE_FILE, url,
400 false /* no preview*/, 0 /* delay */);
404 function displayThumbnail(loadType, canvas) {
409 metadata.media.width || metadata.drive.imageWidth,
410 metadata.media.height || metadata.drive.imageHeight,
412 if (displayCallback) displayCallback();
414 loadMainImage(loadType, url, !!canvas,
415 (effect && canvas) ? effect.getSafeInterval() : 0);
418 function loadMainImage(loadType, contentURL, previewShown, delay) {
419 if (self.prefetchLoader_.isLoading(contentURL)) {
420 // The image we need is already being prefetched. Initiating another load
421 // would be a waste. Hijack the load instead by overriding the callback.
422 self.prefetchLoader_.setCallback(
423 displayMainImage.bind(null, loadType, previewShown));
425 // Swap the loaders so that the self.isLoading works correctly.
426 var temp = self.prefetchLoader_;
427 self.prefetchLoader_ = self.imageLoader_;
428 self.imageLoader_ = temp;
431 self.prefetchLoader_.cancel(); // The prefetch was doing something useless.
433 self.imageLoader_.load(
435 self.localImageTransformFetcher_,
436 displayMainImage.bind(null, loadType, previewShown),
440 function displayMainImage(loadType, previewShown, content, opt_error) {
442 loadType = ImageView.LOAD_TYPE_ERROR;
444 // If we already displayed the preview we should not replace the content if:
445 // 1. The full content failed to load.
447 // 2. We are loading a video (because the full video is displayed in the
448 // same HTML element as the preview).
449 var animationDuration = 0;
450 if (!(previewShown &&
451 (loadType == ImageView.LOAD_TYPE_ERROR ||
452 loadType == ImageView.LOAD_TYPE_VIDEO_FILE))) {
453 var replaceEffect = previewShown ? null : effect;
454 animationDuration = replaceEffect ? replaceEffect.getSafeInterval() : 0;
455 self.replace(content, replaceEffect);
456 if (!previewShown && displayCallback) displayCallback();
459 if (loadType != ImageView.LOAD_TYPE_ERROR &&
460 loadType != ImageView.LOAD_TYPE_CACHED_SCREEN) {
461 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('DisplayTime'));
463 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('LoadMode'),
464 loadType, ImageView.LOAD_TYPE_TOTAL);
466 if (loadType == ImageView.LOAD_TYPE_ERROR &&
467 !navigator.onLine && metadata.streaming) {
468 // |streaming| is set only when the file is not locally cached.
469 loadType = ImageView.LOAD_TYPE_OFFLINE;
471 if (loadCallback) loadCallback(loadType, animationDuration, opt_error);
478 * @param {string} url The image url.
479 * @param {number} delay Image load delay in ms.
481 ImageView.prototype.prefetch = function(url, delay) {
483 function prefetchDone(canvas) {
485 self.contentCache_.putItem(url, canvas);
488 var cached = this.contentCache_.getItem(url);
490 prefetchDone(cached);
491 } else if (FileType.getMediaType(url) == 'image') {
492 // Evict the LRU item before we allocate the new canvas to avoid unneeded
494 this.contentCache_.evictLRU();
496 this.prefetchLoader_.load(
498 this.localImageTransformFetcher_,
505 * Rename the current image.
507 * @param {string} newUrl The new image url.
509 ImageView.prototype.changeUrl = function(newUrl) {
510 this.contentCache_.renameItem(this.contentID_, newUrl);
511 this.screenCache_.renameItem(this.contentID_, newUrl);
512 this.contentID_ = newUrl;
518 * @param {Rect} zoomToRect Target rectangle for zoom-out-effect.
520 ImageView.prototype.unload = function(zoomToRect) {
521 if (this.unloadTimer_) {
522 clearTimeout(this.unloadTimer_);
523 this.unloadTimer_ = null;
525 if (zoomToRect && this.screenImage_) {
526 var effect = this.createZoomEffect(zoomToRect);
527 this.setTransform(this.screenImage_, effect);
528 this.screenImage_.setAttribute('fade', true);
529 this.unloadTimer_ = setTimeout(function() {
530 this.unloadTimer_ = null;
531 this.unload(null /* force unload */);
533 effect.getSafeInterval());
536 this.container_.textContent = '';
537 this.contentCanvas_ = null;
538 this.screenImage_ = null;
539 this.videoElement_ = null;
544 * @param {HTMLCanvasElement|HTMLVideoElement} content The image element.
545 * @param {number=} opt_width Image width.
546 * @param {number=} opt_height Image height.
547 * @param {boolean=} opt_preview True if the image is a preview (not full res).
550 ImageView.prototype.replaceContent_ = function(
551 content, opt_width, opt_height, opt_preview) {
553 if (this.contentCanvas_ && this.contentCanvas_.parentNode == this.container_)
554 this.container_.removeChild(this.contentCanvas_);
556 if (content.constructor.name == 'HTMLVideoElement') {
557 this.contentCanvas_ = null;
558 this.videoElement_ = content;
559 this.screenImage_ = content;
560 this.screenImage_.className = 'image';
561 this.container_.appendChild(this.screenImage_);
562 this.videoElement_.play();
566 this.screenImage_ = this.document_.createElement('canvas');
567 this.screenImage_.className = 'image';
569 this.videoElement_ = null;
570 this.contentCanvas_ = content;
571 this.invalidateCaches();
572 this.viewport_.setImageSize(
573 opt_width || this.contentCanvas_.width,
574 opt_height || this.contentCanvas_.height);
575 this.viewport_.fitImage();
576 this.viewport_.update();
579 this.container_.appendChild(this.screenImage_);
581 this.preview_ = opt_preview;
582 // If this is not a thumbnail, cache the content and the screen-scale image.
583 if (this.hasValidImage()) {
584 // Insert the full resolution canvas into DOM so that it can be printed.
585 this.container_.appendChild(this.contentCanvas_);
586 this.contentCanvas_.classList.add('fullres');
588 this.contentCache_.putItem(this.contentID_, this.contentCanvas_, true);
589 this.screenCache_.putItem(this.contentID_, this.screenImage_);
591 // TODO(kaznacheev): It is better to pass screenImage_ as it is usually
592 // much smaller than contentCanvas_ and still contains the entire image.
593 // Once we implement zoom/pan we should pass contentCanvas_ instead.
594 this.updateThumbnail_(this.screenImage_);
596 this.contentRevision_++;
597 for (var i = 0; i != this.contentCallbacks_.length; i++) {
599 this.contentCallbacks_[i]();
608 * Add a listener for content changes.
609 * @param {function} callback Callback.
611 ImageView.prototype.addContentCallback = function(callback) {
612 this.contentCallbacks_.push(callback);
616 * Update the cached thumbnail image.
618 * @param {HTMLCanvasElement} canvas The source canvas.
621 ImageView.prototype.updateThumbnail_ = function(canvas) {
622 ImageUtil.trace.resetTimer('thumb');
623 var pixelCount = 10000;
625 Math.max(1, Math.sqrt(canvas.width * canvas.height / pixelCount));
627 this.thumbnailCanvas_ = canvas.ownerDocument.createElement('canvas');
628 this.thumbnailCanvas_.width = Math.round(canvas.width / downScale);
629 this.thumbnailCanvas_.height = Math.round(canvas.height / downScale);
630 Rect.drawImage(this.thumbnailCanvas_.getContext('2d'), canvas);
631 ImageUtil.trace.reportTimer('thumb');
635 * Replace the displayed image, possibly with slide-in animation.
637 * @param {HTMLCanvasElement|HTMLVideoElement} content The image element.
638 * @param {Object=} opt_effect Transition effect object.
639 * @param {number=} opt_width Image width.
640 * @param {number=} opt_height Image height.
641 * @param {boolean=} opt_preview True if the image is a preview (not full res).
643 ImageView.prototype.replace = function(
644 content, opt_effect, opt_width, opt_height, opt_preview) {
645 var oldScreenImage = this.screenImage_;
647 this.replaceContent_(content, opt_width, opt_height, opt_preview);
650 oldScreenImage.parentNode.removeChild(oldScreenImage);
654 var newScreenImage = this.screenImage_;
657 ImageUtil.setAttribute(newScreenImage, 'fade', true);
658 this.setTransform(newScreenImage, opt_effect, 0 /* instant */);
660 setTimeout(function() {
661 this.setTransform(newScreenImage, null,
662 opt_effect && opt_effect.getDuration());
663 if (oldScreenImage) {
664 ImageUtil.setAttribute(newScreenImage, 'fade', false);
665 ImageUtil.setAttribute(oldScreenImage, 'fade', true);
666 console.assert(opt_effect.getReverse, 'Cannot revert an effect.');
667 var reverse = opt_effect.getReverse();
668 this.setTransform(oldScreenImage, reverse);
669 setTimeout(function() {
670 if (oldScreenImage.parentNode)
671 oldScreenImage.parentNode.removeChild(oldScreenImage);
672 }, reverse.getSafeInterval());
678 * @param {HTMLCanvasElement|HTMLVideoElement} element The element to transform.
679 * @param {ImageView.Effect=} opt_effect The effect to apply.
680 * @param {number=} opt_duration Transition duration.
682 ImageView.prototype.setTransform = function(element, opt_effect, opt_duration) {
684 opt_effect = new ImageView.Effect.None();
685 if (typeof opt_duration != 'number')
686 opt_duration = opt_effect.getDuration();
687 element.style.webkitTransitionDuration = opt_duration + 'ms';
688 element.style.webkitTransitionTimingFunction = opt_effect.getTiming();
689 element.style.webkitTransform = opt_effect.transform(element, this.viewport_);
693 * @param {Rect} screenRect Target rectangle in screen coordinates.
694 * @return {ImageView.Effect.Zoom} Zoom effect object.
696 ImageView.prototype.createZoomEffect = function(screenRect) {
697 return new ImageView.Effect.Zoom(
698 this.viewport_.screenToDeviceRect(screenRect),
699 null /* use viewport */,
700 ImageView.MODE_TRANSITION_DURATION);
704 * Visualize crop or rotate operation. Hide the old image instantly, animate
705 * the new image to visualize the operation.
707 * @param {HTMLCanvasElement} canvas New content canvas.
708 * @param {Rect} imageCropRect The crop rectangle in image coordinates.
709 * Null for rotation operations.
710 * @param {number} rotate90 Rotation angle in 90 degree increments.
711 * @return {number} Animation duration.
713 ImageView.prototype.replaceAndAnimate = function(
714 canvas, imageCropRect, rotate90) {
715 var oldScale = this.viewport_.getScale();
716 var deviceCropRect = imageCropRect && this.viewport_.screenToDeviceRect(
717 this.viewport_.imageToScreenRect(imageCropRect));
719 var oldScreenImage = this.screenImage_;
720 this.replaceContent_(canvas);
721 var newScreenImage = this.screenImage_;
723 // Display the new canvas, initially transformed.
724 var deviceFullRect = this.viewport_.getDeviceClipped();
726 var effect = rotate90 ?
727 new ImageView.Effect.Rotate(
728 oldScale / this.viewport_.getScale(), -rotate90) :
729 new ImageView.Effect.Zoom(deviceCropRect, deviceFullRect);
731 this.setTransform(newScreenImage, effect, 0 /* instant */);
733 oldScreenImage.parentNode.appendChild(newScreenImage);
734 oldScreenImage.parentNode.removeChild(oldScreenImage);
736 // Let the layout fire, then animate back to non-transformed state.
738 this.setTransform.bind(
739 this, newScreenImage, null, effect.getDuration()),
742 return effect.getSafeInterval();
746 * Visualize "undo crop". Shrink the current image to the given crop rectangle
747 * while fading in the new image.
749 * @param {HTMLCanvasElement} canvas New content canvas.
750 * @param {Rect} imageCropRect The crop rectangle in image coordinates.
751 * @return {number} Animation duration.
753 ImageView.prototype.animateAndReplace = function(canvas, imageCropRect) {
754 var deviceFullRect = this.viewport_.getDeviceClipped();
755 var oldScale = this.viewport_.getScale();
757 var oldScreenImage = this.screenImage_;
758 this.replaceContent_(canvas);
759 var newScreenImage = this.screenImage_;
761 var deviceCropRect = this.viewport_.screenToDeviceRect(
762 this.viewport_.imageToScreenRect(imageCropRect));
764 var setFade = ImageUtil.setAttribute.bind(null, newScreenImage, 'fade');
766 oldScreenImage.parentNode.insertBefore(newScreenImage, oldScreenImage);
768 var effect = new ImageView.Effect.Zoom(deviceCropRect, deviceFullRect);
769 // Animate to the transformed state.
770 this.setTransform(oldScreenImage, effect);
772 setTimeout(setFade.bind(null, false), 0);
774 setTimeout(function() {
775 if (oldScreenImage.parentNode)
776 oldScreenImage.parentNode.removeChild(oldScreenImage);
777 }, effect.getSafeInterval());
779 return effect.getSafeInterval();
784 * Generic cache with a limited capacity and LRU eviction.
786 * @param {number} capacity Maximum number of cached item.
789 ImageView.Cache = function(capacity) {
790 this.capacity_ = capacity;
796 * Fetch the item from the cache.
798 * @param {string} id The item ID.
799 * @return {Object} The cached item.
801 ImageView.Cache.prototype.getItem = function(id) { return this.map_[id] };
804 * Put the item into the cache.
805 * @param {string} id The item ID.
806 * @param {Object} item The item object.
807 * @param {boolean=} opt_keepLRU True if the LRU order should not be modified.
809 ImageView.Cache.prototype.putItem = function(id, item, opt_keepLRU) {
810 var pos = this.order_.indexOf(id);
812 if ((pos >= 0) != (id in this.map_))
813 throw new Error('Inconsistent cache state');
815 if (id in this.map_) {
817 // Move to the end (most recently used).
818 this.order_.splice(pos, 1);
819 this.order_.push(id);
823 this.order_.push(id);
826 if ((pos >= 0) && (item != this.map_[id]))
827 this.deleteItem_(this.map_[id]);
828 this.map_[id] = item;
830 if (this.order_.length > this.capacity_)
831 throw new Error('Exceeded cache capacity');
835 * Evict the least recently used items.
837 ImageView.Cache.prototype.evictLRU = function() {
838 if (this.order_.length == this.capacity_) {
839 var id = this.order_.shift();
840 this.deleteItem_(this.map_[id]);
841 delete this.map_[id];
846 * Change the id of an entry.
847 * @param {string} oldId The old ID.
848 * @param {string} newId The new ID.
850 ImageView.Cache.prototype.renameItem = function(oldId, newId) {
852 return; // No need to rename.
854 var pos = this.order_.indexOf(oldId);
856 return; // Not cached.
858 this.order_[pos] = newId;
859 this.map_[newId] = this.map_[oldId];
860 delete this.map_[oldId];
864 * Disposes an object.
866 * @param {Object} item The item object.
869 ImageView.Cache.prototype.deleteItem_ = function(item) {
870 // Trick to reduce memory usage without waiting for gc.
871 if (item instanceof HTMLCanvasElement) {
872 // If the canvas is being used somewhere else (eg. displayed on the screen),
873 // it will be cleared.
879 /* Transition effects */
882 * Base class for effects.
884 * @param {number} duration Duration in ms.
885 * @param {string=} opt_timing CSS transition timing function name.
888 ImageView.Effect = function(duration, opt_timing) {
889 this.duration_ = duration;
890 this.timing_ = opt_timing || 'linear';
896 ImageView.Effect.DEFAULT_DURATION = 180;
901 ImageView.Effect.MARGIN = 100;
904 * @return {number} Effect duration in ms.
906 ImageView.Effect.prototype.getDuration = function() { return this.duration_ };
909 * @return {number} Delay in ms since the beginning of the animation after which
910 * it is safe to perform CPU-heavy operations without disrupting the animation.
912 ImageView.Effect.prototype.getSafeInterval = function() {
913 return this.getDuration() + ImageView.Effect.MARGIN;
917 * @return {string} CSS transition timing function name.
919 ImageView.Effect.prototype.getTiming = function() { return this.timing_ };
922 * @param {HTMLCanvasElement|HTMLVideoElement} element Element.
923 * @return {number} Preferred pixel ration to use with this element.
926 ImageView.Effect.getPixelRatio_ = function(element) {
927 if (element.constructor.name == 'HTMLCanvasElement')
928 return Viewport.getDevicePixelRatio();
934 * Default effect. It is not a no-op as it needs to adjust a canvas scale
935 * for devicePixelRatio.
939 ImageView.Effect.None = function() {
940 ImageView.Effect.call(this, 0);
944 * Inherits from ImageView.Effect.
946 ImageView.Effect.None.prototype = { __proto__: ImageView.Effect.prototype };
949 * @param {HTMLCanvasElement|HTMLVideoElement} element Element.
950 * @return {string} Transform string.
952 ImageView.Effect.None.prototype.transform = function(element) {
953 var ratio = ImageView.Effect.getPixelRatio_(element);
954 return 'scale(' + (1 / ratio) + ')';
960 * @param {number} direction -1 for left, 1 for right.
961 * @param {boolean=} opt_slow True if slow (as in slideshow).
964 ImageView.Effect.Slide = function Slide(direction, opt_slow) {
965 ImageView.Effect.call(this,
966 opt_slow ? 800 : ImageView.Effect.DEFAULT_DURATION, 'ease-in-out');
967 this.direction_ = direction;
968 this.slow_ = opt_slow;
969 this.shift_ = opt_slow ? 100 : 40;
970 if (this.direction_ < 0) this.shift_ = -this.shift_;
974 * Inherits from ImageView.Effect.
976 ImageView.Effect.Slide.prototype = { __proto__: ImageView.Effect.prototype };
979 * @return {ImageView.Effect.Slide} Reverse Slide effect.
981 ImageView.Effect.Slide.prototype.getReverse = function() {
982 return new ImageView.Effect.Slide(-this.direction_, this.slow_);
986 * @param {HTMLCanvasElement|HTMLVideoElement} element Element.
987 * @return {string} Transform string.
989 ImageView.Effect.Slide.prototype.transform = function(element) {
990 var ratio = ImageView.Effect.getPixelRatio_(element);
991 return 'scale(' + (1 / ratio) + ') translate(' + this.shift_ + 'px, 0px)';
997 * Animates the original rectangle to the target rectangle. Both parameters
998 * should be given in device coordinates (accounting for devicePixelRatio).
1000 * @param {Rect} deviceTargetRect Target rectangle.
1001 * @param {Rect=} opt_deviceOriginalRect Original rectangle. If omitted,
1002 * the full viewport will be used at the time of |transform| call.
1003 * @param {number=} opt_duration Duration in ms.
1006 ImageView.Effect.Zoom = function(
1007 deviceTargetRect, opt_deviceOriginalRect, opt_duration) {
1008 ImageView.Effect.call(this,
1009 opt_duration || ImageView.Effect.DEFAULT_DURATION);
1010 this.target_ = deviceTargetRect;
1011 this.original_ = opt_deviceOriginalRect;
1015 * Inherits from ImageView.Effect.
1017 ImageView.Effect.Zoom.prototype = { __proto__: ImageView.Effect.prototype };
1020 * @param {HTMLCanvasElement|HTMLVideoElement} element Element.
1021 * @param {Viewport} viewport Viewport.
1022 * @return {string} Transform string.
1024 ImageView.Effect.Zoom.prototype.transform = function(element, viewport) {
1025 if (!this.original_)
1026 this.original_ = viewport.getDeviceClipped();
1028 var ratio = ImageView.Effect.getPixelRatio_(element);
1030 var dx = (this.target_.left + this.target_.width / 2) -
1031 (this.original_.left + this.original_.width / 2);
1032 var dy = (this.target_.top + this.target_.height / 2) -
1033 (this.original_.top + this.original_.height / 2);
1035 var scaleX = this.target_.width / this.original_.width;
1036 var scaleY = this.target_.height / this.original_.height;
1038 return 'translate(' + (dx / ratio) + 'px,' + (dy / ratio) + 'px) ' +
1039 'scaleX(' + (scaleX / ratio) + ') scaleY(' + (scaleY / ratio) + ')';
1045 * @param {number} scale Scale.
1046 * @param {number} rotate90 Rotation in 90 degrees increments.
1049 ImageView.Effect.Rotate = function(scale, rotate90) {
1050 ImageView.Effect.call(this, ImageView.Effect.DEFAULT_DURATION);
1051 this.scale_ = scale;
1052 this.rotate90_ = rotate90;
1056 * Inherits from ImageView.Effect.
1058 ImageView.Effect.Rotate.prototype = { __proto__: ImageView.Effect.prototype };
1061 * @param {HTMLCanvasElement|HTMLVideoElement} element Element.
1062 * @return {string} Transform string.
1064 ImageView.Effect.Rotate.prototype.transform = function(element) {
1065 var ratio = ImageView.Effect.getPixelRatio_(element);
1066 return 'rotate(' + (this.rotate90_ * 90) + 'deg) ' +
1067 'scale(' + (this.scale_ / ratio) + ')';