PageRenderTime 40ms CodeModel.GetById 22ms app.highlight 12ms RepoModel.GetById 3ms app.codeStats 0ms

/ext-4.0.7/src/util/Point.js

https://bitbucket.org/srogerf/javascript
JavaScript | 108 lines | 33 code | 11 blank | 64 comment | 10 complexity | a9b1a891b007a9500eda5f4cf288076a MD5 | raw file
  1/*
  2
  3This file is part of Ext JS 4
  4
  5Copyright (c) 2011 Sencha Inc
  6
  7Contact:  http://www.sencha.com/contact
  8
  9GNU General Public License Usage
 10This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
 11
 12If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
 13
 14*/
 15/**
 16 * Represents a 2D point with x and y properties, useful for comparison and instantiation
 17 * from an event:
 18 *
 19 *     var point = Ext.util.Point.fromEvent(e);
 20 *
 21 */
 22Ext.define('Ext.util.Point', {
 23
 24    /* Begin Definitions */
 25    extend: 'Ext.util.Region',
 26
 27    statics: {
 28
 29        /**
 30         * Returns a new instance of Ext.util.Point base on the pageX / pageY values of the given event
 31         * @static
 32         * @param {Event} e The event
 33         * @return {Ext.util.Point}
 34         */
 35        fromEvent: function(e) {
 36            e = (e.changedTouches && e.changedTouches.length > 0) ? e.changedTouches[0] : e;
 37            return new this(e.pageX, e.pageY);
 38        }
 39    },
 40
 41    /* End Definitions */
 42
 43    /**
 44     * Creates a point from two coordinates.
 45     * @param {Number} x X coordinate.
 46     * @param {Number} y Y coordinate.
 47     */
 48    constructor: function(x, y) {
 49        this.callParent([y, x, y, x]);
 50    },
 51
 52    /**
 53     * Returns a human-eye-friendly string that represents this point,
 54     * useful for debugging
 55     * @return {String}
 56     */
 57    toString: function() {
 58        return "Point[" + this.x + "," + this.y + "]";
 59    },
 60
 61    /**
 62     * Compare this point and another point
 63     * @param {Ext.util.Point/Object} The point to compare with, either an instance
 64     * of Ext.util.Point or an object with left and top properties
 65     * @return {Boolean} Returns whether they are equivalent
 66     */
 67    equals: function(p) {
 68        return (this.x == p.x && this.y == p.y);
 69    },
 70
 71    /**
 72     * Whether the given point is not away from this point within the given threshold amount.
 73     * @param {Ext.util.Point/Object} p The point to check with, either an instance
 74     * of Ext.util.Point or an object with left and top properties
 75     * @param {Object/Number} threshold Can be either an object with x and y properties or a number
 76     * @return {Boolean}
 77     */
 78    isWithin: function(p, threshold) {
 79        if (!Ext.isObject(threshold)) {
 80            threshold = {
 81                x: threshold,
 82                y: threshold
 83            };
 84        }
 85
 86        return (this.x <= p.x + threshold.x && this.x >= p.x - threshold.x &&
 87                this.y <= p.y + threshold.y && this.y >= p.y - threshold.y);
 88    },
 89
 90    /**
 91     * Compare this point with another point when the x and y values of both points are rounded. E.g:
 92     * [100.3,199.8] will equals to [100, 200]
 93     * @param {Ext.util.Point/Object} p The point to compare with, either an instance
 94     * of Ext.util.Point or an object with x and y properties
 95     * @return {Boolean}
 96     */
 97    roundedEquals: function(p) {
 98        return (Math.round(this.x) == Math.round(p.x) && Math.round(this.y) == Math.round(p.y));
 99    }
100}, function() {
101    /**
102     * @method
103     * Alias for {@link #translateBy}
104     * @alias Ext.util.Region#translateBy
105     */
106    this.prototype.translate = Ext.util.Region.prototype.translateBy;
107});
108