Skip to content

Set gradient fill for each feature separately #15739

Answered by ahocevar
TheWeirdDev asked this question in Q&A
Discussion options

You must be logged in to vote

I don't know what exactly you are trying to achieve. But this example shows how to create a gradient for the extent of each feature: https://codesandbox.io/p/sandbox/canvas-gradient-pattern-forked-g9ntwf?file=%2Fmain.js%3A53%2C32:

// Generate a rainbow gradient
function gradient(feature, resolution) {
  const canvas = document.createElement("canvas");
  const context = canvas.getContext("2d");
  var extent = feature.getGeometry().getExtent();
  // Gradient starts on the left edge of each feature, and ends on the right.
  // Coordinate origin is the top-left corner of the extent of the geometry, so
  // we just divide the geometry's extent width by resolution and multiply with
  // pixelRa…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@TheWeirdDev
Comment options

Comment options

You must be logged in to vote
2 replies
@TheWeirdDev
Comment options

@ahocevar
Comment options

Answer selected by TheWeirdDev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants