Skip to content

Commit

Permalink
Merge pull request #1714 from 6uliver/0.1
Browse files Browse the repository at this point in the history
Add linear gradient to ellipse
  • Loading branch information
liborm85 authored May 4, 2019
2 parents 1aab12a + 0f88949 commit e9fd05d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
8 changes: 7 additions & 1 deletion examples/vectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ var docDefinition = {
type: 'rect',
x: 10, y: 230, w: 100, h: 10,
linearGradient: ['red', 'yellow', 'green', 'blue']
}
},
{
type: 'ellipse',
x: 260, y: 140,
r1: 30, r2: 20,
linearGradient: ['red', 'green', 'blue', 'red'],
},
]
},
'This text should be rendered under canvas',
Expand Down
25 changes: 17 additions & 8 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,15 @@ function renderVector(vector, pdfKitDoc) {

//TODO: clipping

var gradient = null;

switch (vector.type) {
case 'ellipse':
pdfKitDoc.ellipse(vector.x, vector.y, vector.r1, vector.r2);

if (vector.linearGradient) {
gradient = pdfKitDoc.linearGradient(vector.x - vector.r1, vector.y, vector.x + vector.r1, vector.y);
}
break;
case 'rect':
if (vector.r) {
Expand All @@ -514,14 +520,7 @@ function renderVector(vector, pdfKitDoc) {
}

if (vector.linearGradient) {
var gradient = pdfKitDoc.linearGradient(vector.x, vector.y, vector.x + vector.w, vector.y);
var step = 1 / (vector.linearGradient.length - 1);

for (var i = 0; i < vector.linearGradient.length; i++) {
gradient.stop(i * step, vector.linearGradient[i]);
}

vector.color = gradient;
gradient = pdfKitDoc.linearGradient(vector.x, vector.y, vector.x + vector.w, vector.y);
}
break;
case 'line':
Expand Down Expand Up @@ -552,6 +551,16 @@ function renderVector(vector, pdfKitDoc) {
break;
}

if (vector.linearGradient && gradient) {
var step = 1 / (vector.linearGradient.length - 1);

for (var i = 0; i < vector.linearGradient.length; i++) {
gradient.stop(i * step, vector.linearGradient[i]);
}

vector.color = gradient;
}

if (vector.color && vector.lineColor) {
pdfKitDoc.fillColor(vector.color, vector.fillOpacity || 1);
pdfKitDoc.strokeColor(vector.lineColor, vector.strokeOpacity || 1);
Expand Down

0 comments on commit e9fd05d

Please sign in to comment.