Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
41 changes: 30 additions & 11 deletions src/render/canvas/canvas-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
parsePathForBorderDoubleOuter,
parsePathForBorderStroke
} from '../border';
import {calculateBackgroundRendering, getBackgroundValueForIndex} from '../background';
import {calculateBackgroundRendering, calculateBackgroundRepeatPath, getBackgroundValueForIndex} from '../background';
import {isDimensionToken} from '../../css/syntax/parser';
import {TextBounds} from '../../css/layout/text';
import {ImageElementContainer} from '../../dom/replaced-elements/image-element-container';
Expand Down Expand Up @@ -633,36 +633,55 @@ export class CanvasRenderer extends Renderer {
null,
null
]);

const newBounds = new Bounds(0, 0, width, height);
const normalisedPath = calculateBackgroundRepeatPath(
getBackgroundValueForIndex(container.styles.backgroundRepeat, index),
[0, 0],
[width, height],
newBounds,
newBounds
);
const position = backgroundImage.position.length === 0 ? [FIFTY_PERCENT] : backgroundImage.position;
const x = getAbsoluteValue(position[0], width);
const y = getAbsoluteValue(position[position.length - 1], height);

const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;

const [rx, ry] = calculateRadius(backgroundImage, x, y, width, height);
if (rx > 0 && ry > 0) {
const radialGradient = this.ctx.createRadialGradient(left + x, top + y, 0, left + x, top + y, rx);
const radialGradient = ctx.createRadialGradient(0 + x, 0 + y, 0, 0 + x, 0 + y, rx);

processColorStops(backgroundImage.stops, rx * 2).forEach((colorStop) =>
radialGradient.addColorStop(colorStop.stop, asString(colorStop.color))
);

this.path(path);
this.ctx.fillStyle = radialGradient;
this.path.bind({ctx, formatPath: this.formatPath.bind({ctx})})(normalisedPath);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The fact that you need to use bind seem suspicious. Can't we use arrow functions instead?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'll try take a look, this I believe was a workaround because background (html2canvas) didn't allow arguments again long time ago I'll take a look

ctx.fillStyle = radialGradient;
if (rx !== ry) {
// transforms for elliptical radial gradient
const midX = container.bounds.left + 0.5 * container.bounds.width;
const midY = container.bounds.top + 0.5 * container.bounds.height;
const f = ry / rx;
const invF = 1 / f;

this.ctx.save();
this.ctx.translate(midX, midY);
this.ctx.transform(1, 0, 0, f, 0, 0);
this.ctx.translate(-midX, -midY);
ctx.save();
ctx.translate(midX, midY);
ctx.transform(1, 0, 0, f, 0, 0);
ctx.translate(-midX, -midY);

this.ctx.fillRect(left, invF * (top - midY) + midY, width, height * invF);
this.ctx.restore();
ctx.fillRect(0, invF * (0 - midY) + midY, width, height * invF);
ctx.restore();
} else {
this.ctx.fill();
ctx.fill();
}

if (width > 0 && height > 0) {
const pattern = this.ctx.createPattern(canvas, 'repeat') as CanvasPattern;
this.renderRepeat(path, pattern, left, top);
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/reftests/background/repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@
display:block;
}

div.psudo-after::before {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

psudo?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Typo likely, pr is pretty old now. I'll spend some time and clean up bits.

background-image: radial-gradient(circle, #6ca233 30%, rgba(255, 255, 255, 0) 0%);
background-position: center;
background-size: 10px 10px;
background-repeat: repeat-y;
height: calc(20% - 50px + 25px);
z-index: 1;
content: "";
position: absolute;
width: 5px;
left: 35px;
}



</style>

</head>
Expand All @@ -53,5 +68,8 @@
<div style="background:url(../../assets/image.jpg) no-repeat;"></div>
</div>

<div class="psudo-after">
</div>

</body>
</html>