7. a global innovation firm
500+ people, 30+ nationalities, 40+ years of global experience
Amsterdam, Netherlands
Munich, Germany
Seattle, WA
Milan, Italy
New York, NY
San Francisco, CA
Austin, TX
Shanghai, China
19. worldwide success
AWOT generated more than 30,000 AWOT has been featured on Time
tweets in one week. The web site had Magazine, Tech Crunch, Harvard
more than 75,000 unique visitors during Business Review, PSFK, Chrome
the launch week. Experiments, etc.
29. 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.
30. canvas is part of HTML5
canvas allows drawing
canvas has a simple API
37. 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);
transparency
canvas provides both a global alpha attribute and color based alpha
39. 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”;
using images
the canvas element lets you draw images on its surface
44. faster
slower
size matters.
canvas performances are tightly bound to its size
45. function loop(){
var bounds=detectBoundingRect();
clearRect(bounds);
draw();
}
refresh only what matters
don’t clear the whole canvas if you can. clear only the area that changed last.
47. function loop(){
draw();
}
setInterval(loop,Math.ceil(1000/60));
you can’t beat the screen
60Hz refresh rate means a frame each 16.7ms
Align animations to the refresh speed (16.7ms=60fps - 33.4ms=30fps)
48. var thisFrame = new Date().getTime();
var dT = (thisFrame - this.lastFrame)/1000;
this.lastFrame = thisFrame;
sprite.x+= dX * dT; //smooth movement
don’t rely on a given interval
always calculate the real time between two frames to smooth the animations.
49. 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]; //alpha
i+=4;
}
video memory is slow
decrease the rate of requests to video memory. save a big chunk of data and use it.
50. 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]; //alpha
i+=4;
}
CanvasPixelArray is slow
always cache the data array in a proper javascript object
52. canvas shapes drawing double bu er pixel
size functions with drawImage manipulation
59fps 59fps 55fps
100px x 100px
10% 10% 15%
52fps 56fps 24fps
500px x 500px 70%
20% 38%
50fps 44fps 9fps
1000px x 1000px
40% 50% 92%
comparison: FPS and CPU %
http://jsperf.com/vsummit-canvas-perf/5
53. .gpu {
-webkit-transform: rotate3d(0,0,1, 0deg);
-moz-transform: rotate3d(0,0,1, 0deg);
transform: rotate3d(0,0,1, 0deg);
}
hardware acceleration
HTML5 supports GPU accelerated operations.
To be use only if you experience a true performance win. It might not be better.
54. don’t trust me. experiment always
these 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 .