1
1
/* Copyright© 2000 - 2020 SuperMap Software Co.Ltd. All rights reserved.
2
2
* This program are made available under the terms of the Apache License, Version 2.0
3
3
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
4
- import {
5
- CommonUtil
6
- } from "@supermap/iclient-common" ;
7
- import {
8
- Util
9
- } from '../../core/Util' ;
4
+ import { CommonUtil } from '@supermap/iclient-common' ;
5
+ import { Util } from '../../core/Util' ;
10
6
import olObject from 'ol/Object' ;
11
7
import * as olStyle from 'ol/style' ;
12
8
import Point from 'ol/geom/Point' ;
13
9
import * as olRender from 'ol/render' ;
14
10
15
- //获取某像素坐标点pixelP绕中心center逆时针旋转rotation弧度后的像素点坐标。
16
- function rotate ( pixelP , rotation , center ) {
17
- let x = Math . cos ( rotation ) * ( pixelP [ 0 ] - center [ 0 ] ) - Math . sin ( rotation ) * ( pixelP [ 1 ] - center [ 1 ] ) + center [ 0 ] ;
18
- let y = Math . sin ( rotation ) * ( pixelP [ 0 ] - center [ 0 ] ) + Math . cos ( rotation ) * ( pixelP [ 1 ] - center [ 1 ] ) + center [ 1 ] ;
19
- return [ x , y ] ;
20
- }
21
-
22
11
//获取某像素坐标点pixelP相对于中心center进行缩放scaleRatio倍后的像素点坐标。
23
12
function scale ( pixelP , center , scaleRatio ) {
24
13
let x = ( pixelP [ 0 ] - center [ 0 ] ) * scaleRatio + center [ 0 ] ;
@@ -66,8 +55,8 @@ export class GraphicCanvasRenderer extends olObject {
66
55
this . context = Util . createCanvasContext2D ( this . mapWidth , this . mapHeight ) ;
67
56
this . context . scale ( this . pixelRatio , this . pixelRatio ) ;
68
57
this . canvas = this . context . canvas ;
69
- this . canvas . style . width = this . width + "px" ;
70
- this . canvas . style . height = this . height + "px" ;
58
+ this . canvas . style . width = this . width + 'px' ;
59
+ this . canvas . style . height = this . height + 'px' ;
71
60
this . _registerEvents ( ) ;
72
61
}
73
62
@@ -93,8 +82,8 @@ export class GraphicCanvasRenderer extends olObject {
93
82
94
83
this . canvas . width = this . mapWidth ;
95
84
this . canvas . height = this . mapHeight ;
96
- this . canvas . style . width = this . width + "px" ;
97
- this . canvas . style . height = this . height + "px" ;
85
+ this . canvas . style . width = this . width + 'px' ;
86
+ this . canvas . style . height = this . height + 'px' ;
98
87
}
99
88
100
89
_clearAndRedraw ( ) {
@@ -108,7 +97,6 @@ export class GraphicCanvasRenderer extends olObject {
108
97
109
98
_clearBuffer ( ) { }
110
99
111
-
112
100
/**
113
101
* @private
114
102
* @function GraphicCanvasRenderer.prototype.getCanvas
@@ -126,13 +114,9 @@ export class GraphicCanvasRenderer extends olObject {
126
114
*/
127
115
drawGraphics ( graphics ) {
128
116
this . graphics_ = graphics || [ ] ;
129
-
130
117
let mapWidth = this . mapWidth / this . pixelRatio ;
131
118
let mapHeight = this . mapHeight / this . pixelRatio ;
132
- let width = this . width ;
133
- let height = this . height ;
134
119
135
- let offset = [ ( mapWidth - width ) / 2 , ( mapHeight - height ) / 2 ] ;
136
120
let vectorContext = olRender . toContext ( this . context , {
137
121
size : [ mapWidth , mapHeight ] ,
138
122
pixelRatio : this . pixelRatio
@@ -141,7 +125,7 @@ export class GraphicCanvasRenderer extends olObject {
141
125
let me = this ,
142
126
layer = me . layer ,
143
127
map = layer . map ;
144
- graphics . map ( function ( graphic ) {
128
+ graphics . map ( function ( graphic ) {
145
129
let style = graphic . getStyle ( ) || defaultStyle ;
146
130
if ( me . selected === graphic ) {
147
131
let defaultHighLightStyle = style ;
@@ -171,22 +155,25 @@ export class GraphicCanvasRenderer extends olObject {
171
155
}
172
156
style = me . highLightStyle || defaultHighLightStyle ;
173
157
}
174
- vectorContext . setStyle ( new olStyle . Style ( {
175
- image : style
176
- } ) ) ;
158
+ vectorContext . setStyle (
159
+ new olStyle . Style ( {
160
+ image : style
161
+ } )
162
+ ) ;
177
163
let geometry = graphic . getGeometry ( ) ;
178
164
let coordinate = geometry . getCoordinates ( ) ;
179
- let pixelP = map . getPixelFromCoordinate ( coordinate ) ;
180
- let rotation = - map . getView ( ) . getRotation ( ) ;
181
- let center = map . getPixelFromCoordinate ( map . getView ( ) . getCenter ( ) ) ;
182
- let scaledP = scale ( pixelP , center , 1 ) ;
183
- let rotatedP = rotate ( scaledP , rotation , center ) ;
184
- let result = [ rotatedP [ 0 ] + offset [ 0 ] , rotatedP [ 1 ] + offset [ 1 ] ] ;
165
+ let center = map . getView ( ) . getCenter ( ) ;
166
+ let mapCenterPx = map . getPixelFromCoordinate ( center ) ;
167
+ let resolution = map . getView ( ) . getResolution ( ) ;
168
+ let x = ( coordinate [ 0 ] - center [ 0 ] ) / resolution ;
169
+ let y = ( center [ 1 ] - coordinate [ 1 ] ) / resolution ;
170
+ let scaledP = [ x + mapCenterPx [ 0 ] , y + mapCenterPx [ 1 ] ] ;
171
+ scaledP = scale ( scaledP , mapCenterPx , 1 ) ;
172
+ //处理放大或缩小级别*/
173
+ let result = [ scaledP [ 0 ] + me . offset [ 0 ] , scaledP [ 1 ] + me . offset [ 1 ] ] ;
185
174
let pixelGeometry = new Point ( result ) ;
186
175
vectorContext . drawGeometry ( pixelGeometry ) ;
187
176
return graphic ;
188
177
} ) ;
189
178
}
190
-
191
-
192
- }
179
+ }
0 commit comments