SlideShare una empresa de Scribd logo
1 de 59
Descargar para leer sin conexión
RANDOMISING CSS
ANIMATIONS
ADAM BANKIN
Front End Engineer at Shutterstock
14 years experience with HTML, CSS and JS
AUSTRALIAN
(z === "zed"); // true
"ize".replace(/z/, "s");
(r === "ahhhhhh"); // true
$("#shrimp").on("barbie", function () {
console.error("racial stereotype");
console.error("and frankly, I'm offended");
return false;
});
RANDOMISING CSS ANIMATIONS
Why? Is this difficult?
Shutterstock labs home page
THE APPROACH
Respect the separation of concerns
Minimal interaction with the DOM
Keep it "jank"-free
DAFT JANK
Addy Osmani's post on jank busting
SEPARATION OF CONCERNS
Don't write HTML Elements with JavaScript
Use CSS to change appearance
Use JavaScript for the rest
THE APPROACH (AGAIN)
Respect the separation of concerns
Minimal interaction with the DOM
Keep it "jank"-free
Can we achieve randomised CSS animations with this
approach?
HTML
Keep it simple
PREDEFINE YOUR DOM ELEMENTS
<div id="bubble_source">
<div id="bubble_0" class="bubble"></div>
<div id="bubble_1" class="bubble"></div>
<div id="bubble_2" class="bubble"></div>
<div id="bubble_3" class="bubble"></div>
<div id="bubble_4" class="bubble"></div>
<div id="bubble_5" class="bubble"></div>
<div id="bubble_6" class="bubble"></div>
…
<div id="bubble_15" class="bubble"></div>
</div>
HTML… Done
CSS
The syntax of CSS Keyframe Animations
AT-RULE AND NAME
@keyframes myAnimationName {...}
… WITH PREFIXES
@-webkit-keyframes myAnimationName {...}
@-moz-keyframes myAnimationName {...}
@keyframes myAnimationName {...}
WITH KEYFRAMES
@keyframes myAnimationName {
from {
background-position: 0 0;
}
to {
background-position: -162px 0;
}
}
MDN has a list of animatable properties
CHOOSE THE RIGHT PROPERTIES TO ANIMATE
top/left | translate
Paul Irish's post on using translate instead of top/left
WITH PERCENTAGE-BASED KEYFRAMES
@keyframes myAnimationName {
0% {
background-position: 0 0;
}
100% {
background-position: -162px 0;
}
}
MIXED VALUES IN KEYFRAMES
@keyframes myAnimationName {
0% {
background-position: 0 0;
}
23% {
opacity: 0;
background-position: -162px 0;
}
100% {
opacity: 1;
}
}
TARGETING PREFIXED PROPERTIES
@-webkit-keyframes myAnimationName {
0% {-webkit-transform-origin: 0 0;}
100% {-webkit-transform-origin: 25px 0;}
}
@-moz-keyframes myAnimationName {
0% {-moz-transform-origin: 0 0;}
100% {-moz-transform-origin: 25px 0;}
}
@keyframes myAnimationName {
0% {transform-origin: 0 0;}
100% {transform-origin: 25px 0;}
}
ATTACHING A DEFINED KEYFRAME ANIMATION
.classWithAnimation {
animation: myAnimationName 1.2s linear 0s infinite;
}
... WITH PREFIXES
.classWithAnimation {
-webkit-animation: myAnimationName 1.2s linear 0s infinite;
-moz-animation: myAnimationName 1.2s linear 0s infinite;
animation: myAnimationName 1.2s linear 0s infinite;
}
SHORTHAND PROPERTIES EXPANDED
/* shorthand */
.bubble {
animation: myAnimationName 1.2s linear 0s infinite;
}
/* expanded */
.bubble {
animation-name: myAnimationName;
animation-duration: 1.2s;
animation-timing-function: linear;
animation-delay: 0s;
animation-iteration-count: infinite;
}
and that's just some of them
… at MDNthere are more
*AHEM*… PREFIXED
/* let's now call it 'bubble' */
.bubble {
-webkit-animation-name: myAnimationName;
-webkit-animation-duration: 1.2s;
-webkit-animation-timing-function: linear;
-webkit-animation-delay: 0s;
-webkit-animation-iteration-count: infinite;
}
ARE WE THERE YET?
CSS Keyframe Animations, they:
define a start
… the inbetween frames
… and the end?
THE END IS NOT THE END
"omit the to or 100% declaration from the
@keyframe…"
"then you call the animation on the progress
bar…"
"and just like that, the progress bar will
animate itself up to the value set by the inline
style."
Chris Coyer's post on animating to an inline style
"omit the to or 100% declaration from the
@keyframe…"
@keyframes myAnimationName {
0% {background-position: 0 0;}
}
"…then you call the animation on the progress
bar…"
.bubble {
animation: myAnimationName 1.2s linear 0s infinite;
}
"…and just like that, the progress bar will
animate itself up to the value set by the inline
style."
<div id="bubble_0" class="bubble" style="background-position: -162px 0;"></div>
DOES IT NEED TO BE INLINE?
What if the properties were set in a class ruleset?
/* css */
@keyframes myAnimationName {
0% {background-position: 0 0;}
}
.bubble {
animation: myAnimationName 1.2s linear 0s infinite;
}
.background-move {
background-position: -162px 0;
}
Then the class was dynamically applied to the DOM Element?
<!-- html -->
<div id="bubble_0" class="bubble background-move"></div>
RULESET AS INLINE STYLE
/* Keyframe Animation definitions */
@-webkit-keyframes lightBlue {
0% {
color: #2ed0e5;
-webkit-transform: translate(0, 50px) scale(.2, .2);
-webkit-transform-origin: 20px 0;
}
14% {
color: #2ed0e5;
-webkit-transform: translate(0, -100px) scale(.3, .3);
-webkit-transform-origin: 43px 0;
}
}
@-moz-keyframes lightBlue {
0% {
color: #2ed0e5;
-moz-transform: translate(0, 50px) scale(.2, .2);
-moz-transform-origin: 20px 0;
}
14% {
color: #2ed0e5;
-moz-transform: translate(0, -100px) scale(.3, .3);
-moz-transform-origin: 43px 0;
}
}
@keyframes lightBlue {
0% {
color: #2ed0e5;
JAVASCRIPT
Make it work
WHAT DOES JAVASCRIPT GIVE US?
requestAnimationFrame/cancelAnimationFrame methods
Event-type "animationend"
… WITH PREFIXES
requestAnimationFrame, msRequestAnimationFrame,
mozRequestAnimationFrame,
webkitRequestAnimationFrame, oRequestAnimationFrame
animationend, MSAnimationEnd, webkitAnimationEnd,
oAnimationEnd
CLASSY JAVASCRIPT
var ANIMATION_CLASSES = [
// colors
['color_dark_red', 'color_red', 'color_yellow', 'color_green', 'col
// translations
['translateA', 'translateB', 'translateC', 'translateD', 'translate
// origins
['originA', 'originB', 'originC', 'originD', 'originE', 'originF'],
// durations
['time_1500', 'time_1800', 'time_2000', 'time_2500', 'time_2700'],
// easings
['easing_1', 'easing_2', 'easing_3'],
// names
['light_blue', 'mid_blue']
];
GAME ON
When the page loads we:
1. Load custom classes "Bubble" and "BubbleController"
2. BubbleController puts all ".bubble" divs into an Array
3. It then makes two 'object pools'
4. One has "Bubble" instances, the other is ids from each div in
the Array
OBJECT POOLS ON HTML5ROCKS
Colt McAnlis's post on Object Pools in JS
GAME ON…
Then:
1. BubbleController runs a loop creating setTimeouts at
+200ms
2. The interval calls BubbleController's 'createBubble' method
3. This method pops a Bubble instance from its pool
4. It also pops an id from the id pool
5. It creates a listener for completion of the Bubble's 'init'
method
6. It creates a listener for completion of the Bubble's 'hide'
method
7. It runs the Bubble's 'init' method, with the id property
OBJECT POOL CODE IN BUBBLECONTROLLER
// `bubbles` is an Array of Dom Elements
// `bubbleClass` is Bubble
function createObjectPools (bubbles, bubbleClass) {
var j = bubbles.length;
var bubbleArgs = { delegateName: NAME };
while (j--) {
// populate the individual bubble's ids into an Array
idPool[j] = bubbles[j].id;
// use an Object Pool to create enough instances to control each bubble
bubblePool[j] = new bubbleClass(bubbleArgs);
}
}
'CREATEBUBBLE' METHOD IN
BUBBLECONTROLLER
function createBubble () {
// get new bubble instance id form the pools
var bubble = bubblePool.pop();
var id = idPool.pop();
// listen for the end of the new bubble's init phase
$.subscribe(NAME + EVENTS.INIT + STATUS.COMPLETE + id, bubbleInitCompleteHandle
// call init on the bubble instance
bubble.init({ id: id });
}
GAME ON… ALMOST THERE
Finally, the Bubble's 'init' method:
1. The Bubble instance gets the DOM Element via the id
2. Bubble creates a listener for the 'animationend' event
3. It then gets a random class from each sub-Array in
ANIMATION_CLASSES
4. It saves these as a String, but doesn't set its className
5. It fires off its 'init complete' Event
6. BubbleController reacts to this and calls
requestAnimationFrame.
7. requestAnimationFrame calls the Bubble instances 'show'
method
8. The 'show' method attaches the classlist, starting the
animation
'INIT' METHOD IN BUBBLE
Bubble.prototype.init = function (args) {
// add inited variables
this.id = args.id;
this.node = doc.getElementById(args.id);
this.$node = $(this.node);
this.classList = 'bubble on';
// add listeners
addListeners(this);
setClassList(this);
$.publish(this.delegateName + EVENTS.INIT + STATUS.COMPLETE + this.id, [this]);
};
RANDOMISING THROUGH THE
ANIMATION_CLASSES ARRAY
function setClassList (instance) {
var j = ANIMATION_CLASSES.length;
while (j--) {
instance.classList += (' ' + randomValue(ANIMATION_CLASSES[j]));
}
}
function randomValue (arr) {
var length = arr.length;
var idx = (Math.random() * length) | 0;
return arr[idx];
}
CALLING REQUESTANIMATIONFRAME IN
BUBBLECONTROLLER
function bubbleInitCompleteHandler (e, bubble) {
$.unsubscribe(e.type, bubbleInitCompleteHandler);
win.requestAnimationFrame(function () {
bubble.show();
});
}
'SHOW' METHOD IN BUBBLE
Bubble.prototype.show = function () {
// appending the classes makes the bubble appear and the animation run
this.node.className = this.classList;
};
GAME OFF
'animationend' event
1. The Bubble reacts to the DOM Element's 'animationend'
event
2. It changes the className of the DOM Element to "splat"
3. It runs its 'hide' method and fires off 'hide complete'
4. Inside the 'hide complete' Event is the id as a String
5. BubbleController reacts to this
6. It 'pools' the Bubble instance and 'pools' the id
7. It then runs its 'createBubble' method to start all over again
RE-'POOLING' THE BUBBLE AND ID IN
BUBBLECONTROLLER
function bubbleAnimationCompleteHandler (e, bubble, id) {
// add the completed bubble and its id back into the pools
bubblePool.unshift(bubble);
idPool.unshift(id);
createBubble();
}
SO!!!
Did it work?
Labs home page
TIME ESCAPES ME
The bubbles are done with :before and :after
They're webfonts - that's a story in itself
You can pause the bubbles
'animation-timing-function' easing is done with cubic-
beziers
THEY'RE CLICKABLE!
THANKS!
abankin@shutterstock.com
@adambankin
DID I MENTION WE'RE
HIRING?!

Más contenido relacionado

La actualidad más candente

CSS Position and it’s values
CSS Position and it’s valuesCSS Position and it’s values
CSS Position and it’s valuesGunjan Tulsiani
 
CSS - Text Properties
CSS - Text PropertiesCSS - Text Properties
CSS - Text Propertieshstryk
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Chris Poteet
 
CSS Flexbox (flexible box layout)
CSS Flexbox (flexible box layout)CSS Flexbox (flexible box layout)
CSS Flexbox (flexible box layout)Woodridge Software
 
CSS framework By Palash
CSS framework By PalashCSS framework By Palash
CSS framework By PalashPalashBajpai
 
9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classesIn a Rocket
 
Introduction to flexbox
Introduction  to  flexboxIntroduction  to  flexbox
Introduction to flexboxJyoti Gautam
 
Flexbox and Grid Layout
Flexbox and Grid LayoutFlexbox and Grid Layout
Flexbox and Grid LayoutRachel Andrew
 
Dynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation BasicsDynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation BasicsBeth Soderberg
 
Media queries A to Z
Media queries A to ZMedia queries A to Z
Media queries A to ZShameem Reza
 
Background property in css
Background property in cssBackground property in css
Background property in cssDhruvin Nakrani
 

La actualidad más candente (20)

The Power of CSS Flexbox
The Power of CSS FlexboxThe Power of CSS Flexbox
The Power of CSS Flexbox
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
 
CSS Position and it’s values
CSS Position and it’s valuesCSS Position and it’s values
CSS Position and it’s values
 
CSS - Text Properties
CSS - Text PropertiesCSS - Text Properties
CSS - Text Properties
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)
 
CSS
CSSCSS
CSS
 
CSS Flexbox (flexible box layout)
CSS Flexbox (flexible box layout)CSS Flexbox (flexible box layout)
CSS Flexbox (flexible box layout)
 
CSS framework By Palash
CSS framework By PalashCSS framework By Palash
CSS framework By Palash
 
9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes
 
Introduction to flexbox
Introduction  to  flexboxIntroduction  to  flexbox
Introduction to flexbox
 
Css position
Css positionCss position
Css position
 
Flexbox
FlexboxFlexbox
Flexbox
 
Flexbox and Grid Layout
Flexbox and Grid LayoutFlexbox and Grid Layout
Flexbox and Grid Layout
 
Css floats
Css floatsCss floats
Css floats
 
Dynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation BasicsDynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation Basics
 
Media queries A to Z
Media queries A to ZMedia queries A to Z
Media queries A to Z
 
Css pseudo-classes
Css pseudo-classesCss pseudo-classes
Css pseudo-classes
 
CSS Grid vs. Flexbox
CSS Grid vs. FlexboxCSS Grid vs. Flexbox
CSS Grid vs. Flexbox
 
Background property in css
Background property in cssBackground property in css
Background property in css
 
CSS Grid
CSS GridCSS Grid
CSS Grid
 

Similar a Randomising css animations

Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasminePaulo Ragonha
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6Solution4Future
 
Animate The Web With Ember.js - Jessica Jordan
Animate The Web With Ember.js - Jessica JordanAnimate The Web With Ember.js - Jessica Jordan
Animate The Web With Ember.js - Jessica JordanJessica Jordan
 
Effecient javascript
Effecient javascriptEffecient javascript
Effecient javascriptmpnkhan
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 CanvasMindy McAdams
 
Fórum de Software Livre do Serpro RJ 2009
Fórum de Software Livre do Serpro RJ 2009Fórum de Software Livre do Serpro RJ 2009
Fórum de Software Livre do Serpro RJ 2009Fabio Akita
 
Creating Beautiful CSS3 Animations - FITC Amsterdam 2016
Creating Beautiful CSS3 Animations - FITC Amsterdam 2016Creating Beautiful CSS3 Animations - FITC Amsterdam 2016
Creating Beautiful CSS3 Animations - FITC Amsterdam 2016Rami Sayar
 
Building mobile web apps with Mobello
Building mobile web apps with MobelloBuilding mobile web apps with Mobello
Building mobile web apps with MobelloJeong-Geun Kim
 
js+ts fullstack typescript with react and express.pdf
js+ts fullstack typescript with react and express.pdfjs+ts fullstack typescript with react and express.pdf
js+ts fullstack typescript with react and express.pdfNuttavutThongjor1
 
fullstack typescript with react and express.pdf
fullstack typescript with react and express.pdffullstack typescript with react and express.pdf
fullstack typescript with react and express.pdfNuttavutThongjor1
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescriptDavid Furber
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6Nitay Neeman
 

Similar a Randomising css animations (20)

Angular animate
Angular animateAngular animate
Angular animate
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
 
JavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talkJavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talk
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6
 
Animate The Web With Ember.js - Jessica Jordan
Animate The Web With Ember.js - Jessica JordanAnimate The Web With Ember.js - Jessica Jordan
Animate The Web With Ember.js - Jessica Jordan
 
Effecient javascript
Effecient javascriptEffecient javascript
Effecient javascript
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 Canvas
 
Fórum de Software Livre do Serpro RJ 2009
Fórum de Software Livre do Serpro RJ 2009Fórum de Software Livre do Serpro RJ 2009
Fórum de Software Livre do Serpro RJ 2009
 
ES6: Features + Rails
ES6: Features + RailsES6: Features + Rails
ES6: Features + Rails
 
Creating Beautiful CSS3 Animations - FITC Amsterdam 2016
Creating Beautiful CSS3 Animations - FITC Amsterdam 2016Creating Beautiful CSS3 Animations - FITC Amsterdam 2016
Creating Beautiful CSS3 Animations - FITC Amsterdam 2016
 
Js hacks
Js hacksJs hacks
Js hacks
 
DrupalCon jQuery
DrupalCon jQueryDrupalCon jQuery
DrupalCon jQuery
 
Building mobile web apps with Mobello
Building mobile web apps with MobelloBuilding mobile web apps with Mobello
Building mobile web apps with Mobello
 
ts+js
ts+jsts+js
ts+js
 
js+ts fullstack typescript with react and express.pdf
js+ts fullstack typescript with react and express.pdfjs+ts fullstack typescript with react and express.pdf
js+ts fullstack typescript with react and express.pdf
 
fullstack typescript with react and express.pdf
fullstack typescript with react and express.pdffullstack typescript with react and express.pdf
fullstack typescript with react and express.pdf
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6
 
ES6 - Level up your JavaScript Skills
ES6 - Level up your JavaScript SkillsES6 - Level up your JavaScript Skills
ES6 - Level up your JavaScript Skills
 

Último

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 

Último (20)

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 

Randomising css animations

  • 2. ADAM BANKIN Front End Engineer at Shutterstock 14 years experience with HTML, CSS and JS
  • 3. AUSTRALIAN (z === "zed"); // true "ize".replace(/z/, "s"); (r === "ahhhhhh"); // true $("#shrimp").on("barbie", function () { console.error("racial stereotype"); console.error("and frankly, I'm offended"); return false; });
  • 4. RANDOMISING CSS ANIMATIONS Why? Is this difficult?
  • 5.
  • 7. THE APPROACH Respect the separation of concerns Minimal interaction with the DOM Keep it "jank"-free
  • 8. DAFT JANK Addy Osmani's post on jank busting
  • 9. SEPARATION OF CONCERNS Don't write HTML Elements with JavaScript Use CSS to change appearance Use JavaScript for the rest
  • 10.
  • 11. THE APPROACH (AGAIN) Respect the separation of concerns Minimal interaction with the DOM Keep it "jank"-free Can we achieve randomised CSS animations with this approach?
  • 13. PREDEFINE YOUR DOM ELEMENTS <div id="bubble_source"> <div id="bubble_0" class="bubble"></div> <div id="bubble_1" class="bubble"></div> <div id="bubble_2" class="bubble"></div> <div id="bubble_3" class="bubble"></div> <div id="bubble_4" class="bubble"></div> <div id="bubble_5" class="bubble"></div> <div id="bubble_6" class="bubble"></div> … <div id="bubble_15" class="bubble"></div> </div> HTML… Done
  • 14. CSS The syntax of CSS Keyframe Animations
  • 15. AT-RULE AND NAME @keyframes myAnimationName {...}
  • 16. … WITH PREFIXES @-webkit-keyframes myAnimationName {...} @-moz-keyframes myAnimationName {...} @keyframes myAnimationName {...}
  • 17. WITH KEYFRAMES @keyframes myAnimationName { from { background-position: 0 0; } to { background-position: -162px 0; } } MDN has a list of animatable properties
  • 18. CHOOSE THE RIGHT PROPERTIES TO ANIMATE top/left | translate Paul Irish's post on using translate instead of top/left
  • 19. WITH PERCENTAGE-BASED KEYFRAMES @keyframes myAnimationName { 0% { background-position: 0 0; } 100% { background-position: -162px 0; } }
  • 20. MIXED VALUES IN KEYFRAMES @keyframes myAnimationName { 0% { background-position: 0 0; } 23% { opacity: 0; background-position: -162px 0; } 100% { opacity: 1; } }
  • 21. TARGETING PREFIXED PROPERTIES @-webkit-keyframes myAnimationName { 0% {-webkit-transform-origin: 0 0;} 100% {-webkit-transform-origin: 25px 0;} } @-moz-keyframes myAnimationName { 0% {-moz-transform-origin: 0 0;} 100% {-moz-transform-origin: 25px 0;} } @keyframes myAnimationName { 0% {transform-origin: 0 0;} 100% {transform-origin: 25px 0;} }
  • 22. ATTACHING A DEFINED KEYFRAME ANIMATION .classWithAnimation { animation: myAnimationName 1.2s linear 0s infinite; }
  • 23. ... WITH PREFIXES .classWithAnimation { -webkit-animation: myAnimationName 1.2s linear 0s infinite; -moz-animation: myAnimationName 1.2s linear 0s infinite; animation: myAnimationName 1.2s linear 0s infinite; }
  • 24. SHORTHAND PROPERTIES EXPANDED /* shorthand */ .bubble { animation: myAnimationName 1.2s linear 0s infinite; } /* expanded */ .bubble { animation-name: myAnimationName; animation-duration: 1.2s; animation-timing-function: linear; animation-delay: 0s; animation-iteration-count: infinite; } and that's just some of them … at MDNthere are more
  • 25. *AHEM*… PREFIXED /* let's now call it 'bubble' */ .bubble { -webkit-animation-name: myAnimationName; -webkit-animation-duration: 1.2s; -webkit-animation-timing-function: linear; -webkit-animation-delay: 0s; -webkit-animation-iteration-count: infinite; }
  • 26. ARE WE THERE YET? CSS Keyframe Animations, they: define a start … the inbetween frames … and the end?
  • 27.
  • 28. THE END IS NOT THE END "omit the to or 100% declaration from the @keyframe…" "then you call the animation on the progress bar…" "and just like that, the progress bar will animate itself up to the value set by the inline style." Chris Coyer's post on animating to an inline style
  • 29.
  • 30. "omit the to or 100% declaration from the @keyframe…" @keyframes myAnimationName { 0% {background-position: 0 0;} }
  • 31. "…then you call the animation on the progress bar…" .bubble { animation: myAnimationName 1.2s linear 0s infinite; }
  • 32. "…and just like that, the progress bar will animate itself up to the value set by the inline style." <div id="bubble_0" class="bubble" style="background-position: -162px 0;"></div>
  • 33.
  • 34. DOES IT NEED TO BE INLINE? What if the properties were set in a class ruleset? /* css */ @keyframes myAnimationName { 0% {background-position: 0 0;} } .bubble { animation: myAnimationName 1.2s linear 0s infinite; } .background-move { background-position: -162px 0; } Then the class was dynamically applied to the DOM Element? <!-- html --> <div id="bubble_0" class="bubble background-move"></div>
  • 35.
  • 36. RULESET AS INLINE STYLE /* Keyframe Animation definitions */ @-webkit-keyframes lightBlue { 0% { color: #2ed0e5; -webkit-transform: translate(0, 50px) scale(.2, .2); -webkit-transform-origin: 20px 0; } 14% { color: #2ed0e5; -webkit-transform: translate(0, -100px) scale(.3, .3); -webkit-transform-origin: 43px 0; } } @-moz-keyframes lightBlue { 0% { color: #2ed0e5; -moz-transform: translate(0, 50px) scale(.2, .2); -moz-transform-origin: 20px 0; } 14% { color: #2ed0e5; -moz-transform: translate(0, -100px) scale(.3, .3); -moz-transform-origin: 43px 0; } } @keyframes lightBlue { 0% { color: #2ed0e5;
  • 38. WHAT DOES JAVASCRIPT GIVE US? requestAnimationFrame/cancelAnimationFrame methods Event-type "animationend"
  • 39. … WITH PREFIXES requestAnimationFrame, msRequestAnimationFrame, mozRequestAnimationFrame, webkitRequestAnimationFrame, oRequestAnimationFrame animationend, MSAnimationEnd, webkitAnimationEnd, oAnimationEnd
  • 40. CLASSY JAVASCRIPT var ANIMATION_CLASSES = [ // colors ['color_dark_red', 'color_red', 'color_yellow', 'color_green', 'col // translations ['translateA', 'translateB', 'translateC', 'translateD', 'translate // origins ['originA', 'originB', 'originC', 'originD', 'originE', 'originF'], // durations ['time_1500', 'time_1800', 'time_2000', 'time_2500', 'time_2700'], // easings ['easing_1', 'easing_2', 'easing_3'], // names ['light_blue', 'mid_blue'] ];
  • 41. GAME ON When the page loads we: 1. Load custom classes "Bubble" and "BubbleController" 2. BubbleController puts all ".bubble" divs into an Array 3. It then makes two 'object pools' 4. One has "Bubble" instances, the other is ids from each div in the Array
  • 42. OBJECT POOLS ON HTML5ROCKS Colt McAnlis's post on Object Pools in JS
  • 43. GAME ON… Then: 1. BubbleController runs a loop creating setTimeouts at +200ms 2. The interval calls BubbleController's 'createBubble' method 3. This method pops a Bubble instance from its pool 4. It also pops an id from the id pool 5. It creates a listener for completion of the Bubble's 'init' method 6. It creates a listener for completion of the Bubble's 'hide' method 7. It runs the Bubble's 'init' method, with the id property
  • 44. OBJECT POOL CODE IN BUBBLECONTROLLER // `bubbles` is an Array of Dom Elements // `bubbleClass` is Bubble function createObjectPools (bubbles, bubbleClass) { var j = bubbles.length; var bubbleArgs = { delegateName: NAME }; while (j--) { // populate the individual bubble's ids into an Array idPool[j] = bubbles[j].id; // use an Object Pool to create enough instances to control each bubble bubblePool[j] = new bubbleClass(bubbleArgs); } }
  • 45. 'CREATEBUBBLE' METHOD IN BUBBLECONTROLLER function createBubble () { // get new bubble instance id form the pools var bubble = bubblePool.pop(); var id = idPool.pop(); // listen for the end of the new bubble's init phase $.subscribe(NAME + EVENTS.INIT + STATUS.COMPLETE + id, bubbleInitCompleteHandle // call init on the bubble instance bubble.init({ id: id }); }
  • 46.
  • 47. GAME ON… ALMOST THERE Finally, the Bubble's 'init' method: 1. The Bubble instance gets the DOM Element via the id 2. Bubble creates a listener for the 'animationend' event 3. It then gets a random class from each sub-Array in ANIMATION_CLASSES 4. It saves these as a String, but doesn't set its className 5. It fires off its 'init complete' Event 6. BubbleController reacts to this and calls requestAnimationFrame. 7. requestAnimationFrame calls the Bubble instances 'show' method 8. The 'show' method attaches the classlist, starting the animation
  • 48. 'INIT' METHOD IN BUBBLE Bubble.prototype.init = function (args) { // add inited variables this.id = args.id; this.node = doc.getElementById(args.id); this.$node = $(this.node); this.classList = 'bubble on'; // add listeners addListeners(this); setClassList(this); $.publish(this.delegateName + EVENTS.INIT + STATUS.COMPLETE + this.id, [this]); };
  • 49. RANDOMISING THROUGH THE ANIMATION_CLASSES ARRAY function setClassList (instance) { var j = ANIMATION_CLASSES.length; while (j--) { instance.classList += (' ' + randomValue(ANIMATION_CLASSES[j])); } } function randomValue (arr) { var length = arr.length; var idx = (Math.random() * length) | 0; return arr[idx]; }
  • 50. CALLING REQUESTANIMATIONFRAME IN BUBBLECONTROLLER function bubbleInitCompleteHandler (e, bubble) { $.unsubscribe(e.type, bubbleInitCompleteHandler); win.requestAnimationFrame(function () { bubble.show(); }); }
  • 51. 'SHOW' METHOD IN BUBBLE Bubble.prototype.show = function () { // appending the classes makes the bubble appear and the animation run this.node.className = this.classList; };
  • 52. GAME OFF 'animationend' event 1. The Bubble reacts to the DOM Element's 'animationend' event 2. It changes the className of the DOM Element to "splat" 3. It runs its 'hide' method and fires off 'hide complete' 4. Inside the 'hide complete' Event is the id as a String 5. BubbleController reacts to this 6. It 'pools' the Bubble instance and 'pools' the id 7. It then runs its 'createBubble' method to start all over again
  • 53. RE-'POOLING' THE BUBBLE AND ID IN BUBBLECONTROLLER function bubbleAnimationCompleteHandler (e, bubble, id) { // add the completed bubble and its id back into the pools bubblePool.unshift(bubble); idPool.unshift(id); createBubble(); }
  • 55. TIME ESCAPES ME The bubbles are done with :before and :after They're webfonts - that's a story in itself You can pause the bubbles 'animation-timing-function' easing is done with cubic- beziers THEY'RE CLICKABLE!
  • 56.
  • 57.
  • 58.