11/* Copyright© 2000 - 2020 SuperMap Software Co.Ltd. All rights reserved.
22 * This program are made available under the terms of the Apache License, Version 2.0
33 * 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' ;
106import olObject from 'ol/Object' ;
117import * as olStyle from 'ol/style' ;
128import Point from 'ol/geom/Point' ;
139import * as olRender from 'ol/render' ;
1410
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-
2211//获取某像素坐标点pixelP相对于中心center进行缩放scaleRatio倍后的像素点坐标。
2312function scale ( pixelP , center , scaleRatio ) {
2413 let x = ( pixelP [ 0 ] - center [ 0 ] ) * scaleRatio + center [ 0 ] ;
@@ -66,8 +55,8 @@ export class GraphicCanvasRenderer extends olObject {
6655 this . context = Util . createCanvasContext2D ( this . mapWidth , this . mapHeight ) ;
6756 this . context . scale ( this . pixelRatio , this . pixelRatio ) ;
6857 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' ;
7160 this . _registerEvents ( ) ;
7261 }
7362
@@ -93,8 +82,8 @@ export class GraphicCanvasRenderer extends olObject {
9382
9483 this . canvas . width = this . mapWidth ;
9584 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' ;
9887 }
9988
10089 _clearAndRedraw ( ) {
@@ -108,7 +97,6 @@ export class GraphicCanvasRenderer extends olObject {
10897
10998 _clearBuffer ( ) { }
11099
111-
112100 /**
113101 * @private
114102 * @function GraphicCanvasRenderer.prototype.getCanvas
@@ -126,13 +114,9 @@ export class GraphicCanvasRenderer extends olObject {
126114 */
127115 drawGraphics ( graphics ) {
128116 this . graphics_ = graphics || [ ] ;
129-
130117 let mapWidth = this . mapWidth / this . pixelRatio ;
131118 let mapHeight = this . mapHeight / this . pixelRatio ;
132- let width = this . width ;
133- let height = this . height ;
134119
135- let offset = [ ( mapWidth - width ) / 2 , ( mapHeight - height ) / 2 ] ;
136120 let vectorContext = olRender . toContext ( this . context , {
137121 size : [ mapWidth , mapHeight ] ,
138122 pixelRatio : this . pixelRatio
@@ -141,7 +125,7 @@ export class GraphicCanvasRenderer extends olObject {
141125 let me = this ,
142126 layer = me . layer ,
143127 map = layer . map ;
144- graphics . map ( function ( graphic ) {
128+ graphics . map ( function ( graphic ) {
145129 let style = graphic . getStyle ( ) || defaultStyle ;
146130 if ( me . selected === graphic ) {
147131 let defaultHighLightStyle = style ;
@@ -171,22 +155,25 @@ export class GraphicCanvasRenderer extends olObject {
171155 }
172156 style = me . highLightStyle || defaultHighLightStyle ;
173157 }
174- vectorContext . setStyle ( new olStyle . Style ( {
175- image : style
176- } ) ) ;
158+ vectorContext . setStyle (
159+ new olStyle . Style ( {
160+ image : style
161+ } )
162+ ) ;
177163 let geometry = graphic . getGeometry ( ) ;
178164 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 ] ] ;
185174 let pixelGeometry = new Point ( result ) ;
186175 vectorContext . drawGeometry ( pixelGeometry ) ;
187176 return graphic ;
188177 } ) ;
189178 }
190-
191-
192- }
179+ }
0 commit comments