mapping the world with twitter

55
Mapping the world with Twitter Behind the <canvas> of a HTML5 application May 11th, 2011 Carlo Zapponi

Upload: carlo-zapponi

Post on 12-May-2015

2.671 views

Category:

Technology


4 download

DESCRIPTION

These are the slides of the speech about "A world of tweets" internals I gave at the jsDay in Verona on 11th May 2011.

TRANSCRIPT

Page 1: Mapping the world with Twitter

Mapping the world with TwitterBehind the <canvas> of a HTML5 application

May 11th, 2011Carlo Zapponi

Page 2: Mapping the world with Twitter

2

who am I?@littleark

Page 3: Mapping the world with Twitter

travelling

Page 4: Mapping the world with Twitter

mixing cultures

Page 5: Mapping the world with Twitter

creativity (and football)

Page 6: Mapping the world with Twitter
Page 7: Mapping the world with Twitter

a global innovation firm500+ people, 30+ nationalities, 40+ years of global experience

New York, NYMilan, Italy

Austin, TX

Seattle, WA

Shanghai, China

Munich, Germany

San Francisco, CA

Amsterdam, Netherlands

Page 8: Mapping the world with Twitter

8

a world of tweets

Page 9: Mapping the world with Twitter

it was a rainy afternoon......when I had the idea of remapping the world through Twitter.

Page 10: Mapping the world with Twitter

I jumped into fast prototyping...and I coded a map of falling tweets on a canvas based on the Twitter Stream API

Page 11: Mapping the world with Twitter

back to o!ce they caught me!and they were excited! The personal sunday project turned into an internal frog project.

Page 12: Mapping the world with Twitter

HTML5

Geo LocationsTwitter Stream API

<canvas>

Yahoo! PlacemakerFlash*

Page 13: Mapping the world with Twitter

A World of Tweetshttp://www.aworldoftweets.com

Innovative real-time 2/3D info-visualization of activity in the twitter-sphere

Page 14: Mapping the world with Twitter

a world of tweetsa real-time data infographics of the twitter sphere

Page 15: Mapping the world with Twitter

one minute videoa walkthrough video of A world of Tweets

Page 16: Mapping the world with Twitter

multi layers structure

AWOT is based on a stack of transparent background canvases

Page 17: Mapping the world with Twitter

system map

geo-located based architecture built on top of the Twitter Stream API and Yahoo! Placemaker

Page 18: Mapping the world with Twitter

some facts from AWOT

Event related hot spots

60+ millions tweets

USA, Brazil and Indonesia top

229 countries

Page 19: Mapping the world with Twitter

AWOT generated more than 30,000 tweets in one week. The web site had more than 75,000 unique visitors during the launch week.

AWOT has been featured on Time Magazine, Tech Crunch, Harvard Business Review, PSFK, Chrome Experiments, etc.

worldwide success

Page 20: Mapping the world with Twitter

20

other versions

Page 21: Mapping the world with Twitter

SXSW 2011

http://aworldoftweets.com/sxsw

Page 22: Mapping the world with Twitter

IE9 LAUNCH

http://aworldoftweets.com/ie9

Page 23: Mapping the world with Twitter

Salone del Mobile

http://aworldoftweets.com/salone

Page 24: Mapping the world with Twitter

Crowd Tracking Tools

AWOT can be used as a crowd tracking system for urban areas (coming soon).

Page 25: Mapping the world with Twitter

25

<canvas>the good parts

Page 26: Mapping the world with Twitter

Do you know about canvas?

Page 27: Mapping the world with Twitter

Have you written a piece of code with canvas?

Page 28: Mapping the world with Twitter

Have you deployed a web application with canvas?

Page 29: Mapping the world with Twitter

the <canvas> element

provides scripts with a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, or other visual images on the fly.

Page 30: Mapping the world with Twitter

canvas is part of HTML5canvas allows drawingcanvas has a simple API

Page 31: Mapping the world with Twitter

globalAlphaglobalCompositeOperationstrokeStylefillStylelineWidthlineCaplineJoinmiterLimitshadowO"setXshadowO"setYshadowBlurshadowColorfonttextAligntextBaselinesaverestorescalerotatetranslatetransformsetTransformcreateLinearGradientcreateRadialGradientcreatePatternclearRectfillRectstrokeRectbeginPathclosePathmoveTolineToquadraticCurveTobezierCurveToarcTorectarcfillstrokeclipisPointInPathdrawFocusRingfillTextstrokeTextdrawImagecreateImageDatagetImageDataputImageDataaddColorStopCanvasGradient.addColorStopTextMetrics.widthImageData.widthImageData.heightImageData.dataCanvasPixelArray.length

canvas API is simple to learn21 attributes and 45 methods

Page 32: Mapping the world with Twitter

draw shapesdraw textsolid and gradient paintdraw and tweak bitmapsplay with videosapply transformations

Page 33: Mapping the world with Twitter

canvas runs in many browsers

canvas has no memorycanvas is not 3D yet

Page 34: Mapping the world with Twitter

Safari 3.0+

IE 9Firefox 3.0+

Android 2.1+

Chrome 3.0+

iOS Safari 3.2+Opera 10.0+

Page 35: Mapping the world with Twitter

35

highlighting interesting features

AWOT insights

Page 36: Mapping the world with Twitter

clear and redrawthe traditional animation loop through the use of simple shapes

function loop(){

ctx.clearRect(0,0,canvas.width,canvas.height);

draw(); //drawing functions

}setInterval(loop,interval);

Page 37: Mapping the world with Twitter

transparencycanvas provides both a global alpha attribute and color based alpha

var canvas=document.getElementById(“canvas”), ctx=canvas.getContext(“2d”);ctx.globalAlpha=0.5;ctx.fillStyle=“rgba(255,255,0,0.5)”;ctx.fillRect(50,50,100,100);

Page 38: Mapping the world with Twitter

canvas state with save() and restore()a canvas keeps a stack of states of its main attributes, transformations and clipping

var ctx=document.getElementById(“canvas”).getContext(“2d”);ctx.fillStyle=“#ffff00”;ctx.fillRect(0,0,100,100);ctx.save();ctx.fillStyle=“#339933”;ctx.globalAlpha=0.5;ctx.fillRect(50,50,100,100);ctx.restore();ctx.fillRect(100,100,100,100);

Page 39: Mapping the world with Twitter

using imagesthe canvas element lets you draw images on its surface

var image=new Image();

image.onload=function(){

drawImage(image,x,y); //placing

drawImage(image,x,y,width,height); //scaling

drawImage(image,sx,sy,sWidth,sHeight,dx,dy,dWidth,dHeight); //slicing

}

img.src=”image.png”;

Page 40: Mapping the world with Twitter

pixel data manipulationthe canvas element lets you play directly with pixels at the byte level

var imgData=context.getImageData(left,top,width,height);var pixelArray=imgData.data; //CanvasPixelArray

manipulatePixels(pixelArray);

context.putImageData(imgData,x,y);

Page 41: Mapping the world with Twitter

globalCompositeOperationcanvas defines 12 composition operations

source-insource-over

destination-over destination-in

source-out

destination-out

source-atop

destination-atop

lighter

darker

xor

copy

Page 42: Mapping the world with Twitter

42

lessons learnt through the journey

tricks & tips

Page 43: Mapping the world with Twitter

always check if canvas is supported.browser detection is not enough.

var canvas=document.createElement(“canvas”);if(canvas && canvas.getContext(“2d”)) {//supported

} else {//not supported

}

Page 44: Mapping the world with Twitter

size matters.canvas performances are tightly bound to its size

faster

slower

Page 45: Mapping the world with Twitter

refresh only what mattersdon’t clear the whole canvas if you can. clear only the area that changed last.

function loop(){var bounds=detectBoundingRect();clearRect(bounds);draw();

}

Page 46: Mapping the world with Twitter

clearRect vs copycomposite operation copy it’s faster with transformations and clipping

ctx.clearRect(0,0,width,height); //slower

ctx.save();ctx.globalCompositeOperation=”copy”;ctx.fillStyle=”#ffffff”; //background colorctx.fillRect(0,0,width,height); //fasterctx.restore();

Page 47: Mapping the world with Twitter

you can’t beat the screen60Hz refresh rate means a frame each 16.7ms

Align animations to the refresh speed (16.7ms=60fps - 33.4ms=30fps)

function loop(){draw();

}

setInterval(loop,Math.ceil(1000/60));

Page 48: Mapping the world with Twitter

don’t rely on a given intervalalways calculate the real time between two frames to smooth the animations.

var thisFrame = new Date().getTime();var dT = (thisFrame - this.lastFrame)/1000;this.lastFrame = thisFrame;

sprite.x+= dX * dT; //smooth movement

Page 49: Mapping the world with Twitter

video memory is slowdecrease the rate of requests to video memory. save a big chunk of data and use it.

var img=ctx.getImageData(0,0,width,height), data=img.data, length=data.length, i=0,a,r,g,b;

while(i<length){r=data[i-3]; g=data[i-2]; b=data[i-1];a=data[i]; //alphai+=4;

}

Page 50: Mapping the world with Twitter

CanvasPixelArray is slowalways cache the data array in a proper javascript object

var img=ctx.getImageData(0,0,width,height), data=img.data, length=data.length, i=0,a,r,g,b;

while(i<length){r=data[i-3]; g=data[i-2]; b=data[i-1];a=data[i]; //alphai+=4;

}

Page 51: Mapping the world with Twitter

if makes sense then double bu"erdecrease the rate of requests to video memory. save a big chunk of data and use it.

var backBuffer=document.createElement(“canvas”);backBuffer.width=context.canvas.width;backBuffer.height=context.canvas.height;var bctx=backBuffer.getContext(“2d”);

drawOnBackBuffer();

context.drawImage(backBuffer,0,0);

Page 52: Mapping the world with Twitter

comparison: FPS and CPU %http://jsperf.com/vsummit-canvas-perf/5

canvas size

shapes drawing functions

double bu!er with drawImage

pixel manipulation

100px x 100px

500px x 500px

1000px x 1000px

59fps10%

52fps20%

50fps40%

59fps10%

56fps38%

44fps50%

55fps15%

24fps70%

9fps92%

Page 53: Mapping the world with Twitter

hardware accelerationHTML5 supports GPU accelerated operations.

To be use only if you experience a true performance win. It might not be better.

.gpu {-webkit-transform: rotate3d(0,0,1, 0deg);-moz-transform: rotate3d(0,0,1, 0deg);transform: rotate3d(0,0,1, 0deg);

}

Page 54: Mapping the world with Twitter

don’t trust me. experiment alwaysthese tips are generally useful but di!erent situations may need di!erent solutions.

Now go and try yourself finding new ways to improve your stu!.