forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixel.js
More file actions
182 lines (167 loc) · 5.9 KB
/
Pixel.js
File metadata and controls
182 lines (167 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import {SuperMap} from '../SuperMap';
/**
* @class SuperMap.Pixel
* @category BaseTypes Geometry
* @classdesc 此类用 x,y 坐标描绘屏幕坐标(像素点)。
* @param {number} [x=0.0] - x 坐标。
* @param {number} [y=0.0] - y 坐标。
* @param {SuperMap.Pixel.Mode} [mode=SuperMap.Pixel.Mode.LeftTop] - 坐标模式。
*
* @example
* //单独创建一个对象
* var pixcel = new SuperMap.Pixel(100,50);
*
* //依据 size 创建
* var size = new SuperMap.Size(21,25);
* var offset = new SuperMap.Pixel(-(size.w/2), -size.h);
*/
export class Pixel {
constructor(x, y, mode) {
/**
* @member {number} [SuperMap.Pixel.prototype.x=0.0]
* @description x 坐标。
*/
this.x = x ? parseFloat(x) : 0.0;
/**
* @member {number} [SuperMap.Pixel.prototype.y=0.0]
* @description y 坐标。
*/
this.y = y ? parseFloat(y) : 0.0;
/**
* @member {SuperMap.Pixel.Mode} [SuperMap.Pixel.prototype.mode=SuperMap.Pixel.Mode.LeftTop]
* @description 坐标模式,有左上、右上、右下、左下这几种模式,分别表示相对于左上角、右上角、右下角、左下角的坐标。
*/
this.mode = mode;
this.CLASS_NAME = "SuperMap.Pixel";
/**
* @enum SuperMap.Pixel.Mode
* @readonly
* @description 模式。
* @type {string}
*/
SuperMap.Pixel.Mode = {
/** 左上模式。*/
LeftTop: "lefttop",
/** 右上模式。 */
RightTop: "righttop",
/** 右下模式。 */
RightBottom: "rightbottom",
/** 左下模式。 */
LeftBottom: "leftbottom"
};
}
/**
* @function SuperMap.Pixel.prototype.toString
* @description 返回此对象的字符串形式。
* @example
*
* var pixcel = new SuperMap.Pixel(100,50);
* var str = pixcel.toString();
*
* @returns {string} 例如: "x=200.4,y=242.2"
*/
toString() {
return ("x=" + this.x + ",y=" + this.y);
}
/**
* @function SuperMap.Pixel.prototype.clone
* @description 克隆当前的 pixel 对象。
* @example
* var pixcel = new SuperMap.Pixel(100,50);
* var pixcel2 = pixcel.clone();
* @returns {SuperMap.Pixel} 返回一个新的与当前 pixel 对象有相同 x、y 坐标的 pixel 对象。
*/
clone() {
return new Pixel(this.x, this.y, this.mode);
}
/**
* @function SuperMap.Pixel.prototype.equals
* @description 比较两 pixel 是否相等。
* @example
* var pixcel = new SuperMap.Pixel(100,50);
* var pixcel2 = new SuperMap.Pixel(100,50);
* var isEquals = pixcel.equals(pixcel2);
*
* @param {SuperMap.Pixel} px - 用于比较相等的 pixel 对象。
* @returns {boolean} 如果传入的像素点和当前像素点相同返回 true,如果不同或传入参数为 NULL 则返回 false。
*/
equals(px) {
var equals = false;
if (px != null) {
equals = ((this.x == px.x && this.y == px.y) ||
(isNaN(this.x) && isNaN(this.y) && isNaN(px.x) && isNaN(px.y)));
}
return equals;
}
/**
* @function SuperMap.Pixel.prototype.distanceTo
* @description 返回两个 pixel 的距离。
* @example
* var pixcel = new SuperMap.Pixel(100,50);
* var pixcel2 = new SuperMap.Pixel(110,30);
* var distance = pixcel.distanceTo(pixcel2);
*
* @param {SuperMap.Pixel} px - 用于计算的一个 pixel。
* @returns {float} 作为参数传入的像素与当前像素点的距离。
*/
distanceTo(px) {
return Math.sqrt(
Math.pow(this.x - px.x, 2) +
Math.pow(this.y - px.y, 2)
);
}
/**
* @function SuperMap.Pixel.prototype.add
* @description 在原来像素坐标基础上,x 值加上传入的 x 参数,y 值加上传入的 y 参数。
* @example
* var pixcel = new SuperMap.Pixel(100,50);
* //pixcel2是新的对象
* var pixcel2 = pixcel.add(20,30);
*
* @param {number} x - 传入的 x 值。
* @param {number} y - 传入的 y 值。
* @returns {SuperMap.Pixel} 返回一个新的 pixel 对象,该 pixel 是由当前的 pixel 与传入的 x,y 相加得到。
*/
add(x, y) {
if ((x == null) || (y == null)) {
throw new TypeError('Pixel.add cannot receive null values');
}
return new Pixel(this.x + x, this.y + y);
}
/**
* @function SuperMap.Pixel.prototype.offset
* @description 通过传入的 {@link SuperMap.Pixel} 参数对原屏幕坐标进行偏移。
* @example
* var pixcel = new SuperMap.Pixel(100,50);
* var pixcel2 = new SuperMap.Pixel(130,20);
* //pixcel3 是新的对象
* var pixcel3 = pixcel.offset(pixcel2);
*
* @param {SuperMap.Pixel} px - 传入的 <SuperMap.Pixel> 对象。
* @returns {SuperMap.Pixel} 返回一个新的 pixel,该 pixel 是由当前的 pixel 对象的 x,y 值与传入的 Pixel 对象的 x,y 值相加得到。
*/
offset(px) {
var newPx = this.clone();
if (px) {
newPx = this.add(px.x, px.y);
}
return newPx;
}
/**
*
* @function SuperMap.Pixel.prototype.destroy
* @description 销毁此对象。销毁后此对象的所有属性为 null,而不是初始值。
* @example
* var pixcel = new SuperMap.Pixel(100,50);
* pixcel.destroy();
*/
destroy() {
this.x = null;
this.y = null;
this.mode = null;
}
}
SuperMap.Pixel = Pixel;