SlideShare a Scribd company logo
TrackPad Destroyer: Whoʼs Got the Fastest Fingers?"
by Rongchao Fan
We love games at PubNub. So when I was presented with the opportunity to make whatever I wanted
at the PubNub Hackathon, I knew I wanted to make a fun game with a single input. Inspired by the
“boring” Android game Toilet Paper Man, I wanted my game to be both multiplayer and dead simple.
As a result, TrackPad Destroyer was born. The game is simple. Two players scroll their trackpads as
fast as possible before the timer counts down to zero. Whoever scrolls more pixels wins.
That’s it. You’ll soon find out that your trackpad isn’t the only thing that’ll get destroyed:















You can check out the Github Repository here, and I’ll walk you through the code below.
// TIMER	
// CREDITS : http://houtz.tv/javascript/timer.html	
function start(){	
var seconds = 30;	
var millisec = 0;	
var timer;	
function display(){	
if (millisec <= 0){	
millisec=9;	
seconds-=1;	
publish(pos);	
}else	
millisec-=1;	
if (seconds<=0 && millisec<=0 ){	
end();	
}else	
timer = setTimeout(display, 100);	
$('#count-down').html(seconds+'.'+millisec);	
}	
function end(){	
clearTimeout(timer);	
whowin();	
}	
function whowin(){	
	
if (pos > other){	
$('#score-board').html("<div class=result>You Win ;)</div>")	
return;	
}	
if (pos < other){	
$('#score-board').html("<div class=result>You Lose ;(</div>")	
return;	
}	
else{	
$('#score-board').html("<div class=result>Tie, LOL</div>")	
return;	
}	
console.log('pos', pos, 'other', other);	
}	
display();	
}	
The Timer

I used setTimeout and clearTimeout to implement the
countdown timer. There are two variables to record
the time left. seconds represents seconds left and
millisec represents tenths of seconds left. The
frequency of the update is a tenth of a second, so I
set the 100 milliseconds interval to setTimeout. One
thing to mention is that the callback of setTimeout will
only execute once, which means that you need to
call setTimeout inside your callback to keep it working.
The callback here is display(). It updates time left and
pixels scrolled by both you and your opponent. When
time is due, clearTimeout is called to stop the timer and
pixels scrolled are compared to check who won.
The Track

Here is what I used to generate space to scroll. I took
the supplant function from the PubNub Javascript API
and used it to generate colorful divs. It replaced the
string inside of {} and {} itself with the value passed. In
a loop, I pushed the div string filled with different
background colors into an array called tiers and use
join(‘’’) to concatenate into a string and then render.
<div id=screen><div>	
// UTILS	
function supplant( str, values ) {	
return str.replace( /{([w-]+)}/g, function( _, match )
{	
return values[match] || _	
} );	
}	
	
// TIERS	
var tiers = []	
var tier ='<div class=tier style="background-color:
{color}"></div>'	
var colors = ["#ff4646", "#0863bf", "#3d4c6b", "#ec9319",	
"#7d9906", "#dd641f", "#8dc050", "#f18822",	
"#8fb037", "#785A3A", "#E7A800", "#1A8FC9",	
"#681D2A", "#af4009", "#0f8c98", "#f18822",	
"#005eac", "#f64000", "#3b5998"];	
	
for(var i=0; i<10000; i++){	
var color = colors[Math.floor(Math.random()
*colors.length)];	
var _tier = supplant(tier, {'color': color});	
tiers.push(_tier);	
}	
	
$(function(){	
$('#screen').html(tiers.join(''));	
start();	
});
We use window.scrollTop() to get the offset to the top of
the window, and use an anonymous function that
updates pixels scrolled as the scroll event handler.







So how do we make it multiplayer and keep both
screens synchronized? Use PubNub! Here’s how:

First, initialize pubnub with pub_key and sub_key.
On Your Mark, Get Set, Go!

Here is what I used to generate space to scroll. I took
the supplant function from the PubNub Javascript API
and used it to generate colorful divs. It replaced the
string inside of {} and {} itself with the value passed. In
a loop, I pushed the div string filled with different
background colors into an array called tiers and use
join(‘’’) to concatenate into a string and then render.
var other = 0;	
var pos=0;	
// GET SCROLL POSITION	
$window = $(window);	
$window.scroll(function(){	
pos = $window.scrollTop();	
$('#scrolled-me').html(pos);	
});	
var pubnub = PUBNUB.init({	
publish_key : 'demo',	
subscribe_key : 'demo'	
});
function publish(m){	
pubnub.publish({	
channel:'trackpad-destroyer-1',	
message: m	
});	
pubnub.subscribe({	
channel: 'trackpad-destroyer-2',	
message: update	
	
});	
// CALLBACK FOR SUBSCRIBE	
function update(m){	
other = m;	
$('#scrolled-other').html(m);	
}	
Next, use publish to publish messages to channel. Here, I publish
the pixels scrolled.






Lastly, subscribe to receive pixels scrolled by your competitors.










That’s it. Now we have a multiplayer game with dead simple input.
Have fun!

More Related Content

What's hot

The Canvas Tag
The Canvas TagThe Canvas Tag
The Canvas Tag
Dave Ross
 
Yahoo! JAPANとKotlin
Yahoo! JAPANとKotlinYahoo! JAPANとKotlin
Yahoo! JAPANとKotlin
Shoichi Matsuda
 
Open Social Data (Jaca), Alejandro Rivero
Open Social Data (Jaca), Alejandro RiveroOpen Social Data (Jaca), Alejandro Rivero
Open Social Data (Jaca), Alejandro Rivero
Aragón Open Data
 
Using the Command Line with Magento
Using the Command Line with MagentoUsing the Command Line with Magento
Using the Command Line with Magento
Matthew Haworth
 
Interactive subway map
Interactive subway mapInteractive subway map
Interactive subway map
Konstantin Chukhlomin
 
step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension Yuren Ju
 

What's hot (6)

The Canvas Tag
The Canvas TagThe Canvas Tag
The Canvas Tag
 
Yahoo! JAPANとKotlin
Yahoo! JAPANとKotlinYahoo! JAPANとKotlin
Yahoo! JAPANとKotlin
 
Open Social Data (Jaca), Alejandro Rivero
Open Social Data (Jaca), Alejandro RiveroOpen Social Data (Jaca), Alejandro Rivero
Open Social Data (Jaca), Alejandro Rivero
 
Using the Command Line with Magento
Using the Command Line with MagentoUsing the Command Line with Magento
Using the Command Line with Magento
 
Interactive subway map
Interactive subway mapInteractive subway map
Interactive subway map
 
step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension
 

Viewers also liked

English presentation
English presentation English presentation
English presentation thenamekelly
 
Lisbeth Coivu, Vuodenajat
Lisbeth Coivu, VuodenajatLisbeth Coivu, Vuodenajat
Lisbeth Coivu, Vuodenajat
Lisbeth Coivu
 
Instaforex presentation en
Instaforex presentation enInstaforex presentation en
Instaforex presentation en
Dosen Forex
 
Streaming Geo Coordinates from MongoDB to your iPhone App with PubNub using W...
Streaming Geo Coordinates from MongoDB to your iPhone App with PubNub using W...Streaming Geo Coordinates from MongoDB to your iPhone App with PubNub using W...
Streaming Geo Coordinates from MongoDB to your iPhone App with PubNub using W...PubNub
 
Real-Time Stats for Candy Box
Real-Time Stats for Candy Box  Real-Time Stats for Candy Box
Real-Time Stats for Candy Box
PubNub
 
Rails monolith-to-microservices-design
Rails monolith-to-microservices-designRails monolith-to-microservices-design
Rails monolith-to-microservices-design
Philippe Lafoucrière
 

Viewers also liked (8)

Fai
FaiFai
Fai
 
English presentation
English presentation English presentation
English presentation
 
Lisbeth Coivu, Vuodenajat
Lisbeth Coivu, VuodenajatLisbeth Coivu, Vuodenajat
Lisbeth Coivu, Vuodenajat
 
Lisbeth valojavesi
Lisbeth valojavesiLisbeth valojavesi
Lisbeth valojavesi
 
Instaforex presentation en
Instaforex presentation enInstaforex presentation en
Instaforex presentation en
 
Streaming Geo Coordinates from MongoDB to your iPhone App with PubNub using W...
Streaming Geo Coordinates from MongoDB to your iPhone App with PubNub using W...Streaming Geo Coordinates from MongoDB to your iPhone App with PubNub using W...
Streaming Geo Coordinates from MongoDB to your iPhone App with PubNub using W...
 
Real-Time Stats for Candy Box
Real-Time Stats for Candy Box  Real-Time Stats for Candy Box
Real-Time Stats for Candy Box
 
Rails monolith-to-microservices-design
Rails monolith-to-microservices-designRails monolith-to-microservices-design
Rails monolith-to-microservices-design
 

Similar to TrackPad Destroyer

[EN] Ada Lovelace Day 2014 - Tampon run
[EN] Ada Lovelace Day 2014  - Tampon run[EN] Ada Lovelace Day 2014  - Tampon run
[EN] Ada Lovelace Day 2014 - Tampon run
Maja Kraljič
 
Small pieces loosely joined
Small pieces loosely joinedSmall pieces loosely joined
Small pieces loosely joined
ennui2342
 
[SI] Ada Lovelace Day 2014 - Tampon Run
[SI] Ada Lovelace Day 2014  - Tampon Run[SI] Ada Lovelace Day 2014  - Tampon Run
[SI] Ada Lovelace Day 2014 - Tampon Run
Maja Kraljič
 
The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196
Mahmoud Samir Fayed
 
Java Guessing Game Number Tutorial
Java Guessing Game Number TutorialJava Guessing Game Number Tutorial
Java Guessing Game Number Tutorial
OXUS 20
 
Building Realtime Javascript Apps with PubNub
Building Realtime Javascript Apps with PubNubBuilding Realtime Javascript Apps with PubNub
Building Realtime Javascript Apps with PubNub
Tomomi Imura
 
Javascript: repetita iuvant
Javascript: repetita iuvantJavascript: repetita iuvant
Javascript: repetita iuvant
Luciano Mammino
 
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 58 of 210
The Ring programming language version 1.9 book - Part 58 of 210The Ring programming language version 1.9 book - Part 58 of 210
The Ring programming language version 1.9 book - Part 58 of 210
Mahmoud Samir Fayed
 
Creating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfCreating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdf
ShaiAlmog1
 
Asynchronous Programming at Netflix
Asynchronous Programming at NetflixAsynchronous Programming at Netflix
Asynchronous Programming at Netflix
C4Media
 
Доклад Антона Поварова "Go in Badoo" с Golang Meetup
Доклад Антона Поварова "Go in Badoo" с Golang MeetupДоклад Антона Поварова "Go in Badoo" с Golang Meetup
Доклад Антона Поварова "Go in Badoo" с Golang Meetup
Badoo Development
 
Es6 overview
Es6 overviewEs6 overview
Es6 overview
Bertrand Chevrier
 
Cc code cards
Cc code cardsCc code cards
Cc code cards
ysolanki78
 
How to make a video game
How to make a video gameHow to make a video game
How to make a video game
dandylion13
 
WP7 HUB_XNA
WP7 HUB_XNAWP7 HUB_XNA
WP7 HUB_XNA
MICTT Palma
 
A Complete Tutorial to Develop Real-Time Fighting Game with Cocos2dx
A Complete Tutorial to Develop Real-Time Fighting Game with Cocos2dxA Complete Tutorial to Develop Real-Time Fighting Game with Cocos2dx
A Complete Tutorial to Develop Real-Time Fighting Game with Cocos2dx
ShepHertz
 
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docxbbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
ikirkton
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
André Faria Gomes
 

Similar to TrackPad Destroyer (20)

[EN] Ada Lovelace Day 2014 - Tampon run
[EN] Ada Lovelace Day 2014  - Tampon run[EN] Ada Lovelace Day 2014  - Tampon run
[EN] Ada Lovelace Day 2014 - Tampon run
 
Small pieces loosely joined
Small pieces loosely joinedSmall pieces loosely joined
Small pieces loosely joined
 
[SI] Ada Lovelace Day 2014 - Tampon Run
[SI] Ada Lovelace Day 2014  - Tampon Run[SI] Ada Lovelace Day 2014  - Tampon Run
[SI] Ada Lovelace Day 2014 - Tampon Run
 
The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196
 
Java Guessing Game Number Tutorial
Java Guessing Game Number TutorialJava Guessing Game Number Tutorial
Java Guessing Game Number Tutorial
 
Building Realtime Javascript Apps with PubNub
Building Realtime Javascript Apps with PubNubBuilding Realtime Javascript Apps with PubNub
Building Realtime Javascript Apps with PubNub
 
Javascript: repetita iuvant
Javascript: repetita iuvantJavascript: repetita iuvant
Javascript: repetita iuvant
 
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84
 
The Ring programming language version 1.9 book - Part 58 of 210
The Ring programming language version 1.9 book - Part 58 of 210The Ring programming language version 1.9 book - Part 58 of 210
The Ring programming language version 1.9 book - Part 58 of 210
 
Creating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfCreating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdf
 
Asynchronous Programming at Netflix
Asynchronous Programming at NetflixAsynchronous Programming at Netflix
Asynchronous Programming at Netflix
 
Доклад Антона Поварова "Go in Badoo" с Golang Meetup
Доклад Антона Поварова "Go in Badoo" с Golang MeetupДоклад Антона Поварова "Go in Badoo" с Golang Meetup
Доклад Антона Поварова "Go in Badoo" с Golang Meetup
 
Es6 overview
Es6 overviewEs6 overview
Es6 overview
 
Cc code cards
Cc code cardsCc code cards
Cc code cards
 
How to make a video game
How to make a video gameHow to make a video game
How to make a video game
 
WP7 HUB_XNA
WP7 HUB_XNAWP7 HUB_XNA
WP7 HUB_XNA
 
A Complete Tutorial to Develop Real-Time Fighting Game with Cocos2dx
A Complete Tutorial to Develop Real-Time Fighting Game with Cocos2dxA Complete Tutorial to Develop Real-Time Fighting Game with Cocos2dx
A Complete Tutorial to Develop Real-Time Fighting Game with Cocos2dx
 
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docxbbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 
Est 8 2 nd
Est 8 2 ndEst 8 2 nd
Est 8 2 nd
 

Recently uploaded

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
Globus
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 

Recently uploaded (20)

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 

TrackPad Destroyer

  • 1. TrackPad Destroyer: Whoʼs Got the Fastest Fingers?" by Rongchao Fan
  • 2. We love games at PubNub. So when I was presented with the opportunity to make whatever I wanted at the PubNub Hackathon, I knew I wanted to make a fun game with a single input. Inspired by the “boring” Android game Toilet Paper Man, I wanted my game to be both multiplayer and dead simple. As a result, TrackPad Destroyer was born. The game is simple. Two players scroll their trackpads as fast as possible before the timer counts down to zero. Whoever scrolls more pixels wins.
  • 3. That’s it. You’ll soon find out that your trackpad isn’t the only thing that’ll get destroyed: You can check out the Github Repository here, and I’ll walk you through the code below.
  • 4. // TIMER // CREDITS : http://houtz.tv/javascript/timer.html function start(){ var seconds = 30; var millisec = 0; var timer; function display(){ if (millisec <= 0){ millisec=9; seconds-=1; publish(pos); }else millisec-=1; if (seconds<=0 && millisec<=0 ){ end(); }else timer = setTimeout(display, 100); $('#count-down').html(seconds+'.'+millisec); } function end(){ clearTimeout(timer); whowin(); } function whowin(){ if (pos > other){ $('#score-board').html("<div class=result>You Win ;)</div>") return; } if (pos < other){ $('#score-board').html("<div class=result>You Lose ;(</div>") return; } else{ $('#score-board').html("<div class=result>Tie, LOL</div>") return; } console.log('pos', pos, 'other', other); } display(); } The Timer I used setTimeout and clearTimeout to implement the countdown timer. There are two variables to record the time left. seconds represents seconds left and millisec represents tenths of seconds left. The frequency of the update is a tenth of a second, so I set the 100 milliseconds interval to setTimeout. One thing to mention is that the callback of setTimeout will only execute once, which means that you need to call setTimeout inside your callback to keep it working. The callback here is display(). It updates time left and pixels scrolled by both you and your opponent. When time is due, clearTimeout is called to stop the timer and pixels scrolled are compared to check who won.
  • 5. The Track Here is what I used to generate space to scroll. I took the supplant function from the PubNub Javascript API and used it to generate colorful divs. It replaced the string inside of {} and {} itself with the value passed. In a loop, I pushed the div string filled with different background colors into an array called tiers and use join(‘’’) to concatenate into a string and then render. <div id=screen><div> // UTILS function supplant( str, values ) { return str.replace( /{([w-]+)}/g, function( _, match ) { return values[match] || _ } ); } // TIERS var tiers = [] var tier ='<div class=tier style="background-color: {color}"></div>' var colors = ["#ff4646", "#0863bf", "#3d4c6b", "#ec9319", "#7d9906", "#dd641f", "#8dc050", "#f18822", "#8fb037", "#785A3A", "#E7A800", "#1A8FC9", "#681D2A", "#af4009", "#0f8c98", "#f18822", "#005eac", "#f64000", "#3b5998"]; for(var i=0; i<10000; i++){ var color = colors[Math.floor(Math.random() *colors.length)]; var _tier = supplant(tier, {'color': color}); tiers.push(_tier); } $(function(){ $('#screen').html(tiers.join('')); start(); });
  • 6. We use window.scrollTop() to get the offset to the top of the window, and use an anonymous function that updates pixels scrolled as the scroll event handler. So how do we make it multiplayer and keep both screens synchronized? Use PubNub! Here’s how: First, initialize pubnub with pub_key and sub_key. On Your Mark, Get Set, Go! Here is what I used to generate space to scroll. I took the supplant function from the PubNub Javascript API and used it to generate colorful divs. It replaced the string inside of {} and {} itself with the value passed. In a loop, I pushed the div string filled with different background colors into an array called tiers and use join(‘’’) to concatenate into a string and then render. var other = 0; var pos=0; // GET SCROLL POSITION $window = $(window); $window.scroll(function(){ pos = $window.scrollTop(); $('#scrolled-me').html(pos); }); var pubnub = PUBNUB.init({ publish_key : 'demo', subscribe_key : 'demo' });
  • 7. function publish(m){ pubnub.publish({ channel:'trackpad-destroyer-1', message: m }); pubnub.subscribe({ channel: 'trackpad-destroyer-2', message: update }); // CALLBACK FOR SUBSCRIBE function update(m){ other = m; $('#scrolled-other').html(m); } Next, use publish to publish messages to channel. Here, I publish the pixels scrolled. Lastly, subscribe to receive pixels scrolled by your competitors. That’s it. Now we have a multiplayer game with dead simple input. Have fun!