1
1
import { Chart , View } from '@antv/g2' ;
2
2
import DataSet from '@antv/data-set' ;
3
- import { isArray , isFunction , isNumber } from '@antv/util' ;
3
+ import { isArray , isFunction , isNumber , isString } from '@antv/util' ;
4
4
import { Params } from '../../core/adaptor' ;
5
5
import { log , LEVEL , getContainerSize } from '../../utils' ;
6
6
import { WordCloudOptions } from './types' ;
@@ -23,7 +23,7 @@ export function transform(params: Params<WordCloudOptions>) {
23
23
dv . transform ( {
24
24
type : 'tag-cloud' ,
25
25
fields : [ wordField , weightField ] ,
26
- imageMask : getImageMask ( imageMask ) ,
26
+ imageMask : imageMask as HTMLImageElement ,
27
27
font : fontFamily ,
28
28
fontSize : getFontSize ( options , range ) ,
29
29
fontWeight : fontWeight ,
@@ -43,7 +43,7 @@ export function transform(params: Params<WordCloudOptions>) {
43
43
*/
44
44
function getSize ( params : Params < WordCloudOptions > & { chart : Chart } ) {
45
45
const { chart, options } = params ;
46
- const { autoFit } = options ;
46
+ const { autoFit = true } = options ;
47
47
let { width, height } = chart ;
48
48
49
49
// 由于词云图每个词语的坐标都是先通过 DataSet 根据图表宽高计算出来的,
@@ -105,8 +105,31 @@ function normalPadding(padding: number | number[] | 'auto'): [number, number, nu
105
105
return [ 0 , 0 , 0 , 0 ] ;
106
106
}
107
107
108
- function getImageMask ( img : HTMLImageElement ) {
109
- return img ;
108
+ /**
109
+ * 处理 imageMask 可能为 url 字符串的情况
110
+ * @param img
111
+ * @param callback
112
+ */
113
+ export function processImageMask ( img : HTMLImageElement | string , callback ?: ( img ?: HTMLImageElement ) => void ) {
114
+ if ( img instanceof HTMLImageElement ) {
115
+ callback ( img ) ;
116
+ return ;
117
+ }
118
+ if ( isString ( img ) ) {
119
+ const image = new Image ( ) ;
120
+ image . crossOrigin = 'anonymous' ;
121
+ image . src = img ;
122
+ image . onload = ( ) => {
123
+ callback ( image ) ;
124
+ } ;
125
+ image . onerror = ( ) => {
126
+ log ( LEVEL . ERROR , false , 'image %s load failed !!!' , img ) ;
127
+ callback ( ) ;
128
+ } ;
129
+ return ;
130
+ }
131
+ log ( LEVEL . WARN , img === undefined , 'the type of imageMask option must be String or HTMLImageElement.' ) ;
132
+ callback ( ) ;
110
133
}
111
134
112
135
/**
0 commit comments