Skip to content

Commit 621b9ff

Browse files
authored
fix gradientPath color alpha Not effective (#2505)
* fix gradientPath color alpha Not effective * spec * update spec * update spec * update spec * update spec * update spec * update spec * fire tiledelete event
1 parent b885052 commit 621b9ff

File tree

3 files changed

+79
-9
lines changed

3 files changed

+79
-9
lines changed

packages/maptalks/src/core/Canvas.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,10 @@ function getSegmentPercentPoint(p1: Point, p2: Point, percent: number) {
515515
return new Point(x1 + dx * percent, y1 + dy * percent);
516516
}
517517

518+
function rgbaToCSSRGBA(r: number, g: number, b: number, a: number) {
519+
return `rgba(${r}, ${g}, ${b}, ${a / 255})`;
520+
}
521+
518522
const Canvas = {
519523
getCanvas2DContext(canvas: HTMLCanvasElement) {
520524
return canvas.getContext('2d', { willReadFrequently: true });
@@ -1150,7 +1154,7 @@ const Canvas = {
11501154
let preX, preY, currentX, currentY, nextPoint;
11511155

11521156
const [r, g, b, a] = colorIn.getColor(0);
1153-
preColor = `rgba(${r}, ${g}, ${b}, ${a})`;
1157+
preColor = rgbaToCSSRGBA(r, g, b, a);
11541158

11551159
const firstPoint = points[0];
11561160
preX = firstPoint.x;
@@ -1199,7 +1203,7 @@ const Canvas = {
11991203
//segment的步数小于minStep
12001204
if (percent <= minStep) {
12011205
const [r, g, b, a] = colorIn.getColor(step + percent);
1202-
color = `rgba(${r}, ${g}, ${b}, ${a})`;
1206+
color = rgbaToCSSRGBA(r, g, b, a);
12031207
currentX = x;
12041208
currentY = y;
12051209
drawSegment();
@@ -1210,7 +1214,7 @@ const Canvas = {
12101214
for (let n = 1; n <= segments; n++) {
12111215
const tempStep = Math.min((n * minStep), percent);
12121216
const [r, g, b, a] = colorIn.getColor(step + tempStep);
1213-
color = `rgba(${r}, ${g}, ${b}, ${a})`;
1217+
color = rgbaToCSSRGBA(r, g, b, a);
12141218
if (color === preColor) {
12151219
continue;
12161220
}
@@ -1277,7 +1281,7 @@ const Canvas = {
12771281
}
12781282

12791283
if (ctx.lineColorIn) {
1280-
this._gradientPath(ctx, points, lineDashArray, lineOpacity);
1284+
Canvas._gradientPath(ctx, points, lineDashArray, lineOpacity);
12811285
} else {
12821286
ctx.beginPath();
12831287
ctx.moveTo(points[0].x, points[0].y);

packages/maptalks/src/renderer/layer/tilelayer/TileLayerRendererable.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,20 @@ const TileLayerRenderable = function <T extends MixinConstructor>(Base: T) {
12041204
tile.image.onload = null;
12051205
tile.image.onerror = null;
12061206
}
1207+
const layer = this.layer;
1208+
if (layer) {
1209+
/**
1210+
* tiledelete event, fired when tile is delete.
1211+
*
1212+
* @event TileLayer#tiledelete
1213+
* @type {Object}
1214+
* @property {String} type - tiledelete
1215+
* @property {TileLayer} target - tile layer
1216+
* @property {Object} tileInfo - tile info
1217+
* @property {Image} tileImage - tile image
1218+
*/
1219+
layer.fire('tiledelete', { tile: tile.info, tileImage: tile.image });
1220+
}
12071221
}
12081222

12091223
//@internal
@@ -1313,12 +1327,12 @@ export type TilesInViewType = {
13131327
}
13141328

13151329
export interface TileGrid {
1316-
extent: Extent;
1317-
count: number;
1318-
tiles: Tile[];
1330+
extent: Extent;
1331+
count: number;
1332+
tiles: Tile[];
13191333
parents: any[];
1320-
offset: number[];
1321-
zoom: number;
1334+
offset: number[];
1335+
zoom: number;
13221336
}
13231337

13241338
export interface TileGrids {

packages/maptalks/test/geometry/symbol/StrokeAndFillSpec.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,58 @@ describe('StrokeAndFillSpec', function () {
620620
}, 1000);
621621

622622

623+
});
624+
625+
626+
it('#2504 lineColor gradient color with alpha ', function (done) {
627+
628+
const c1 = map.getCenter(), c2 = c1.add(0.1, 0);
629+
map.setBearing(0);
630+
const lineDx = -5;
631+
var line = new maptalks.LineString([c1, c2], {
632+
symbol: {
633+
'lineColor': {
634+
'type': 'linear',
635+
'colorStops': [
636+
[0.00, 'rgba(255,0,0,0.1)'],
637+
[1.00, 'rgba(0,0,255,0.5)']
638+
]
639+
},
640+
'lineWidth': 10,
641+
lineDx
642+
}
643+
})
644+
var layer = new maptalks.VectorLayer('v').addTo(map);
645+
line.addTo(layer);
646+
map.fitExtent(layer.getExtent());
647+
setTimeout(() => {
648+
const p1 = map.coordinateToContainerPoint(c1);
649+
const p2 = map.coordinateToContainerPoint(c2);
650+
console.log(p1, p2);
651+
652+
var canvas = map.getRenderer().canvas;
653+
var context = canvas.getContext('2d');
654+
655+
var imgData = context.getImageData(Math.round(p1.x), Math.round(p1.y), 1, 1).data;
656+
var imgData1 = context.getImageData(Math.round(p2.x + lineDx * 2), Math.round(p2.y), 1, 1).data;
657+
658+
// eslint-disable-next-line no-undef
659+
if (isWindows()) {
660+
expect([...imgData]).to.be.eql([255, 0, 5, 51]);
661+
} else {
662+
expect([...imgData]).to.be.eql([245, 0, 5, 53]);
663+
}
664+
// eslint-disable-next-line no-undef
665+
if (isWindows()) {
666+
expect([...imgData1]).to.be.eql([3, 0, 254, 190]);
667+
} else {
668+
expect([...imgData1]).to.be.eql([4, 0, 254, 190]);
669+
}
670+
671+
done();
672+
}, 1000);
673+
674+
623675
});
624676
});
625677

0 commit comments

Comments
 (0)