Skip to content

Commit

Permalink
fix: opacity with overflow hidden (#2450)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasvh authored Dec 29, 2020
1 parent 3982df1 commit 82b7da5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/render/canvas/canvas-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {contentBox} from '../box-sizing';
import {CanvasElementContainer} from '../../dom/replaced-elements/canvas-element-container';
import {SVGElementContainer} from '../../dom/replaced-elements/svg-element-container';
import {ReplacedElementContainer} from '../../dom/replaced-elements/index';
import {EffectTarget, IElementEffect, isClipEffect, isTransformEffect} from '../effects';
import {EffectTarget, IElementEffect, isClipEffect, isOpacityEffect, isTransformEffect} from '../effects';
import {contains} from '../../core/bitwise';
import {calculateGradientDirection, calculateRadius, processColorStops} from '../../css/types/functions/gradient';
import {FIFTY_PERCENT, getAbsoluteValue} from '../../css/types/length-percentage';
Expand Down Expand Up @@ -99,6 +99,10 @@ export class CanvasRenderer {

applyEffect(effect: IElementEffect) {
this.ctx.save();
if (isOpacityEffect(effect)) {
this.ctx.globalAlpha = effect.opacity;
}

if (isTransformEffect(effect)) {
this.ctx.translate(effect.offsetX, effect.offsetY);
this.ctx.transform(
Expand Down Expand Up @@ -128,7 +132,6 @@ export class CanvasRenderer {
async renderStack(stack: StackingContext) {
const styles = stack.element.container.styles;
if (styles.isVisible()) {
this.ctx.globalAlpha = styles.opacity;
await this.renderStackContent(stack);
}
}
Expand Down
23 changes: 16 additions & 7 deletions src/render/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {Path} from './path';

export const enum EffectType {
TRANSFORM = 0,
CLIP = 1
CLIP = 1,
OPACITY = 2
}

export const enum EffectTarget {
Expand All @@ -17,33 +18,41 @@ export interface IElementEffect {
}

export class TransformEffect implements IElementEffect {
readonly type: EffectType;
readonly target: number;
readonly type: EffectType = EffectType.TRANSFORM;
readonly target: number = EffectTarget.BACKGROUND_BORDERS | EffectTarget.CONTENT;
readonly offsetX: number;
readonly offsetY: number;
readonly matrix: Matrix;

constructor(offsetX: number, offsetY: number, matrix: Matrix) {
this.type = EffectType.TRANSFORM;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.matrix = matrix;
this.target = EffectTarget.BACKGROUND_BORDERS | EffectTarget.CONTENT;
}
}

export class ClipEffect implements IElementEffect {
readonly type: EffectType;
readonly type: EffectType = EffectType.CLIP;
readonly target: number;
readonly path: Path[];

constructor(path: Path[], target: EffectTarget) {
this.type = EffectType.CLIP;
this.target = target;
this.path = path;
}
}

export class OpacityEffect implements IElementEffect {
readonly type: EffectType = EffectType.OPACITY;
readonly target: number = EffectTarget.BACKGROUND_BORDERS | EffectTarget.CONTENT;
readonly opacity: number;

constructor(opacity: number) {
this.opacity = opacity;
}
}

export const isTransformEffect = (effect: IElementEffect): effect is TransformEffect =>
effect.type === EffectType.TRANSFORM;
export const isClipEffect = (effect: IElementEffect): effect is ClipEffect => effect.type === EffectType.CLIP;
export const isOpacityEffect = (effect: IElementEffect): effect is OpacityEffect => effect.type === EffectType.OPACITY;
6 changes: 5 additions & 1 deletion src/render/stacking-context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ElementContainer, FLAGS} from '../dom/element-container';
import {contains} from '../core/bitwise';
import {BoundCurves, calculateBorderBoxPath, calculatePaddingBoxPath} from './bound-curves';
import {ClipEffect, EffectTarget, IElementEffect, TransformEffect} from './effects';
import {ClipEffect, EffectTarget, IElementEffect, OpacityEffect, TransformEffect} from './effects';
import {OVERFLOW} from '../css/property-descriptors/overflow';
import {equalPath} from './path';
import {DISPLAY} from '../css/property-descriptors/display';
Expand Down Expand Up @@ -41,6 +41,10 @@ export class ElementPaint {
this.container = element;
this.effects = parentStack.slice(0);
this.curves = new BoundCurves(element);
if (element.styles.opacity < 1) {
this.effects.push(new OpacityEffect(element.styles.opacity));
}

if (element.styles.transform !== null) {
const offsetX = element.bounds.left + element.styles.transformOrigin[0].number;
const offsetY = element.bounds.top + element.styles.transformOrigin[1].number;
Expand Down
1 change: 1 addition & 0 deletions tests/reftests/overflow/overflow.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,6 @@ <h1>Overflow: hidden</h1>

</script>
</div>
<div class="hidden">Hidden<div style="opacity: 0.5">With opacity</div></div>
</body>
</html>

0 comments on commit 82b7da5

Please sign in to comment.