且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将图像作为填充图案应用于多边形不会改变其样式

更新时间:2023-02-26 20:30:17

遵循@Mike的建议和

Following @Mike's suggestions and the continuation of this problem, the problem can be solved using a function like displayed bellow, which apply the desired pattern to a VectorLayer.

stylePatternSimplePoly(pattern:string, vectorLayer) {
    let ctx = document.createElement('canvas').getContext('2d');
    let vectorImage = new Image();  
    vectorImage.onload = () => {
        console.log("Function triggered!");
        let createdPattern = ctx.createPattern(vectorImage, 'repeat');
        vectorLayer.setStyle(
            new Style({
                fill: new Fill({
                    color: createdPattern
                })
            })
        );
    };      
    vectorImage.src = 'assets/images/patterns/' + pattern;  
}