Skip to content

Commit

Permalink
feat: conic gradient (#888)
Browse files Browse the repository at this point in the history
* feat: conic gradient

* feat: unit test for conic gradient

* refactor: removed conic gradient from svg

* chore: empty row in between test-suites
  • Loading branch information
Moranski25 authored Jan 15, 2025
1 parent 78a5fd2 commit 3078e41
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import NodeContainer from '../node-container';
* @typedef {object} GradientNode
* @property {string} type
* @property {object[]} stops
* @property {string} [stops[].type=linearGradient] - radialGradient|linearGradient
* @property {string} [stops[].type=linearGradient] - radialGradient|linearGradient|conicGradient
* @property {string} stops[].color - {@link https://www.w3.org/TR/SVG/types.html#DataTypeColor}
* @property {string} [stops[].opacity=1] - {@link https://www.w3.org/TR/css-color-4/#propdef-opacity}
* @property {number} stops[].offset - {@link https://www.w3.org/TR/SVG/pservers.html#StopElementOffsetAttribute}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ reg.add('container', container);
reg.add('defs', container);
reg.add('linearGradient', gradientItem);
reg.add('radialGradient', gradientItem);
reg.add('conicGradient', gradientItem);
reg.add('stop', gradientItem);
reg.add('pattern', patternItem);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ describe('canvas-gradient', () => {
});
});

describe('conic', () => {
it('should create a conic gradient node', () => {
shape = {
fill: {
type: 'gradient',
orientation: 'conic',
startAngle: Math.PI / 2,
x: 0,
y: 0,
stops: [
{ offset: 0, color: 'red', opacity: 0 },
{ offset: 1, color: 'green' },
],
},
};
const fill = createCanvasGradient(canvascontext(), shape, shape.fill);
expect(fill).to.be.a('function');
expect(fill()).to.be.equal('dummyGradient-conic');
});
});

describe('linear', () => {
it('should create linear gradients properly', () => {
shape = dummyRectObject('linear');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { degreesToPoints } from '../../../core/math/angles';
*/
export default function createCanvasGradient(g, node, gradient) {
const { orientation, degree, stops = [] } = gradient;

let newGradient = null;

if (orientation === 'radial') {
Expand All @@ -24,6 +23,11 @@ export default function createCanvasGradient(g, node, gradient) {
bounds.y + bounds.height / 2,
Math.max(bounds.width, bounds.height) / 2
);
} else if (orientation === 'conic') {
const startAngle = gradient.startAngle || 0;
const centerOffsetX = gradient.x || 0;
const centerOffsetY = gradient.y || 0;
newGradient = g.createConicGradient(startAngle, centerOffsetX, centerOffsetY);
} else {
const points = degreesToPoints(degree);
['x1', 'x2', 'y1', 'y2'].forEach((c) => {
Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/mocks/canvas-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function canvascontext(contextType = '2d') {
if (contextType === '2d') {
item.createRadialGradient = gradientFactory('radial');
item.createLinearGradient = gradientFactory('linear');
item.createConicGradient = gradientFactory('conic');
}

return item;
Expand Down

0 comments on commit 3078e41

Please sign in to comment.