fix: finish animation/transitions for elements (#2632)

This commit is contained in:
Niklas von Hertzen 2021-08-13 18:15:55 +08:00 committed by GitHub
parent f919204efa
commit 969638fb94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 123 additions and 37 deletions

View File

@ -1,8 +1,8 @@
import {CSSValue} from './syntax/parser'; import {CSSValue} from './syntax/parser';
import {CSSTypes} from './types/index'; import {CSSTypes} from './types';
import {Context} from '../core/context'; import {Context} from '../core/context';
export enum PropertyDescriptorParsingType { export const enum PropertyDescriptorParsingType {
VALUE, VALUE,
LIST, LIST,
IDENT_VALUE, IDENT_VALUE,

View File

@ -57,6 +57,7 @@ import {Tokenizer} from './syntax/tokenizer';
import {Color, color as colorType, isTransparent} from './types/color'; import {Color, color as colorType, isTransparent} from './types/color';
import {angle} from './types/angle'; import {angle} from './types/angle';
import {image} from './types/image'; import {image} from './types/image';
import {time} from './types/time';
import {opacity} from './property-descriptors/opacity'; import {opacity} from './property-descriptors/opacity';
import {textDecorationColor} from './property-descriptors/text-decoration-color'; import {textDecorationColor} from './property-descriptors/text-decoration-color';
import {textDecorationLine} from './property-descriptors/text-decoration-line'; import {textDecorationLine} from './property-descriptors/text-decoration-line';
@ -71,6 +72,7 @@ import {contains} from '../core/bitwise';
import {content} from './property-descriptors/content'; import {content} from './property-descriptors/content';
import {counterIncrement} from './property-descriptors/counter-increment'; import {counterIncrement} from './property-descriptors/counter-increment';
import {counterReset} from './property-descriptors/counter-reset'; import {counterReset} from './property-descriptors/counter-reset';
import {duration} from './property-descriptors/duration';
import {quotes} from './property-descriptors/quotes'; import {quotes} from './property-descriptors/quotes';
import {boxShadow} from './property-descriptors/box-shadow'; import {boxShadow} from './property-descriptors/box-shadow';
import {paintOrder} from './property-descriptors/paint-order'; import {paintOrder} from './property-descriptors/paint-order';
@ -79,6 +81,7 @@ import {webkitTextStrokeWidth} from './property-descriptors/webkit-text-stroke-w
import {Context} from '../core/context'; import {Context} from '../core/context';
export class CSSParsedDeclaration { export class CSSParsedDeclaration {
animationDuration: ReturnType<typeof time.parse>;
backgroundClip: ReturnType<typeof backgroundClip.parse>; backgroundClip: ReturnType<typeof backgroundClip.parse>;
backgroundColor: Color; backgroundColor: Color;
backgroundImage: ReturnType<typeof backgroundImage.parse>; backgroundImage: ReturnType<typeof backgroundImage.parse>;
@ -138,6 +141,7 @@ export class CSSParsedDeclaration {
textTransform: ReturnType<typeof textTransform.parse>; textTransform: ReturnType<typeof textTransform.parse>;
transform: ReturnType<typeof transform.parse>; transform: ReturnType<typeof transform.parse>;
transformOrigin: ReturnType<typeof transformOrigin.parse>; transformOrigin: ReturnType<typeof transformOrigin.parse>;
transitionDuration: ReturnType<typeof time.parse>;
visibility: ReturnType<typeof visibility.parse>; visibility: ReturnType<typeof visibility.parse>;
webkitTextStrokeColor: Color; webkitTextStrokeColor: Color;
webkitTextStrokeWidth: ReturnType<typeof webkitTextStrokeWidth.parse>; webkitTextStrokeWidth: ReturnType<typeof webkitTextStrokeWidth.parse>;
@ -145,6 +149,7 @@ export class CSSParsedDeclaration {
zIndex: ReturnType<typeof zIndex.parse>; zIndex: ReturnType<typeof zIndex.parse>;
constructor(context: Context, declaration: CSSStyleDeclaration) { constructor(context: Context, declaration: CSSStyleDeclaration) {
this.animationDuration = parse(context, duration, declaration.animationDuration);
this.backgroundClip = parse(context, backgroundClip, declaration.backgroundClip); this.backgroundClip = parse(context, backgroundClip, declaration.backgroundClip);
this.backgroundColor = parse(context, backgroundColor, declaration.backgroundColor); this.backgroundColor = parse(context, backgroundColor, declaration.backgroundColor);
this.backgroundImage = parse(context, backgroundImage, declaration.backgroundImage); this.backgroundImage = parse(context, backgroundImage, declaration.backgroundImage);
@ -213,6 +218,7 @@ export class CSSParsedDeclaration {
this.textTransform = parse(context, textTransform, declaration.textTransform); this.textTransform = parse(context, textTransform, declaration.textTransform);
this.transform = parse(context, transform, declaration.transform); this.transform = parse(context, transform, declaration.transform);
this.transformOrigin = parse(context, transformOrigin, declaration.transformOrigin); this.transformOrigin = parse(context, transformOrigin, declaration.transformOrigin);
this.transitionDuration = parse(context, duration, declaration.transitionDuration);
this.visibility = parse(context, visibility, declaration.visibility); this.visibility = parse(context, visibility, declaration.visibility);
this.webkitTextStrokeColor = parse(context, webkitTextStrokeColor, declaration.webkitTextStrokeColor); this.webkitTextStrokeColor = parse(context, webkitTextStrokeColor, declaration.webkitTextStrokeColor);
this.webkitTextStrokeWidth = parse(context, webkitTextStrokeWidth, declaration.webkitTextStrokeWidth); this.webkitTextStrokeWidth = parse(context, webkitTextStrokeWidth, declaration.webkitTextStrokeWidth);
@ -306,6 +312,8 @@ const parse = (context: Context, descriptor: CSSPropertyDescriptor<any>, style?:
case 'length-percentage': case 'length-percentage':
const value = parser.parseComponentValue(); const value = parser.parseComponentValue();
return isLengthPercentage(value) ? value : ZERO_LENGTH; return isLengthPercentage(value) ? value : ZERO_LENGTH;
case 'time':
return time.parse(context, parser.parseComponentValue());
} }
break; break;
} }

View File

@ -0,0 +1,9 @@
import {IPropertyTypeValueDescriptor, PropertyDescriptorParsingType} from '../IPropertyDescriptor';
export const duration: IPropertyTypeValueDescriptor = {
name: 'duration',
initialValue: '0s',
prefix: false,
type: PropertyDescriptorParsingType.TYPE_VALUE,
format: 'time'
};

View File

@ -1,6 +1,6 @@
import {IPropertyIdentValueDescriptor, PropertyDescriptorParsingType} from '../IPropertyDescriptor'; import {IPropertyIdentValueDescriptor, PropertyDescriptorParsingType} from '../IPropertyDescriptor';
import {Context} from '../../core/context'; import {Context} from '../../core/context';
export enum OVERFLOW_WRAP { export const enum OVERFLOW_WRAP {
NORMAL = 'normal', NORMAL = 'normal',
BREAK_WORD = 'break-word' BREAK_WORD = 'break-word'
} }

View File

@ -2,7 +2,7 @@
import {fromCodePoint, toCodePoints} from 'css-line-break'; import {fromCodePoint, toCodePoints} from 'css-line-break';
export enum TokenType { export const enum TokenType {
STRING_TOKEN, STRING_TOKEN,
BAD_STRING_TOKEN, BAD_STRING_TOKEN,
LEFT_PARENTHESIS_TOKEN, LEFT_PARENTHESIS_TOKEN,

View File

@ -1 +1 @@
export type CSSTypes = 'angle' | 'color' | 'image' | 'length' | 'length-percentage'; export type CSSTypes = 'angle' | 'color' | 'image' | 'length' | 'length-percentage' | 'time';

20
src/css/types/time.ts Normal file
View File

@ -0,0 +1,20 @@
import {CSSValue} from '../syntax/parser';
import {TokenType} from '../syntax/tokenizer';
import {ITypeDescriptor} from '../ITypeDescriptor';
import {Context} from '../../core/context';
export const time: ITypeDescriptor<number> = {
name: 'time',
parse: (_context: Context, value: CSSValue): number => {
if (value.type === TokenType.DIMENSION_TOKEN) {
switch (value.unit.toLowerCase()) {
case 's':
return 1000 * value.number;
case 'ms':
return value.number;
}
}
throw new Error(`Unsupported time type`);
}
};

View File

@ -21,10 +21,21 @@ export class ElementContainer {
this.styles = new CSSParsedDeclaration(context, window.getComputedStyle(element, null)); this.styles = new CSSParsedDeclaration(context, window.getComputedStyle(element, null));
this.textNodes = []; this.textNodes = [];
this.elements = []; this.elements = [];
if (this.styles.transform !== null && isHTMLElementNode(element)) {
// getBoundingClientRect takes transforms into account if (isHTMLElementNode(element)) {
element.style.transform = 'none'; if (this.styles.animationDuration > 0) {
element.style.animationDuration = '0s';
}
if (this.styles.transitionDuration > 0) {
element.style.transitionDuration = '0s';
}
if (this.styles.transform !== null) {
// getBoundingClientRect takes transforms into account
element.style.transform = 'none';
}
} }
this.bounds = parseBounds(this.context, element); this.bounds = parseBounds(this.context, element);
this.flags = 0; this.flags = 0;
} }

View File

@ -5,46 +5,84 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../test.js"></script> <script type="text/javascript" src="../test.js"></script>
<style> <style>
span {
color:blue;
}
p {
background-color: green;
}
div {
background: red;
border: 5px solid blue;
animation: spin 3s linear 1s infinite;
}
body { body {
font-family: Arial; font-family: Arial;
} }
@-webkit-keyframes spin { @keyframes rotate0 {
0% { 0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg); transform: rotate(0deg);
/* Firefox 16+, IE 10+, Opera */ } }
100% { }
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
/* Firefox 16+, IE 10+, Opera */ } }
@keyframes spin { @keyframes rotate45 {
0% { 0% {
-webkit-transform: rotate(0deg); transform: rotate(45deg);
transform: rotate(0deg); }
/* Firefox 16+, IE 10+, Opera */ } }
100% {
-webkit-transform: rotate(360deg); p {
transform: rotate(360deg); font: 22px/1 Arial, sans-serif;
/* Firefox 16+, IE 10+, Opera */ } } position: absolute;
top: 25%;
left: 25%;
width: 50%;
height: 50%;
color: #fff;
background-color: #666;
line-height: 90px;
text-align: center;
}
.transformed.working p {
transform: rotate(45deg);
}
.animated.working p {
animation-name: rotate0;
animation-duration: 1ms;
animation-play-state: paused;
}
.animated.broken p {
animation-name: rotate45;
animation-duration: 1ms;
animation-play-state: paused;
}
.transitioned p {
transition: 1ms;
transform: rotate(45deg)
}
div {
float: left;
clear: left;
margin-right: 10px;
background-color: #ccc;
width: 180px;
height: 180px;
position: relative;
}
</style> </style>
</head> </head>
<body> <body>
<div style="clip: rect(0px, 400px, 50px, 200px); ">Some inline text <span> followed by text in span </span> followed by more inline text. <div class="transformed working">
<p>Then a block level element.</p> <p>Hello</p>
Then more inline text.</div> </div>
<div class="animated working">
<p>Hello</p>
</div>
<div class="animated broken">
<p>Hello</p>
</div>
<div class="transitioned broken">
<p>Hello</p>
</div>
</body> </body>
</html> </html>