Skip to content

Commit 2a4b411

Browse files
authored
symbol VectorMarker support roundrectangle type (#2587)
* symbol VectorMarker support roundrectangle type * fix lint
1 parent 2123594 commit 2a4b411

File tree

6 files changed

+62
-13
lines changed

6 files changed

+62
-13
lines changed

packages/maptalks/src/core/Browser.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type BrowserType = {
3636
ie10: boolean;
3737
webgl: boolean;
3838
imageBitMap: boolean;
39+
roundRect: boolean;
3940
resizeObserver: boolean;
4041
btoa: boolean;
4142
decodeImageInWorker: boolean;
@@ -83,7 +84,8 @@ if (!IS_NODE) {
8384
resizeObserver = typeof window !== 'undefined' && isFunction(window.ResizeObserver),
8485
btoa = typeof window !== 'undefined' && isFunction(window.btoa),
8586
proxy = typeof window !== 'undefined' && isFunction(window.Proxy),
86-
requestIdleCallback = typeof window !== 'undefined' && isFunction(window.requestIdleCallback);
87+
requestIdleCallback = typeof window !== 'undefined' && isFunction(window.requestIdleCallback),
88+
roundRect = typeof CanvasRenderingContext2D !== 'undefined' && isFunction(CanvasRenderingContext2D.prototype.roundRect);
8789

8890

8991
let chromeVersion: any = 0;
@@ -122,7 +124,7 @@ if (!IS_NODE) {
122124
try {
123125

124126
const opts = Object.defineProperty({}, 'passive', {
125-
get: function() {
127+
get: function () {
126128
supportsPassive = true;
127129
}
128130
});
@@ -175,6 +177,7 @@ if (!IS_NODE) {
175177

176178
webgl: webgl,
177179
imageBitMap,
180+
roundRect,
178181
resizeObserver,
179182
btoa,
180183
decodeImageInWorker,
@@ -204,5 +207,8 @@ if (!IS_NODE) {
204207
// // }
205208
// }
206209
};
210+
if (!Browser.roundRect) {
211+
console.warn('current env the canvas not support roundRect');
212+
}
207213
}
208214
export default Browser;

packages/maptalks/src/core/Canvas.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,43 @@ const Canvas = {
13071307
Canvas._stroke(ctx, lineOpacity);
13081308
},
13091309

1310+
roundRect(ctx: CanvasRenderingContext2D, points: Array<Point>, lineOpacity?: number, fillOpacity?: number) {
1311+
//canvas not support roundRect https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/roundRect
1312+
if (!Browser.roundRect) {
1313+
return;
1314+
}
1315+
let minx = Infinity, miny = Infinity, maxx = -Infinity, maxy = -Infinity;
1316+
for (let i = 0, len = points.length; i < len; i++) {
1317+
const p = points[i];
1318+
const { x, y } = p;
1319+
minx = Math.min(minx, x);
1320+
miny = Math.min(miny, y);
1321+
maxx = Math.max(maxx, x);
1322+
maxy = Math.max(maxy, y);
1323+
1324+
}
1325+
const width = Math.max(maxx - minx), height = Math.abs(maxy - miny);
1326+
if (width * height <= 0) {
1327+
return;
1328+
}
1329+
const min = Math.min(width, height);
1330+
ctx.roundRect(minx, miny, width, height, min / 4);
1331+
1332+
const globalAlpha = ctx.globalAlpha;
1333+
if (isNumber(fillOpacity) && fillOpacity < 1) {
1334+
ctx.globalAlpha = 1;
1335+
ctx.globalAlpha *= fillOpacity;
1336+
}
1337+
ctx.fill();
1338+
ctx.globalAlpha = globalAlpha;
1339+
if (isNumber(lineOpacity) && lineOpacity < 1) {
1340+
ctx.globalAlpha = 1;
1341+
ctx.globalAlpha *= lineOpacity;
1342+
}
1343+
ctx.stroke();
1344+
ctx.globalAlpha = globalAlpha;
1345+
},
1346+
13101347
//@internal
13111348
_multiClip(ctx, points) {
13121349
if (!points || points.length === 0) return;

packages/maptalks/src/core/util/draw.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,23 @@ export function drawVectorMarker(ctx: CanvasRenderingContext2D, point, symbol, r
5757
//线类型
5858
Canvas.path(ctx, vectorArray.slice(0, 2), lineOpacity);
5959
Canvas.path(ctx, vectorArray.slice(2, 4), lineOpacity);
60-
} else if (markerType === 'diamond' || markerType === 'bar' || markerType === 'square' || markerType === 'rectangle' || markerType === 'triangle') {
60+
} else if (markerType === 'diamond' || markerType === 'bar' || markerType === 'square' || markerType === 'rectangle' || markerType === 'triangle' || markerType === 'roundrectangle') {
6161
if (markerType === 'bar') {
6262
point = point.add(0, -hLineWidth);
63-
} else if (markerType === 'rectangle') {
63+
} else if (markerType === 'rectangle' || markerType === 'roundrectangle') {
6464
point = point.add(hLineWidth, hLineWidth);
6565
}
6666
for (let j = vectorArray.length - 1; j >= 0; j--) {
6767
vectorArray[j]._add(point);
6868
}
69-
//面类型
70-
Canvas.polygon(ctx, vectorArray, lineOpacity, fillOpacity);
69+
70+
if (markerType === 'roundrectangle') {
71+
//round rectangle, draw the corners
72+
Canvas.roundRect(ctx, vectorArray, lineOpacity, fillOpacity);
73+
} else {
74+
//面类型
75+
Canvas.polygon(ctx, vectorArray, lineOpacity, fillOpacity);
76+
}
7177
} else if (markerType === 'pin') {
7278
point = point.add(0, -hLineWidth);
7379
for (let j = vectorArray.length - 1; j >= 0; j--) {
@@ -140,7 +146,7 @@ export function translateMarkerLineAndFill<T extends Partial<TemplateSymbol>>(s:
140146
return result;
141147
}
142148

143-
export type MarkerType = 'triangle' | 'cross' | 'diamond' | 'square' | 'rectangle' | 'x' | 'bar' | 'pin' | 'pie'
149+
export type MarkerType = 'triangle' | 'cross' | 'diamond' | 'square' | 'rectangle' | 'roundrectangle' | 'x' | 'bar' | 'pin' | 'pie'
144150

145151
export function getVectorMarkerPoints(markerType: MarkerType, width: number, height: number) {
146152
//half height and half width
@@ -172,7 +178,7 @@ export function getVectorMarkerPoints(markerType: MarkerType, width: number, hei
172178
v2 = new Point((left + hw), (top - hh));
173179
v3 = new Point((left - hw), (top - hh));
174180
return [v0, v1, v2, v3];
175-
} else if (markerType === 'rectangle') {
181+
} else if (markerType === 'rectangle' || markerType === 'roundrectangle') {
176182
v0 = new Point(left, top);
177183
v1 = v0.add(width, 0);
178184
v2 = v0.add(width, height);

packages/maptalks/src/core/util/marker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export function getVectorMarkerFixedExtent(out: PointExtent, symbol: any, size?:
152152
}
153153

154154
export function getDefaultHAlign(markerType?: MarkerType) {
155-
if (markerType === 'rectangle') {
155+
if (markerType === 'rectangle' || markerType === 'roundrectangle') {
156156
return 'right';
157157
} else {
158158
return 'middle';
@@ -162,7 +162,7 @@ export function getDefaultHAlign(markerType?: MarkerType) {
162162
export function getDefaultVAlign(markerType?: MarkerType) {
163163
if (markerType === 'bar' || markerType === 'pie' || markerType === 'pin') {
164164
return 'top';
165-
} else if (markerType === 'rectangle') {
165+
} else if (markerType === 'rectangle' || markerType === 'roundrectangle') {
166166
return 'bottom';
167167
} else {
168168
return 'middle';
@@ -213,7 +213,7 @@ function rotateExtent(fixedExtent: PointExtent, angle: number) {
213213
return ROTATE_EXTENT.convertTo(p => p._rotate(angle), fixedExtent);
214214
}
215215

216-
export function getMarkerRotation(symbol:any, prop = 'markerRotation') {
216+
export function getMarkerRotation(symbol: any, prop = 'markerRotation') {
217217
const r = symbol[prop];
218218
if (!isNumber(r)) {
219219
return 0;

packages/maptalks/src/renderer/geometry/symbolizers/VectorMarkerSymbolizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export default class VectorMarkerSymbolizer extends PointSymbolizer {
183183
const markerType = this.style['markerType'];
184184
if (markerType === 'bar' || markerType === 'pie' || markerType === 'pin') {
185185
return new Point(w / 2, h - margin);
186-
} else if (markerType === 'rectangle') {
186+
} else if (markerType === 'rectangle' || markerType === 'roundrectangle') {
187187
return new Point(margin, margin);
188188
} else {
189189
return new Point(w / 2, h / 2);

packages/maptalks/src/symbol/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export type FileMarkerSymbol = {
7070
} & MarkerCommonSymbol & SymbolCommon;
7171

7272
export type VectorMarkerSymbol = {
73-
markerType: 'ellipse' | 'cross' | 'x' | 'diamond' | 'bar' | 'square' | 'rectangle' | 'triangle' | 'pin' | 'pie';
73+
markerType: 'ellipse' | 'cross' | 'x' | 'diamond' | 'bar' | 'square' | 'rectangle' | 'roundrectangle' | 'triangle' | 'pin' | 'pie';
7474
markerFill?: SymbolColorType;
7575
markerFillPatternFile?: string;
7676
markerFillOpacity?: number;

0 commit comments

Comments
 (0)