Apply a Gradient to an Icon in Open Layers

508 views Asked by At

On my project, I originally used CircleStyle to create points on a map and then used fill: new Fill({ with CanvasGradient to style the point with two colours.

I now want to use a custom icon (for example 'icon.png') instead of just a coloured dot for these points.

I have tried using image: new Icon for this which works for displaying the icon but I cannot apply a CanvasGradient to this.

I can apply a single colour to the icon with color which overlays the icon with that colour with transparency, ideally I would want this with 2 colours; half the icon being one colour and half the icon being the other colour.

I have uploaded an image showing how my points currently looked below. circleStyle point with 2 colours applied using ColourGradient

The documentation suggests that CanvasGradient cannot be applied to icons so my question is: How can I apply two colours/a gradient to an icon in OpenLayers?

2

There are 2 answers

0
Mike On BEST ANSWER

The OpenLayers Icon style can take a canvas element as well as an icon url. So you could load the icon url, draw it to a canvas, apply the gradient using a multiply operation, and finally use a destination-in operation to restore transparency:

const img = document.createElement('img');
img.onload = function () {
  const width = img.width;
  const height = img.height;
  const canvas = document.createElement('canvas');
  canvas.width = width;
  canvas.height = height;
  const context = canvas.getContext('2d');
  const gradient = context.createLinearGradient(0, 0, width, 0);
  gradient.addColorStop(0, 'red');
  gradient.addColorStop(0.5, 'red');
  gradient.addColorStop(0.5, 'green');
  gradient.addColorStop(1, 'green');
  context.drawImage(img, 0, 0);
  context.globalCompositeOperation = 'multiply';
  context.fillStyle = gradient;
  context.fillRect(0, 0, width, height);
  context.globalCompositeOperation = 'destination-in';
  context.drawImage(img, 0, 0);

  feature.setStyle(
    new Style({
      image: new Icon({
        img: canvas,
        imgSize: [width, height],
      })
    })
  );
};
img.src = icon_url;

Working example https://codesandbox.io/s/icon-color-forked-4b1e1?file=/main.js

1
kossyak On

enter image description here

I didn't find a suitable solution for myself, so I created my own function for generating diagrams. It is very simple and easily customizable. For your convenience, I have packaged my solution as an open-source project. https://github.com/kossyak/olStyleChart