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

fix the inconsisencies of HTML element with both box-shadow and border-radius #2367

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
19 changes: 11 additions & 8 deletions src/render/canvas/canvas-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {ElementContainer} from '../../dom/element-container';
import {BORDER_STYLE} from '../../css/property-descriptors/border-style';
import {CSSParsedDeclaration} from '../../css/index';
import {TextContainer} from '../../dom/text-container';
import {Path, transformPath} from '../path';
import {Path, transformPath, reversePath} from '../path';
import {BACKGROUND_CLIP} from '../../css/property-descriptors/background-clip';
import {BoundCurves, calculateBorderBoxPath, calculateContentBoxPath, calculatePaddingBoxPath} from '../bound-curves';
import {isBezierCurve} from '../bezier-curve';
Expand Down Expand Up @@ -482,12 +482,16 @@ export class CanvasRenderer {

mask(paths: Path[]) {
this.ctx.beginPath();
this.ctx.save();
// reset tranform to identity
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
this.ctx.moveTo(0, 0);
this.ctx.lineTo(this.canvas.width, 0);
this.ctx.lineTo(this.canvas.width, this.canvas.height);
this.ctx.lineTo(0, this.canvas.height);
this.ctx.lineTo(0, 0);
this.formatPath(paths.slice(0).reverse());
this.ctx.restore();
this.formatPath(reversePath(paths));
this.ctx.closePath();
}

Expand Down Expand Up @@ -663,18 +667,17 @@ export class CanvasRenderer {
await this.renderBackgroundImage(paint.container);

this.ctx.restore();

const borderBoxArea = calculateBorderBoxPath(paint.curves);
styles.boxShadow
.slice(0)
.reverse()
.forEach(shadow => {
this.ctx.save();
const borderBoxArea = calculateBorderBoxPath(paint.curves);
const maskOffset = shadow.inset ? 0 : MASK_OFFSET;
const shadowPaintingArea = transformPath(
borderBoxArea,
-maskOffset + (shadow.inset ? 1 : -1) * shadow.spread.number,
(shadow.inset ? 1 : -1) * shadow.spread.number,
shadow.offsetX.number - maskOffset + (shadow.inset ? 1 : -1) * shadow.spread.number,
shadow.offsetY.number + (shadow.inset ? 1 : -1) * shadow.spread.number,
shadow.spread.number * (shadow.inset ? -2 : 2),
shadow.spread.number * (shadow.inset ? -2 : 2)
);
Expand All @@ -689,8 +692,8 @@ export class CanvasRenderer {
this.path(shadowPaintingArea);
}

this.ctx.shadowOffsetX = shadow.offsetX.number + maskOffset;
this.ctx.shadowOffsetY = shadow.offsetY.number;
this.ctx.shadowOffsetX = maskOffset;
this.ctx.shadowOffsetY = 0;
this.ctx.shadowColor = asString(shadow.color);
this.ctx.shadowBlur = shadow.blur.number;
this.ctx.fillStyle = shadow.inset ? asString(shadow.color) : 'rgba(0,0,0,1)';
Expand Down
9 changes: 9 additions & 0 deletions src/render/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum PathType {
export interface IPath {
type: PathType;
add(deltaX: number, deltaY: number): IPath;
reverse(): IPath;
}

export const equalPath = (a: Path[], b: Path[]): boolean => {
Expand All @@ -34,4 +35,12 @@ export const transformPath = (path: Path[], deltaX: number, deltaY: number, delt
});
};

export const reversePath = (path: Path[]): Path[] => {
return path
.slice(0)
.reverse()
.map(point => {
return point.reverse();
});
};
export type Path = Vector | BezierCurve;
3 changes: 3 additions & 0 deletions src/render/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class Vector implements IPath {
add(deltaX: number, deltaY: number): Vector {
return new Vector(this.x + deltaX, this.y + deltaY);
}
reverse(): Vector {
return this;
}
}

export const isVector = (path: Path): path is Vector => path.type === PathType.VECTOR;
2 changes: 1 addition & 1 deletion tests/reftests/background/box-shadow.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
margin: 10px;
display: inline-block;
min-height: 50px;
// border-radius: 10px;
border-radius: 10px;
}

body {
Expand Down