OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 // A simple camera controller which uses an HTML element as the event |
| 32 // source for constructing a view matrix. Assign an "onchange" |
| 33 // function to the controller as follows to receive the updated X and |
| 34 // Y angles for the camera: |
| 35 // |
| 36 // var controller = new CameraController(canvas); |
| 37 // controller.onchange = function(xRot, yRot) { ... }; |
| 38 // |
| 39 // The view matrix is computed elsewhere. |
| 40 // |
| 41 // opt_canvas (an HTMLCanvasElement) and opt_context (a |
| 42 // WebGLRenderingContext) can be passed in to make the hit detection |
| 43 // more precise -- only opaque pixels will be considered as the start |
| 44 // of a drag action. |
| 45 function CameraController(element, opt_canvas, opt_context) { |
| 46 var controller = this; |
| 47 this.onchange = null; |
| 48 this.xRot = 0; |
| 49 this.yRot = 0; |
| 50 this.scaleFactor = 3.0; |
| 51 this.dragging = false; |
| 52 this.curX = 0; |
| 53 this.curY = 0; |
| 54 |
| 55 if (opt_canvas) |
| 56 this.canvas_ = opt_canvas; |
| 57 |
| 58 if (opt_context) |
| 59 this.context_ = opt_context; |
| 60 |
| 61 // Assign a mouse down handler to the HTML element. |
| 62 element.onmousedown = function(ev) { |
| 63 controller.curX = ev.clientX; |
| 64 controller.curY = ev.clientY; |
| 65 var dragging = false; |
| 66 if (controller.canvas_ && controller.context_) { |
| 67 var rect = controller.canvas_.getBoundingClientRect(); |
| 68 // Transform the event's x and y coordinates into the coordinate |
| 69 // space of the canvas |
| 70 var canvasRelativeX = ev.pageX - rect.left; |
| 71 var canvasRelativeY = ev.pageY - rect.top; |
| 72 var canvasWidth = controller.canvas_.width; |
| 73 var canvasHeight = controller.canvas_.height; |
| 74 |
| 75 // Read back a small portion of the frame buffer around this point |
| 76 if (canvasRelativeX > 0 && canvasRelativeX < canvasWidth && |
| 77 canvasRelativeY > 0 && canvasRelativeY < canvasHeight) { |
| 78 var pixels = new Uint8Array(1); |
| 79 controller.context_.readPixels(canvasRelativeX, |
| 80 canvasHeight - canvasRelativeY, |
| 81 1, |
| 82 1, |
| 83 controller.context_.RGBA, |
| 84 controller.context_.UNSIGNED_BYTE
, |
| 85 pixels); |
| 86 // See whether this pixel has an alpha value of >= about 10% |
| 87 if (pixels[3] > (255.0 / 10.0)) { |
| 88 dragging = true; |
| 89 } |
| 90 } |
| 91 } else { |
| 92 dragging = true; |
| 93 } |
| 94 |
| 95 controller.dragging = dragging; |
| 96 }; |
| 97 |
| 98 // Assign a mouse up handler to the HTML element. |
| 99 element.onmouseup = function(ev) { |
| 100 controller.dragging = false; |
| 101 }; |
| 102 |
| 103 // Assign a mouse move handler to the HTML element. |
| 104 element.onmousemove = function(ev) { |
| 105 if (controller.dragging) { |
| 106 // Determine how far we have moved since the last mouse move |
| 107 // event. |
| 108 var curX = ev.clientX; |
| 109 var curY = ev.clientY; |
| 110 var deltaX = (controller.curX - curX) / controller.scaleFactor; |
| 111 var deltaY = (controller.curY - curY) / controller.scaleFactor; |
| 112 controller.curX = curX; |
| 113 controller.curY = curY; |
| 114 // Update the X and Y rotation angles based on the mouse motion. |
| 115 controller.yRot = (controller.yRot + deltaX) % 360; |
| 116 controller.xRot = (controller.xRot + deltaY); |
| 117 // Clamp the X rotation to prevent the camera from going upside |
| 118 // down. |
| 119 if (controller.xRot < -90) { |
| 120 controller.xRot = -90; |
| 121 } else if (controller.xRot > 90) { |
| 122 controller.xRot = 90; |
| 123 } |
| 124 // Send the onchange event to any listener. |
| 125 if (controller.onchange != null) { |
| 126 controller.onchange(controller.xRot, controller.yRot); |
| 127 } |
| 128 } |
| 129 }; |
| 130 } |
OLD | NEW |