question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Re-stroking paths creates unexpected fills

See original GitHub issue

Expected Behaviour

In this code:

const ctx = canvas.getContext('2d');
ctx.beginPath() // Only needed in pureimage :/

// First sub-path
ctx.lineWidth = 26;
ctx.strokeStyle = 'rgba(255,165,0,0.5)'
ctx.moveTo(20, 20);
ctx.lineTo(160, 20);
ctx.stroke();

// Second sub-path
ctx.lineWidth = 14;
ctx.strokeStyle = 'rgba(0,255,0,0.5)'
ctx.moveTo(20, 80);
ctx.lineTo(220, 80);
ctx.stroke();

// Third sub-path
ctx.lineWidth = 4;
ctx.strokeStyle = 'rgba(255,192,203,0.5)'
ctx.moveTo(20, 140);
ctx.lineTo(280, 140);
ctx.stroke();
  1. Lines should be parallel https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/stroke#re-stroking_paths image

Actual Behaviour

  1. lines are mostly parallel, with lots of diagonal fills

Screenshot from 2021-12-17 03-33-13

Steps To Reproduce

  1. Minimal: https://stackblitz.com/edit/koa-starter-yrc77r?file=index.js
  2. Workaround https://stackblitz.com/edit/koa-starter-zvulkw?file=index.js
  3. Where I had to workaround this bug: https://stackblitz.com/edit/feathersjs-7kqlyt?file=server.js

Any Relevant Code Snippets

Full koa server for testing:

import Koa from 'koa'
import { make, encodePNGToStream } from 'pureimage'
import { createWriteStream } from 'fs'
import logger from 'koa-logger'
import Router from '@koa/router'
import serve from 'koa-static'

const filepath = '/tmp/output.png'

function stroker(canvas) {
  const ctx = canvas.getContext('2d')
  ctx.beginPath()

  // First sub-path
  ctx.lineWidth = 26
  ctx.strokeStyle = 'rgba(255,165,0,0.5)'
  ctx.moveTo(20, 20)
  ctx.lineTo(160, 20)
  ctx.stroke()

  // Second sub-path
  ctx.lineWidth = 14
  ctx.strokeStyle = 'rgba(0,255,0,0.5)'
  ctx.moveTo(20, 80)
  ctx.lineTo(220, 80)
  ctx.stroke()

  // Third sub-path
  ctx.lineWidth = 4
  ctx.strokeStyle = 'rgba(255,192,203,0.5)'
  ctx.moveTo(20, 140)
  ctx.lineTo(280, 140)
  ctx.stroke()
}

const webCanvas = (ctx) => {
  ctx.body = `
    <h3>Expected Behavior:</h3>
    <canvas id="canvas"></canvas>
    <h3>Actual Behavior:</h3>
    <img src="output.png" />
    <script>
    ${stroker.toString()}
    stroker(document.getElementById('canvas'))
    </script>
  `
}

const app = new Koa()

// middlewares
app.use(logger())
const router = Router()
router.get('/', webCanvas)
app.use(serve('/tmp'))
app.use(router.routes())

// Run canvas code on server
try {
  const canvas = make(300, 150)
  stroker(canvas)

  await encodePNGToStream(canvas, createWriteStream(filepath))

  console.log('done writing to ', filepath)

  app.listen(3030)
} catch (err) {
  // When a request is aborted - err is an AbortError
  console.error(err)
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
joshmarinaccicommented, Jun 23, 2022

I just pushed back a fix. Can you see if the latest on MAIN fixes it? If so I’ll push out a release.

0reactions
joshmarinaccicommented, Jun 26, 2022

I’ve pushed the changes back and released a patch update on NPM. I’m going to mark this bug fixed. The remaining issues you see I think are due to color blending bugs (perhaps linear vs non-linear?).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix the following unexpected clipping path? - TeX
I want to shade as follows. enter image description here. However, my code below produces an unexpected result as follows. enter image ...
Read more >
Unexpected behaviour of JavaFX GraphicsContext.translate
When I stroke or fill a path after I have called the translate method ... Stroking text however, seems to paint the text...
Read more >
femtovg/README.md at master · femtovg/femtovg · GitHub
Bézier paths filling and stroking; Solid color and image pattern fills and strokes; Gradients - box, linear and radial; Stroke width and miterlimit...
Read more >
Why Are Open Paths an Issue With Stock Sites? (And How to ...
Open shapes that are unfilled are okay. That is, any path that has a stroke color but no fill color can remain open....
Read more >
cairo_t: Cairo: A Vector Graphics Library
To draw with cairo, you create a cairo_t, set the target surface, and drawing options ... As an example, here is how one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found