Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
Apply a Gradient to an Icon in Open Layers

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?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda