OLD | NEW |
(Empty) | |
| 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. |
| 4 */ |
| 5 'use strict'; |
| 6 |
| 7 base.require('base.range'); |
| 8 base.require('ui.event_target'); |
| 9 |
| 10 base.exportTo('ccfv', function() { |
| 11 function QuadViewViewport(bbox, |
| 12 opt_scale, |
| 13 opt_dontPadBbox, opt_devicePixelRatio) { |
| 14 ui.EventTarget.call(this); |
| 15 if (bbox.isEmpty) |
| 16 throw new Error('Cannot initialize a viewport with an empty bbox'); |
| 17 |
| 18 this.setWorldBBox(bbox, opt_dontPadBbox); |
| 19 |
| 20 var devicePixelRatio; |
| 21 if (opt_devicePixelRatio) |
| 22 devicePixelRatio = opt_devicePixelRatio; |
| 23 else |
| 24 devicePixelRatio = window.devicePixelRatio || 1; |
| 25 |
| 26 var scale; |
| 27 if (opt_scale) { |
| 28 scale = opt_scale; |
| 29 } else { |
| 30 if (devicePixelRatio > 1) |
| 31 scale = 0.25; |
| 32 else |
| 33 scale = 0.125; |
| 34 } |
| 35 this.scale_ = scale; |
| 36 this.updateScale_(); |
| 37 } |
| 38 |
| 39 QuadViewViewport.prototype = { |
| 40 |
| 41 __proto__: ui.EventTarget.prototype, |
| 42 |
| 43 set scale(scale) { |
| 44 this.scale_ = scale; |
| 45 this.updateScale_(); |
| 46 this.didChange_(); |
| 47 }, |
| 48 |
| 49 get scale() { |
| 50 return this.scale_; |
| 51 }, |
| 52 |
| 53 updateScale_: function() { |
| 54 this.worldPixelsPerDevicePixel_ = this.scale_; |
| 55 this.devicePixelsPerLayoutPixel_ = 1 / devicePixelRatio; |
| 56 |
| 57 this.deviceWidth = |
| 58 this.worldRect.width * this.worldPixelsPerDevicePixel_; |
| 59 this.deviceHeight = |
| 60 this.worldRect.height * this.worldPixelsPerDevicePixel_; |
| 61 |
| 62 this.layoutWidth = this.deviceWidth * this.devicePixelsPerLayoutPixel_; |
| 63 this.layoutHeight = this.deviceHeight * this.devicePixelsPerLayoutPixel_; |
| 64 |
| 65 this.transformWorldToDevicePixels_ = mat2d.create(); |
| 66 this.transformDevicePixelsToWorld_ = mat2d.create(); |
| 67 this.updateTransform_(); |
| 68 }, |
| 69 |
| 70 setWorldBBox: function(bbox, opt_dontPadBbox) { |
| 71 var world_rect = bbox.asRect(); |
| 72 var world_pad; |
| 73 if (opt_dontPadBbox) { |
| 74 world_pad = 0; |
| 75 } else { |
| 76 world_pad = Math.min(world_rect.width, |
| 77 world_rect.height) * 0.10; |
| 78 } |
| 79 |
| 80 world_rect.enlarge(world_pad); |
| 81 this.worldRect = world_rect; |
| 82 this.updateTransform_(); |
| 83 this.didChange_(); |
| 84 }, |
| 85 |
| 86 updateTransform_: function() { |
| 87 if (!this.transformWorldToDevicePixels_) |
| 88 return; |
| 89 |
| 90 mat2d.identity(this.transformWorldToDevicePixels_); |
| 91 mat2d.translateInplaceXY( |
| 92 this.transformWorldToDevicePixels_, |
| 93 -this.worldRect.left, -this.worldRect.top); |
| 94 mat2d.scaleInplaceXY(this.transformWorldToDevicePixels_, |
| 95 this.worldPixelsPerDevicePixel_, |
| 96 this.worldPixelsPerDevicePixel_); |
| 97 |
| 98 mat2d.invert(this.transformDevicePixelsToWorld_, |
| 99 this.transformWorldToDevicePixels_); |
| 100 }, |
| 101 |
| 102 layoutPixelsToWorldPixels2: function(v) { |
| 103 var tmp = this.layoutPixelsToDevicePixels2(v); |
| 104 return this.devicePixelsToWorldPixels2(tmp); |
| 105 }, |
| 106 |
| 107 layoutPixelsToDevicePixels2: function(v) { |
| 108 var res = vec2.create(); |
| 109 return vec2.scale(res, v, 1 / this.devicePixelsPerLayoutPixel_); |
| 110 }, |
| 111 |
| 112 devicePixelsToWorldPixels2: function(v) { |
| 113 var res = vec2.create(); |
| 114 vec2.transformMat2d(res, v, this.transformDevicePixelsToWorld_); |
| 115 return res; |
| 116 }, |
| 117 |
| 118 getWorldToDevicePixelTransform: function() { |
| 119 return this.transformDevicePixelsToWorld_; |
| 120 }, |
| 121 |
| 122 getDeviceLineWidthAssumingTransformIsApplied: function( |
| 123 desired_device_line_width) { |
| 124 return desired_device_line_width / this.worldPixelsPerDevicePixel_; |
| 125 }, |
| 126 |
| 127 applyTransformToContext: function(ctx) { |
| 128 var transform = this.transformWorldToDevicePixels_; |
| 129 ctx.transform(transform[0], transform[1], transform[2], |
| 130 transform[3], transform[4], transform[5]); |
| 131 }, |
| 132 |
| 133 didChange_: function() { |
| 134 base.dispatchSimpleEvent(this, 'change', false, false); |
| 135 } |
| 136 }; |
| 137 |
| 138 return { |
| 139 QuadViewViewport: QuadViewViewport, |
| 140 } |
| 141 }); |
| 142 |
OLD | NEW |