Pdfkit: Opacity

Created on 1 Aug 2014  路  3Comments  路  Source: foliojs/pdfkit

The .opacity() function does now work with any shapes currently. Does anyone have a code snippet that they have gotten to work with opacity or is this a problem with the code.

Most helpful comment

Internally, when fill is called with a color it calls fillColor first, which assumes an opacity of 1.0 if none is given. If you write it like this, it should work:

doc.save()
   .rect(0, 306, 612, 90)
   .fillColor('red', 0.2)
   .fill()

fillColor takes a color and an opacity. If you want to set both the fill and stroke opacity, set the fillColor first, then call opacity and finally fill.

All 3 comments

Do you have a code example that isn't working? I'm not sure I understand the problem.

doc.save()
.rect(0, 306, 612, 90)
.opacity(.2)
.fill("red");

Opacity never works. I debugged the lib and in the src code .fill somehow overrides opacity to 1 everytime.

Internally, when fill is called with a color it calls fillColor first, which assumes an opacity of 1.0 if none is given. If you write it like this, it should work:

doc.save()
   .rect(0, 306, 612, 90)
   .fillColor('red', 0.2)
   .fill()

fillColor takes a color and an opacity. If you want to set both the fill and stroke opacity, set the fillColor first, then call opacity and finally fill.

Was this page helpful?
0 / 5 - 0 ratings