feat: 新增 CanvasLayer demo

This commit is contained in:
2912401452 2022-03-18 17:37:32 +08:00
parent f52bee04c3
commit b97684cc59
3 changed files with 213 additions and 0 deletions

View File

@ -0,0 +1,140 @@
import { Scene, CanvasLayer } from '@antv/l7';
import { GaodeMap } from '@antv/l7-maps';
let x = 0;
function draw(option) {
const { size, ctx, mapService } = option;
const [ width, height ] = size;
const radius = 30,
rectWidth = radius * 2 * window.devicePixelRatio;
const rectHeight = rectWidth;
ctx.clearRect(0, 0, width, height);
const points = [
{
lng: 108.544921875,
lat: 30.977609093348686,
level: 85,
color: 'rgba( 220,20,60, 0.6)'
},
{
lng: 110.654296875,
lat: 31.090574094954192,
level: 75,
color: 'rgba( 255,140,0, 0.6)'
},
{
lng: 112.5,
lat: 29.80251790576445,
level: 65,
color: 'rgba(255,165,0, 0.6)'
},
{
lng: 114.78515624999999,
lat: 30.64867367928756,
level: 40,
color: 'rgba(30,144,255, 0.6)'
},
{
lng: 116.49902343749999,
lat: 29.84064389983441,
level: 50,
color: 'rgba(30,144,255, 0.6)'
},
{
lng: 118.21289062499999,
lat: 31.16580958786196,
level: 20,
color: 'rgba( 127,255,0, 0.6)'
},
{
lng: 119.091796875,
lat: 32.509761735919426,
level: 50,
color: 'rgba(30,144,255, 0.6)'
},
{
lng: 121.0693359374999,
lat: 31.80289258670676,
level: 45,
color: 'rgba(30,144,255, 0.6)'
}
];
ctx.fillStyle = 'rgb(35,75,225)';
ctx.font = 'normal small-caps bold 14px arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
points.map(point => {
const pixelCenter = mapService.lngLatToContainer([ point.lng, point.lat ]);
const rectStartX = pixelCenter.x - radius;
const rectStartY = pixelCenter.y - radius;
ctx.save();
ctx.fillText(point.level + '%', pixelCenter.x, pixelCenter.y);
ctx.beginPath();
ctx.arc(pixelCenter.x, pixelCenter.y, radius, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(135,206,250,0.2)';
ctx.closePath();
ctx.fill();
ctx.clip();
ctx.beginPath();
ctx.fillStyle = point.color;
ctx.moveTo(rectStartX, pixelCenter.y);
const waterheight = rectStartY + ((100 - point.level) / 100) * rectHeight;
for (let i = 0; i <= rectWidth; i += 10) {
ctx.lineTo(
rectStartX + i,
waterheight + Math.sin(Math.PI * 2 * (i / rectWidth) + x) * 3 + 1
);
}
ctx.lineTo(pixelCenter.x + radius, pixelCenter.y + radius);
ctx.lineTo(rectStartX, pixelCenter.y + radius);
ctx.lineTo(rectStartX, pixelCenter.y);
ctx.closePath();
ctx.fill();
ctx.restore();
return '';
});
return '';
}
const scene = new Scene({
id: 'map',
map: new GaodeMap({
style: 'fresh',
center: [ 115, 31 ],
zoom: 5.0
})
});
scene.on('loaded', () => {
const layer = new CanvasLayer({})
.style({
zIndex: 10,
update: 'aways',
drawingOnCanvas: draw
})
.animate({
enable: true
});
scene.addLayer(layer);
setInterval(() => {
x += 0.1;
layer.style({
drawingOnCanvas: draw
});
scene.render();
return '';
}, 30);
return '';
});

View File

@ -0,0 +1,63 @@
// import { Scene, CanvasLayer } from '@antv/l7';
import { Scene } from '@antv/l7';
import { GaodeMap } from '@antv/l7-maps';
const scene = new Scene({
id: 'map',
map: new GaodeMap({
style: 'fresh',
center: [ 90, 31 ],
zoom: 2
})
});
scene.on('loaded', () => {
// fetch(
// 'https://gw.alipayobjects.com/os/basement_prod/337ddbb7-aa3f-4679-ab60-d64359241955.json'
// )
// .then(res => res.json())
// .then(data => {
// const layer = new CanvasLayer({})
// .style({
// zIndex: 10,
// update: 'dragend',
// drawingOnCanvas: function draw(option) {
// const { size, ctx, mapService } = option;
// const [ width, height ] = size;
// ctx.clearRect(0, 0, width, height);
// ctx.fillStyle = 'rgba(0, 200, 0, 0.2)';
// data.features.map(feature => {
// const pixelCenter = mapService.lngLatToContainer(
// feature.geometry.coordinates
// );
// pixelCenter.x *= window.devicePixelRatio;
// pixelCenter.y *= window.devicePixelRatio;
// if (
// pixelCenter.x < 0 ||
// pixelCenter.y < 0 ||
// pixelCenter.x > width ||
// pixelCenter.y > height
// ) { return; }
// ctx.beginPath();
// ctx.arc(
// pixelCenter.x,
// pixelCenter.y,
// feature.properties.capacity / 200,
// 0,
// Math.PI * 2
// );
// ctx.fill();
// ctx.closePath();
// return '';
// });
// return '';
// }
// });
// scene.addLayer(layer);
// return '';
// });
return '';
});

View File

@ -4,6 +4,16 @@
"en": "Category"
},
"demos": [
{
"filename": "custom.js",
"title": "自定义水位图",
"screenshot":"https://gw.alipayobjects.com/mdn/rms_816329/afts/img/A*7FgbRppWc38AAAAAAAAAAAAAARQnAQ"
},
{
"filename": "custom2.js",
"title": "自定义气泡图",
"screenshot":"https://gw.alipayobjects.com/mdn/rms_816329/afts/img/A*SsiSTaYwCBgAAAAAAAAAAAAAARQnAQ"
},
{
"filename": "bar.js",
"title": "柱图",