Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!