@@ -6,7 +6,7 @@ import { StringExt } from '@supermap/iclient-common/commontypes/BaseTypes';
66import { StyleMap } from '../overlay/vectortile/StyleMap' ;
77import { DeafultCanvasStyle } from '../overlay/vectortile/DeafultCanvasStyle' ;
88import { Util } from '../core/Util' ;
9- import canvg from 'canvg' ;
9+ import Canvg from 'canvg' ;
1010import Style from 'ol/style/Style' ;
1111import Icon from 'ol/style/Icon' ;
1212import CircleStyle from 'ol/style/Circle' ;
@@ -23,7 +23,6 @@ const ZERO = 0.0000001;
2323 * @private
2424 */
2525export class StyleUtils {
26-
2726 /**
2827 * @function StyleUtils.getValidStyleFromLayerInfo
2928 * @description 通过图层信息获取有效的样式。
@@ -595,9 +594,9 @@ export class StyleUtils {
595594 * @description 将样式对象转换成openlayer要求的ol.style
596595 * @param {string } style - 样式对象
597596 * @param {string } type - feature的类型
598- * @returns {ol.style.Style }
597+ * @returns {Promise< ol.style.Style> }
599598 */
600- static toOpenLayersStyle ( style , type ) {
599+ static async toOpenLayersStyle ( style , type ) {
601600 style = style || this . getDefaultStyle ( ) ;
602601 let olStyle = new Style ( ) ;
603602 let newImage , newFill , newStroke ;
@@ -633,7 +632,7 @@ export class StyleUtils {
633632 this . svgDiv = document . createElement ( 'div' ) ;
634633 document . body . appendChild ( this . svgDiv ) ;
635634 }
636- this . getCanvasFromSVG ( src , this . svgDiv , ( canvas ) => {
635+ await this . getCanvasFromSVG ( src , this . svgDiv , ( canvas ) => {
637636 newImage = new Icon ( {
638637 img : canvas ,
639638 scale : radius / canvas . width ,
@@ -965,33 +964,39 @@ export class StyleUtils {
965964 * @param {Object } divDom - div的dom对象
966965 * @param {function } callBack - 转换成功执行的回调函数
967966 */
968- static getCanvasFromSVG ( svgUrl , divDom , callBack ) {
967+ static async getCanvasFromSVG ( svgUrl , divDom , callBack ) {
969968 //一个图层对应一个canvas
970- let canvgs = window . canvg ? window . canvg : canvg ;
969+ const canvgs = window . canvg && window . canvg . default ? window . canvg . default : Canvg ;
971970 let canvas = document . createElement ( 'canvas' ) ;
972971 canvas . id = 'dataviz-canvas-' + Util . newGuid ( 8 ) ;
973972 canvas . style . display = "none" ;
974973 divDom . appendChild ( canvas ) ;
975974 try {
976- canvgs ( canvas . id , svgUrl , {
975+ const ctx = canvas . getContext ( '2d' ) ;
976+ const v = await canvgs . from ( ctx , svgUrl , {
977977 ignoreMouse : true ,
978978 ignoreAnimation : true ,
979- renderCallback : function ( ) {
980- if ( canvas . width > 300 || canvas . height > 300 ) {
981- // Util.showMessage(DataViz.Language.sizeIsWrong,'WARNING');
982- return ;
983- }
984- callBack ( canvas ) ;
985- } ,
986- forceRedraw : function ( ) {
987- return false
988- }
989- } ) ;
979+ forceRedraw : ( ) => false
980+ } )
981+ v . start ( ) ;
982+ if ( canvas . width > 300 || canvas . height > 300 ) {
983+ return ;
984+ }
985+ callBack ( canvas ) ;
990986 } catch ( e ) {
991987 return ;
992988 }
993989 }
994990
991+ /**
992+ * @function StyleUtils.stopCanvg
993+ * @description 调用Canvg实例的stop();
994+ */
995+ static stopCanvg ( ) {
996+ this . canvgsV . forEach ( v => v . stop ( ) ) ;
997+ this . canvgsV = [ ] ;
998+ }
999+
9951000 /**
9961001 * @function StyleUtils.getMarkerDefaultStyle 获取默认标注图层feature的样式
9971002 * @param {string } featureType feature的类型
@@ -1041,16 +1046,16 @@ export class StyleUtils {
10411046 * @param {string } styleParams 样式参数
10421047 * @param {string } featureType feature类型
10431048 * @param {boolean } isRank 是否为等级符号
1044- * @returns {Object } style对象
1049+ * @returns {Promise<ol.style.Style> }
10451050 */
1046- static getOpenlayersStyle ( styleParams , featureType , isRank ) {
1051+ static async getOpenlayersStyle ( styleParams , featureType , isRank ) {
10471052 let style ;
10481053 if ( styleParams . type === "BASIC_POINT" ) {
1049- style = this . toOpenLayersStyle ( styleParams , featureType ) ;
1054+ style = await this . toOpenLayersStyle ( styleParams , featureType ) ;
10501055 } else if ( styleParams . type === "SYMBOL_POINT" ) {
10511056 style = this . getSymbolStyle ( styleParams , isRank ) ;
10521057 } else if ( styleParams . type === "SVG_POINT" ) {
1053- style = this . getSVGStyle ( styleParams ) ;
1058+ style = await this . getSVGStyle ( styleParams ) ;
10541059 } else if ( styleParams . type === 'IMAGE_POINT' ) {
10551060 style = this . getImageStyle ( styleParams ) ;
10561061 }
@@ -1103,17 +1108,17 @@ export class StyleUtils {
11031108 /**
11041109 * @function StyleUtils.getSVGStyle 获取svg的样式
11051110 * @param {Object } styleParams - 样式参数
1106- * @returns {Object } style对象
1111+ * @returns {Promise<ol.style.Style> }
11071112 */
1108- static getSVGStyle ( styleParams ) {
1113+ static async getSVGStyle ( styleParams ) {
11091114 let style , that = this ;
11101115 if ( ! that . svgDiv ) {
11111116 that . svgDiv = document . createElement ( 'div' ) ;
11121117 document . body . appendChild ( that . svgDiv ) ;
11131118 }
11141119 const { url, radius, offsetX, offsetY, fillOpacity, rotation } = styleParams ;
11151120 let anchor = this . getIconAnchor ( offsetX , offsetY ) ;
1116- StyleUtils . getCanvasFromSVG ( url , that . svgDiv , function ( canvas ) {
1121+ await StyleUtils . getCanvasFromSVG ( url , that . svgDiv , function ( canvas ) {
11171122 style = new Style ( {
11181123 image : new Icon ( {
11191124 img : that . setColorToCanvas ( canvas , styleParams ) ,
0 commit comments