# How do I avoid "blending" or "bleeding" of edges of solidly-colored shapes?

**URL:** <https://discourse.processing.org/t/how-do-i-avoid-blending-or-bleeding-of-edges-of-solidly-colored-shapes/39569>\
**Category:** Coding Questions\
**Created:** [November 3, 2022, 11:47am UTC](https://discourse.processing.org/t/how-do-i-avoid-blending-or-bleeding-of-edges-of-solidly-colored-shapes/39569 "2022-11-03T11:47:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sleepingsun](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@sleepingsun](https://discourse.processing.org/u/sleepingsun)\
**Post date:** [November 3, 2022, 11:47am UTC](https://discourse.processing.org/t/how-do-i-avoid-blending-or-bleeding-of-edges-of-solidly-colored-shapes/39569/1 "2022-11-03T11:47:35Z")

</div>

Hi all

Whenever I draw a solid shape of one color, wherever it has an edge meeting up with a different color, it will have a different color along the edge. Not the color of the shape, not the color that is outside the shape, but a mix, maybe?

Here’s a demo for what I’m talking about.

> **[p5.js Web Editor](https://editor.p5js.org/sleepingsun/sketches/LlJkJ2YSiL)**
>
> A web editor for p5.js, a JavaScript library with the goal of making coding accessible to artists, designers, educators, and beginners.

I thought initially that the problem was only for non-vertical and non-horizontal edges, but it seems that _all_ edges are affected.

Initially, you’ll note the diagonal lines in adjoining triangles. These are the most obvious “undesired” lines, and can be made better (but not fixed entirely) by adding a stroke value. I think the issue here is that there is a tiny gap between the triangles that shows the background. This is odd enough in and of itself, since the triangles share coordinates.

However, even along the horizontal and vertical lines, we have odd blending going on. Here is a zoomed screenshot to show what I mean:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/0/a08e37030cdf368bb2149e9de1f12e39a331c709.png)  
(I would show more, but I’m a new user and not allowed ☹ )

The above image is from when I zoom in the browser. If I save the image to png (using built-in browser capabilities), the horizontal and vertical bleeds seem to disappear, but the diagonal remain.

Does anyone know details of why these artifacts appear, and if they’re avoidable? I also tried using noSmooth(), and it made no difference.

---

<div class="post-metadata">

**Author:** ![sableraph](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sableraph/32/251_2.png) [@sableraph](https://discourse.processing.org/u/sableraph)\
**Post date:** [November 3, 2022, 2:31pm UTC](https://discourse.processing.org/t/how-do-i-avoid-blending-or-bleeding-of-edges-of-solidly-colored-shapes/39569/2 "2022-11-03T14:31:51Z")

</div>

Hi @sleepingsun. I believe this is a matter of resolution. You can get around it by making the canvas larger or calling `pixelDensity(2)` or both.

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [November 3, 2022, 2:32pm UTC](https://discourse.processing.org/t/how-do-i-avoid-blending-or-bleeding-of-edges-of-solidly-colored-shapes/39569/3 "2022-11-03T14:32:21Z")

</div>

Hello @sleepingsun !  
Are you loading an image? Or are the shapes code based?  
Can you copy and paste your code so we can see? I’m sure it’s an easy fix.  
🤓

---

<div class="post-metadata">

**Author:** ![sleepingsun](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@sleepingsun](https://discourse.processing.org/u/sleepingsun)\
**Post date:** [November 3, 2022, 6:24pm UTC](https://discourse.processing.org/t/how-do-i-avoid-blending-or-bleeding-of-edges-of-solidly-colored-shapes/39569/4 "2022-11-03T18:24:36Z")

</div>

Thanks @sableraph , I’ll experiment with pixelDensity! Are there rules of thumb as to performance issues when increasing pixelDensity? Or maybe concerns as to overruling pixelDensities of user devices?

---

<div class="post-metadata">

**Author:** ![sleepingsun](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@sleepingsun](https://discourse.processing.org/u/sleepingsun)\
**Post date:** [November 3, 2022, 6:26pm UTC](https://discourse.processing.org/t/how-do-i-avoid-blending-or-bleeding-of-edges-of-solidly-colored-shapes/39569/5 "2022-11-03T18:26:49Z")

</div>

Hello @debxyz , thank you for chiming in!

I linked to a sketch with my code above, but here’s the code again. The shapes are made with calls to, in this sketch, triangle - but it could also be rect, square, circle or even vertice-based shapes (as it is in another sketch I’m working on).

```auto

const get_random_int = (rng_num, min, max) => {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(rng_num * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
const get_random_from_arr = (rng_num, arr) => arr[get_random_int(rng_num,0,arr.length)];

function setup() {
  createCanvas(400, 400);
  colorMode(HSB,360,100,100,100);

}

function draw() {
    randomSeed(34369253);
    noLoop();
    noSmooth();
    const palette = [[241,54,86],[236,83,82],[224,76,26],[204,43,9],[178,96,77]];
    const background_color = [100,100,100];

    background(...background_color);
    const offset = round( width / 10 );
    const num_cells = 5;
    const cell_side = round((width - 2 * offset) / num_cells);
    for(let i = 0; i < num_cells; i++) {
        for(let u = 0; u < num_cells; u++) {
            const left = offset + i * cell_side;
            const right = left + cell_side;
            const top = offset + u * cell_side;
            const bottom = top + cell_side;
            const color1 = get_random_from_arr(random(), palette);
            const color2 = get_random_from_arr(random(), palette);
            noStroke();
            //stroke(...color1);
            fill(color1);
            triangle(left, top, right, top, right, bottom);
            //stroke(...color1);
            fill(color1);
            triangle(left, top, right, bottom, left, bottom);
        }
    }

}

```

---

<div class="post-metadata">

**Author:** ![sableraph](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sableraph/32/251_2.png) [@sableraph](https://discourse.processing.org/u/sableraph)\
**Post date:** [November 4, 2022, 9:32am UTC](https://discourse.processing.org/t/how-do-i-avoid-blending-or-bleeding-of-edges-of-solidly-colored-shapes/39569/6 "2022-11-04T09:32:33Z")

</div>

You are correct to assume that performance will be impacted by calling `pixelDensity()`. Like with screen resolution the number of pixel that needs to be rendered grows exponentially with higher pixel density.

If your canvas can be resized (like to be viewed full screen for example) you will need to render even more pixels.

If you are performing any per-pixel operations (via shaders or `loadPixels()` for example) these will also massively impact performance when increasing pixel density.

You could check the pixel density of the display with [`displayDensity`](https://p5js.org/reference/#/p5/displayDensity) and use that to set the appropriate density for a user’s display.

Since user devices may vary wildly in performance, there is no way to pick settings that will work for sure on all possible devices. One hacky thing you could do is have your sketch test its own framerate and lower the pixel density if it is running too slow.

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [November 4, 2022, 12:17pm UTC](https://discourse.processing.org/t/how-do-i-avoid-blending-or-bleeding-of-edges-of-solidly-colored-shapes/39569/7 "2022-11-04T12:17:22Z")

</div>

My guess is that it has to do with the inner workings of Javascript’s HTML5 Canvas API, rather than P5 itself. If you observe the [source code](https://github.com/processing/p5.js/blob/e03e4d3fa8e96bb5384f0d661d84f2e835f279a6/src/core/p5.Renderer2D.js), it seems like P5 uses this API for its rendering:

```auto
// starting at line 740
p5.Renderer2D.prototype.triangle = function(args) {

  // ... part left out for clarity

  ctx.beginPath();
  ctx.moveTo(x1, y1);
  ctx.lineTo(x2, y2);
  ctx.lineTo(x3, y3);
  ctx.closePath();
  if (doFill) {
    ctx.fill();
  }
  if (doStroke) {
    ctx.stroke();
  }
};

```

When zooming in on the result at Mozilla’s documentation on the [Canvas API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) (which is a `Canvas` element), I see a similar ‘anti-alias’ effect at the edges of the green rectangle. So I think you have to redirect your search in the [Javascript/HTML5 direction](https://stackoverflow.com/questions/195262/can-i-turn-off-antialiasing-on-an-html-canvas-element) to find a solution to your issue.

---

<div class="post-metadata">

**Author:** ![sleepingsun](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@sleepingsun](https://discourse.processing.org/u/sleepingsun)\
**Post date:** [November 7, 2022, 9:13pm UTC](https://discourse.processing.org/t/how-do-i-avoid-blending-or-bleeding-of-edges-of-solidly-colored-shapes/39569/8 "2022-11-07T21:13:45Z")

</div>

Thank you for your detailed reply, @sableraph 🤗 These are things I’ve considered myself, but I haven’t had a very firm grip on them. Your reply is inspiring me to research and implement a more solid strategy, more thoroughly. My hope is to have a small handful of techniques that I can apply easily; I find these kind of optimizations somewhat tedious in face of the more interesting work of the visual art that they enable 😅 So, thank you for the information and inspiration!

---

<div class="post-metadata">

**Author:** ![sleepingsun](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@sleepingsun](https://discourse.processing.org/u/sleepingsun)\
**Post date:** [November 7, 2022, 9:17pm UTC](https://discourse.processing.org/t/how-do-i-avoid-blending-or-bleeding-of-edges-of-solidly-colored-shapes/39569/9 "2022-11-07T21:17:47Z")

</div>

Thank you @Tiemen ! This is very interesting! This is, in a way, perhaps the answer I’m looking for; it’s an implementation detail, perhaps unavoidable. The essence is, it might be avoidable somehow, but at least now I know that I’m not doing anything silly that I shouldn’t be. Now, to research further 🤗
