Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT WIP BREAKING: remove opacity from colorstops in live Gradient class #9622

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- DRAFT WIP BREAKING: remove opacity from colorstops in live Gradient class [#9622](https://github.com/fabricjs/fabric.js/pull/9622)
- fix(ActiveSelection): block ancestors/descendants of selected objects from being selected [#9732](https://github.com/fabricjs/fabric.js/pull/9732)
- fix(Image): typo in style property for svg export [#9717](https://github.com/fabricjs/fabric.js/pull/9717)
- ci(): Update the changelog and stats action to work from forks
Expand Down
28 changes: 7 additions & 21 deletions src/gradient/Gradient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Color } from '../color/Color';
import { iMatrix } from '../constants';
import { parseTransformAttribute } from '../parser/parseTransformAttribute';
import type { FabricObject } from '../shapes/Object/FabricObject';
Expand Down Expand Up @@ -133,11 +132,9 @@ export class Gradient<
*/
addColorStop(colorStops: Record<string, string>) {
for (const position in colorStops) {
const color = new Color(colorStops[position]);
this.colorStops.push({
offset: parseFloat(position),
color: color.toRgb(),
opacity: color.getAlpha(),
color: colorStops[position],
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe those can be sort correctly every time we add new.
Unsorted colorstops are often cause of strange bugs.
Canvas doesn't care much but SVGs or PDFs do.

}
return this;
Expand All @@ -152,8 +149,8 @@ export class Gradient<
return {
...pick(this, propertiesToInclude as (keyof this)[]),
type: this.type,
coords: this.coords,
colorStops: this.colorStops,
coords: { ...this.coords },
colorStops: this.colorStops.map((colorStop) => ({ ...colorStop })),
offsetX: this.offsetX,
offsetY: this.offsetY,
gradientUnits: this.gradientUnits,
Expand Down Expand Up @@ -269,15 +266,9 @@ export class Gradient<
}
}

colorStops.forEach(({ color, offset, opacity }) => {
colorStops.forEach(({ color, offset }) => {
markup.push(
'<stop ',
'offset="',
offset * 100 + '%',
'" style="stop-color:',
color,
typeof opacity !== 'undefined' ? ';stop-opacity: ' + opacity : ';',
'"/>\n'
`<stop offset="${offset * 100}%" style="stop-color:${color};"/>\n`
);
});

Expand Down Expand Up @@ -309,13 +300,8 @@ export class Gradient<
coords.r2
);

this.colorStops.forEach(({ color, opacity, offset }) => {
gradient.addColorStop(
offset,
typeof opacity !== 'undefined'
? new Color(color).setAlpha(opacity).toRgba()
: color
);
this.colorStops.forEach(({ color, offset }) => {
gradient.addColorStop(offset, color);
});

return gradient;
Expand Down
22 changes: 12 additions & 10 deletions src/gradient/parser/parseColorStops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type { ColorStop } from '../typedefs';
const RE_KEY_VALUE_PAIRS = /\s*;\s*/;
const RE_KEY_VALUE = /\s*:\s*/;

function parseColorStop(el: SVGStopElement, multiplier: number) {
let colorValue, opacity;
function parseColorStop(el: SVGStopElement, opacityMultiplier: number) {
let colorValue, opacityValue;
const style = el.getAttribute('style');
if (style) {
const keyValuePairs = style.split(RE_KEY_VALUE_PAIRS);
Expand All @@ -23,22 +23,24 @@ function parseColorStop(el: SVGStopElement, multiplier: number) {
if (key === 'stop-color') {
colorValue = value;
} else if (key === 'stop-opacity') {
opacity = value;
opacityValue = value;
}
}
}

const color = new Color(
colorValue || el.getAttribute('stop-color') || 'rgb(0,0,0)'
colorValue = colorValue || el.getAttribute('stop-color') || 'rgb(0,0,0)';
opacityValue = ifNaN(
parseFloat(opacityValue ?? (el.getAttribute('stop-opacity') || '1')),
1
);

const color = new Color(colorValue);

color.setAlpha(color.getAlpha() * opacityValue * opacityMultiplier);

return {
offset: parsePercent(el.getAttribute('offset'), 0),
color: color.toRgb(),
opacity:
ifNaN(parseFloat(opacity || el.getAttribute('stop-opacity') || ''), 1) *
color.getAlpha() *
multiplier,
color: color.toRgba(),
};
}

Expand Down
1 change: 0 additions & 1 deletion src/gradient/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export type GradientCoordValue = number | Percent | string;
export type ColorStop = {
color: string;
offset: number;
opacity?: number;
};

export type LinearGradientCoords<T extends GradientCoordValue> = {
Expand Down
Loading